Skip to content

Commit 5c6a7e7

Browse files
Merge pull request #35 from germanbisogno/develop
pass cdp session by constructor
2 parents e9b87d0 + 42af92e commit 5c6a7e7

13 files changed

Lines changed: 86 additions & 57 deletions

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ test('Test tracing', async () => {
4040
const googlePage = new GooglePage(driver);
4141

4242
// Initializes the CDP client connection
43-
await CDPSession.init(port);
43+
const cdpSession = new CDPSession();
44+
await cdpSession.init(port);
4445

4546
// Instantiates the class and produces a file as result of the trace
46-
const tracing = new Tracing('tracing.json');
47+
const tracing = new Tracing(cdpSession, 'tracing.json');
4748

4849
// start tracing
4950
await tracing.startTrace();
@@ -56,7 +57,7 @@ test('Test tracing', async () => {
5657
// do whatever with trace
5758

5859
// Close the CDP client connection
59-
await CDPSession.close()
60+
await cdpSession.close()
6061

6162
await driver.quit();
6263

@@ -84,10 +85,11 @@ test('Test performance', async () => {
8485
const googlePage = new GooglePage(driver);
8586

8687
// Initializes the CDP client connection
87-
await CDPSession.init(port);
88+
const cdpSession = new CDPSession();
89+
await cdpSession.init(port);
8890

8991
// Instantiates the class and produces two files as result of the trace
90-
const performance = new Performance('startTrace.json', 'endTrace.json');
92+
const performance = new Performance(cdpSession, 'startTrace.json', 'endTrace.json');
9193

9294
// start tracing
9395
const perfStartResults = await performance.startTrace();
@@ -101,7 +103,7 @@ test('Test performance', async () => {
101103
// Perform assertions or do whatever with perfStartResults or perfEndResults
102104

103105
// Close the CDP client connection
104-
await CDPSession.close()
106+
await cdpSession.close()
105107

106108
await driver.quit();
107109
}

images/src_diagram.png

9.77 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gbisogno/cdp-utils",
3-
"version": "1.3.3",
3+
"version": "1.3.5",
44
"description": "A set of utilities/wrapper for Test Automation or Performance testing on top of Chrome DevTools Protocol",
55
"repository": {
66
"type": "git",

src/__tests__/network.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ test('Test Network', async () => {
2323

2424
const googlePage = new GooglePage(driver);
2525

26-
await CDPSession.init(port);
27-
const network = new Network('network.har');
26+
const cdpSession = new CDPSession();
27+
await cdpSession.init(port);
28+
29+
const network = new Network(cdpSession, 'network.har');
2830

2931
await network.startTrace();
3032

@@ -35,7 +37,7 @@ test('Test Network', async () => {
3537
const networkResults: Har = await network.stopTrace();
3638
expect(networkResults.log.entries.length).toBeGreaterThan(0);
3739

38-
await CDPSession.close();
40+
await cdpSession.close();
3941

4042
await driver.quit()
4143

src/__tests__/performance.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ test('Test Performance', async () => {
2222

2323
const googlePage = new GooglePage(driver);
2424

25-
await CDPSession.init(port);
26-
const performance = new Performance('startTrace.json', 'endTrace.json');
25+
const cdpSession = new CDPSession();
26+
await cdpSession.init(port);
27+
28+
const performance = new Performance(cdpSession, 'startTrace.json', 'endTrace.json');
2729

2830
const perfStartResults = await performance.startTrace();
2931

@@ -36,7 +38,7 @@ test('Test Performance', async () => {
3638
expect(perfStartResults.metrics.length).toBeGreaterThan(0);
3739
expect(perfEndResults.metrics.length).toBeGreaterThan(0);
3840

39-
await CDPSession.close();
41+
await cdpSession.close();
4042

4143
await driver.quit()
4244

src/__tests__/runtime.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ test('Test Runtime', async () => {
2222

2323
const googlePage = new GooglePage(driver);
2424

25-
await CDPSession.init(port);
26-
const runtime = new Runtime('console.json');
25+
const cdpSession = new CDPSession();
26+
await cdpSession.init(port);
27+
28+
const runtime = new Runtime(cdpSession, 'console.json');
2729

2830
await runtime.startTrace();
2931

@@ -43,7 +45,7 @@ test('Test Runtime', async () => {
4345
expect(consoleResults.find(x => x.type === 'warning')).toBeDefined();
4446
expect(consoleResults.find(x => x.type === 'log')).toBeDefined();
4547

46-
await CDPSession.close();
48+
await cdpSession.close();
4749

4850
await driver.quit()
4951

src/__tests__/tracing.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ test('Test Tracing', async () => {
2222

2323
const googlePage = new GooglePage(driver);
2424

25-
await CDPSession.init(port);
25+
const cdpSession = new CDPSession();
26+
await cdpSession.init(port);
2627

27-
const tracing = new Tracing('tracing.json');
28+
const tracing = new Tracing(cdpSession, 'tracing.json');
2829

2930
await driver.get("https://www.google.com");
3031

@@ -36,7 +37,7 @@ test('Test Tracing', async () => {
3637

3738
expect(tracingResults.length).toBeGreaterThan(0);
3839

39-
await CDPSession.close();
40+
await cdpSession.close();
4041

4142
await driver.quit();
4243
});

src/cdpSession.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import CDP from "chrome-remote-interface";
22
import { logger } from "./utils/logger";
33

44
export class CDPSession{
5-
public static client: CDP.Client;
5+
private _client: CDP.Client;
66

77
/**
88
* Initializes the CDP client connection
99
* @port a given port number
1010
* @returns CDP Client
1111
*/
12-
public static async init(port: number): Promise<void> {
12+
public async init(port: number): Promise<void> {
1313
try {
14-
this.client = await CDP({ port });
14+
this._client = await CDP({ port });
1515

1616
logger.info('CDP Session created using port: ' + port);
1717
} catch (e) {
@@ -20,12 +20,19 @@ export class CDPSession{
2020
}
2121
}
2222

23+
/**
24+
* Gets the CDP client connection
25+
*/
26+
public get client(): CDP.Client {
27+
return this._client;
28+
}
29+
2330
/**
2431
* Closes the CDP client connection
2532
*/
26-
public static async close(): Promise<void> {
27-
if (this.client) {
28-
await this.client.close()
33+
public async close(): Promise<void> {
34+
if (this._client) {
35+
await this._client.close()
2936
}
3037
}
3138
}

src/geoLocation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as CDP from 'chrome-remote-interface';
2+
import { CDPSession } from './cdpSession';
23

34
import { Coordinates } from './interfaces/coordinates'
45
import { logger } from "./utils/logger";
56

67
export class GeoLocation {
78
private _client: CDP.Client;
89

9-
constructor(client: CDP.Client) {
10-
this._client = client;
10+
constructor(cdpSession: CDPSession) {
11+
this._client = cdpSession.client;
1112
}
1213
/**
1314
* Emulates a geo location by coordinates

src/network.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { logger } from "./utils/logger";
55
import { NetworkConditions } from './interfaces/networkConditions';
66
import { Har } from 'har-format';
77
import { CDPSession } from './cdpSession';
8+
import CDP from 'chrome-remote-interface';
89

910
// event types to observe
1011
const observe = [
@@ -76,9 +77,11 @@ export const NETWORK_PRESETS = {
7677
export class Network extends TraceOperations {
7778
private _traceFileName: string;
7879
protected _events: any[] = [];
80+
private _client: CDP.Client;
7981

80-
constructor(traceFileName: string = '') {
82+
constructor(cdpSession: CDPSession, traceFileName: string = '') {
8183
super();
84+
this._client = cdpSession.client;
8285
this._traceFileName = traceFileName;
8386
}
8487

@@ -87,11 +90,11 @@ export class Network extends TraceOperations {
8790
*/
8891
public async startTrace(): Promise<void> {
8992
try {
90-
if (CDPSession.client) {
91-
await CDPSession.client.send('Page.enable');
92-
await CDPSession.client.send('Network.enable');
93+
if (this._client) {
94+
await this._client.send('Page.enable');
95+
await this._client.send('Network.enable');
9396
observe.forEach(method => {
94-
CDPSession.client.on(method, params => {
97+
this._client.on(method, params => {
9598
this._events.push({ method, params });
9699
});
97100
});
@@ -108,7 +111,7 @@ export class Network extends TraceOperations {
108111
*/
109112
public async stopTrace(): Promise<Har> {
110113
try {
111-
if (CDPSession.client) {
114+
if (this._client) {
112115
const har = await harFromMessages(this._events, { includeTextFromResponseBody: true });
113116
if (this._traceFileName) {
114117
fs.writeFileSync(this._traceFileName, JSON.stringify(har));
@@ -119,8 +122,8 @@ export class Network extends TraceOperations {
119122
logger.error(e);
120123
throw e;
121124
} finally {
122-
await CDPSession.client.send('Page.disable');
123-
await CDPSession.client.send('Network.disable');
125+
await this._client.send('Page.disable');
126+
await this._client.send('Network.disable');
124127
}
125128
return { log: { entries: [], version: '', creator: { name: '', version: '' } } }
126129
}
@@ -131,8 +134,8 @@ export class Network extends TraceOperations {
131134
*/
132135
public async emulateNetworkConditions(networkConditions: NetworkConditions): Promise<void> {
133136
try {
134-
if (CDPSession.client) {
135-
await CDPSession.client.send('Network.emulateNetworkConditions', networkConditions);
137+
if (this._client) {
138+
await this._client.send('Network.emulateNetworkConditions', networkConditions);
136139
}
137140
} catch (e) {
138141
logger.error(e);

0 commit comments

Comments
 (0)