Skip to content

Commit 295a63e

Browse files
Merge pull request #43 from germanbisogno/develop
Develop
2 parents 12caf96 + 33fab94 commit 295a63e

24 files changed

Lines changed: 451 additions & 192 deletions

.mocharc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extensions": ["ts"],
3+
"spec": ["./src/__tests__/**/*.test.*"],
4+
"node-option": [
5+
"es-module-specifier-resolution=node",
6+
"loader=ts-node/esm"
7+
],
8+
"timeout": "20000"
9+
}

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [1.5.0] - 2023-03-16
6+
7+
### Added
8+
9+
- Migrate to mocha test runner
10+
- Update documentation
11+
- Update lighthouse version to the latest
12+
13+
## [1.4.2] - 2023-03-11
14+
15+
### Added
16+
17+
- Adding support to convert network HAR object into a request format.
18+
- Adding storage manager to store and send request metrics to a database backend.
19+
- CHANGELOG.md file to track changes.
20+
21+
### Changed
22+
23+
- Updated documentation for Network domain by giving examples of how to use it.
24+
25+
### Fixed
26+
27+
Fixing types

README.md

Lines changed: 148 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,52 @@
88

99
## Inspiration
1010

11-
It has been inspired in the power of CDP to interact with the browser like you would do using chrome dev tools but programmatically, **this library is independent of any test automation framework** since it talks directly with chrome using `--remote-debugging-port` commonly 9222 port. There are situations when we need to emulate or collect information from the browser during the execution of e2e tests. This lib intends to be an utility for testing purposes without the need to program each functionality.
11+
It has been inspired in the power of CDP to interact with the browser like you would do using chrome dev tools but programmatically, **this library is independent of any test automation framework** since it talks directly with chrome using `--remote-debugging-port` commonly 9222 port. There are situations when we need to emulate or collect information from the browser during the execution of e2e tests. This lib intends to be an utility for testing purposes without the need to program each functionality.
1212

1313
More inpiration has been based on existing tools that are using this protocol like [puppeteer](https://github.com/puppeteer/puppeteer), see https://puppeteer.github.io/puppeteer/docs/puppeteer.tracing
1414

1515
## Important
1616

17-
It is not a replacement of any actual tool or library, instead it is just a wrapper with common utilities for Test Automation or Performance testing. For example Selenium 4 introduces a new powerful API which grants access to Chrome DevTools directly from your automated tests just accessing it by `driver.getDevTools();`, this is another way to use CDP programmatically in your scripts. It can be used also by importing directly `chrome-remote-interface` like this project is referencing in its package.json.
17+
It is not a replacement of any actual tool or library, instead it is just a wrapper with common utilities for Test Automation or Performance testing. For example Selenium 4 introduces a new powerful API which grants access to Chrome DevTools directly from your automated tests just accessing it by `driver.getDevTools();`, this is another way to use CDP programmatically in your scripts. It can be used also by importing directly `chrome-remote-interface` like this project is referencing in its package.json.
1818

1919
You are very welcome if you feel that can contribute with more utilities to take advantaje of this great API.
2020

2121
## Usage
2222

2323
### Config (Only required for Tracing domain)
2424

25-
Create a config folder and place your cdp.config.json file there. It will contain configurations needed for tracing such as including or excluding categories and more. See the following example of how it will look like:
25+
Create a config folder and place your cdp.config.json file there. It will contain configurations needed for tracing such as including or excluding categories and more. See the following example of how it will look like:
2626

2727
For more information about how to use traceConfig, see [TraceConfig](https://chromedevtools.github.io/devtools-protocol/tot/Tracing/#type-TraceConfig)
2828

2929
```json
30-
3130
{
32-
"tracing": {
33-
"traceConfig": {
34-
"includedCategories": [
35-
"-*",
36-
"devtools.timeline",
37-
"v8.execute",
38-
"disabled-by-default-devtools.timeline",
39-
"disabled-by-default-devtools.timeline.frame",
40-
"toplevel",
41-
"blink.console",
42-
"blink.user_timing",
43-
"latencyInfo",
44-
"disabled-by-default-devtools.timeline",
45-
"disabled-by-default-devtools.timeline.frame",
46-
"disabled-by-default-devtools.timeline.stack",
47-
"disabled-by-default-devtools.screenshot",
48-
"disabled-by-default-v8.cpu_profiler"
49-
],
50-
"excludedCategories": [
51-
"-*"
52-
]
53-
}
54-
},
55-
"cdpPort": 9222,
56-
"maxTimeout": 50000
31+
"tracing": {
32+
"traceConfig": {
33+
"includedCategories": [
34+
"-*",
35+
"devtools.timeline",
36+
"v8.execute",
37+
"disabled-by-default-devtools.timeline",
38+
"disabled-by-default-devtools.timeline.frame",
39+
"toplevel",
40+
"blink.console",
41+
"blink.user_timing",
42+
"latencyInfo",
43+
"disabled-by-default-devtools.timeline",
44+
"disabled-by-default-devtools.timeline.frame",
45+
"disabled-by-default-devtools.timeline.stack",
46+
"disabled-by-default-devtools.screenshot",
47+
"disabled-by-default-v8.cpu_profiler"
48+
],
49+
"excludedCategories": ["-*"]
50+
}
51+
},
52+
"cdpPort": 9222,
53+
"maxTimeout": 50000
5754
}
58-
5955
```
6056

61-
6257
### Tracing
6358

6459
The following example shows how to use the Tracing class with Selenium Webdriver.
@@ -67,129 +62,180 @@ The following example shows how to use the Tracing class with Selenium Webdriver
6762

6863
import { CDPClient, Tracing } from "cdp-utils";
6964

70-
test('Test tracing', async () => {
71-
const options = new chrome.Options();
65+
it('Test tracing', async () => {
66+
const options = new chrome.Options();
7267

73-
options.addArguments(`--remote-debugging-port=${port}`);
68+
options.addArguments(`--remote-debugging-port=${port}`);
7469

75-
const driver = await new Builder().forBrowser('chrome')
76-
.setChromeOptions(options)
77-
.build();
70+
const driver = await new Builder()
71+
.forBrowser('chrome')
72+
.setChromeOptions(options)
73+
.build();
7874

79-
const googlePage = new GooglePage(driver);
75+
const googlePage = new GooglePage(driver);
8076

81-
// Initializes the CDP client connection
82-
const cdpClient = new CDPClient();
83-
await cdpClient.init(port);
84-
85-
// Instantiates the class and produces a file as result of the trace
86-
const tracing = new Tracing(cdpClient, 'tracing.json');
77+
// Initializes the CDP client connection
78+
const cdpClient = new CDPClient();
79+
await cdpClient.init(port);
8780

88-
// start tracing
89-
await tracing.startTrace();
81+
// Instantiates the class and produces a file as result of the trace
82+
const tracing = new Tracing(cdpClient, 'tracing.json');
9083

91-
// here you perform your test steps
84+
// start tracing
85+
await tracing.startTrace();
9286

93-
// stop tracing
94-
const trace = await tracing.stopTrace();
87+
// here you perform your test steps
9588

96-
// do whatever with trace
89+
// stop tracing
90+
const tracingResults = await tracing.stopTrace();
9791

98-
// Close the CDP client connection
99-
await cdpClient.close()
92+
// do whatever with trace
10093

101-
await driver.quit();
94+
// Close the CDP client connection
95+
await cdpClient.close();
10296

97+
await driver.quit();
10398
}
10499

105100
```
106101
102+
### Network
103+
104+
The following example shows how to use the Network class with Selenium Webdriver. Notice that it is possible to convert HAR object returned by `stopTrace` into a
105+
more readable object. The obtained requests can be sent to a database to visualize them in a reporting tool like Grafana.
106+
107+
For more information about how to send metrics to a database, please check the documentation [Capturing requests](https://github.com/germanbisogno/cdp-utils/tree/main/src/performance#capturing-requests)
108+
109+
```js
110+
import { Network, CDPClient } from 'cdp-utils';
111+
112+
it('Test Network', async () => {
113+
const options = new chrome.Options();
114+
115+
options.addArguments(`--remote-debugging-port=${port}`);
116+
117+
const driver = await new Builder()
118+
.forBrowser('chrome')
119+
.setChromeOptions(options)
120+
.build();
121+
122+
const googlePage = new GooglePage(driver);
123+
const cdpClient = new CDPClient();
124+
await cdpClient.init(port);
125+
126+
const network = new Network(cdpClient, 'network.har');
127+
128+
await network.startTrace();
129+
130+
// here you perform your test steps
131+
132+
// Collect network data
133+
const networkResults: Har = await network.stopTrace();
134+
expect(networkResults.log.entries.length).greaterThan(0);
135+
136+
// Convert HAR to an request object
137+
const requests = Utils.transformHar(networkResults);
138+
// Send metrics to a database
139+
await DatabaseManager.getDatabaseProvider().sendRequests(requests);
140+
141+
// Close the CDP client connection
142+
await cdpClient.close();
143+
144+
await driver.quit();
145+
});
146+
```
147+
107148
### Performance
108149
109150
An example using the Performance class with Selenium Webdriver.
110151
111152
```js
112153

113-
import { CDPClient, Performance } from "cdp-utils";
154+
import { CDPClient, Performance } from 'cdp-utils';
155+
156+
it('Test performance', async () => {
157+
const options = new chrome.Options();
114158

115-
test('Test performance', async () => {
116-
const options = new chrome.Options();
159+
options.addArguments(`--remote-debugging-port=${port}`);
117160

118-
options.addArguments(`--remote-debugging-port=${port}`);
161+
const driver = await new Builder()
162+
.forBrowser('chrome')
163+
.setChromeOptions(options)
164+
.build();
119165

120-
const driver = await new Builder().forBrowser('chrome')
121-
.setChromeOptions(options)
122-
.build();
166+
const googlePage = new GooglePage(driver);
123167

124-
const googlePage = new GooglePage(driver);
125-
126-
// Initializes the CDP client connection
127-
const cdpClient = new CDPClient();
128-
await cdpClient.init(port);
129-
130-
// Instantiates the class and produces two files as result of the trace
131-
const performance = new Performance(cdpClient, 'startTrace.json', 'endTrace.json');
168+
// Initializes the CDP client connection
169+
const cdpClient = new CDPClient();
170+
await cdpClient.init(port);
132171

133-
// start tracing
134-
const perfStartResults = await performance.startTrace();
172+
// Instantiates the class and produces two files as result of the trace
173+
const performance = new Performance(
174+
cdpClient,
175+
'startTrace.json',
176+
'endTrace.json'
177+
);
135178

136-
await driver.get("https://www.google.com");
179+
// start tracing
180+
const perfStartResults = await performance.startTrace();
137181

138-
await googlePage.search('test');
182+
await driver.get('https://www.google.com');
139183

140-
const perfEndResults = await performance.stopTrace();
184+
await googlePage.search('test');
141185

142-
// Perform assertions or do whatever with perfStartResults or perfEndResults
186+
const perfEndResults = await performance.stopTrace();
143187

144-
// Close the CDP client connection
145-
await cdpClient.close()
188+
// Perform assertions or do whatever with perfStartResults or perfEndResults
146189

147-
await driver.quit();
190+
// Close the CDP client connection
191+
await cdpClient.close();
192+
193+
await driver.quit();
148194
}
149195

150196
```
151197
152198
### Lighthouse
153199
154-
An example using the Lighthouse class with Selenium Webdriver. Notice that it combines lighthouse/puppeteer in order to initialize the workflow with the configuration required and navigate to the first page.
200+
An example using the Lighthouse class with Selenium Webdriver. Notice that it combines lighthouse/puppeteer in order to initialize the workflow with the configuration required and navigate to the first page.
155201
156202
```js
157203

158-
import { Lighthouse } from "cdp-utils";
159-
import * as DesktopConfig from 'lighthouse/lighthouse-core/config/desktop-config.js';
204+
import { Lighthouse } from 'cdp-utils';
205+
import { desktopConfig } from 'lighthouse';
206+
207+
it('Test performance', async () => {
208+
const options = new chrome.Options();
160209

161-
test('Test performance', async () => {
162-
const options = new chrome.Options();
210+
options.addArguments(`--remote-debugging-port=${port}`);
163211

164-
options.addArguments(`--remote-debugging-port=${port}`);
212+
const driver = await new Builder()
213+
.forBrowser('chrome')
214+
.setChromeOptions(options)
215+
.build();
165216

166-
const driver = await new Builder().forBrowser('chrome')
167-
.setChromeOptions(options)
168-
.build();
217+
const googlePage = new GooglePage(driver);
169218

170-
const googlePage = new GooglePage(driver);
171-
172-
const lighthouse = new Lighthouse(port);
219+
const lighthouse = new Lighthouse(port);
173220

174-
await lighthouse.initWorkFlow('Google search', DesktopConfig.settings);
221+
await lighthouse.initWorkFlow('Google search', desktopConfig);
175222

176-
await lighthouse.navigate("https://www.google.com");
223+
await lighthouse.navigate('https://www.google.com');
177224

178-
await lighthouse.startTrace('search operation');
225+
await lighthouse.startTrace('search operation');
179226

180-
await googlePage.search('test');
227+
await googlePage.search('test');
181228

182-
const res = await lighthouse.stopTrace();
229+
const res = await lighthouse.stopTrace();
183230

184-
await lighthouse.generateFlowReport('lighthouse.html');
231+
await driver.quit();
185232

186-
await driver.quit();
187-
188-
// make assertions using RunnerResult array
189-
res.forEach((step) => {
190-
expect(step.lhr.categories.performance.score).toBeGreaterThanOrEqual(0.8);
191-
})
233+
await lighthouse.generateFlowReport('lighthouse.html');
192234

235+
// make assertions using RunnerResult array
236+
res.forEach((step) => {
237+
expect(step.lhr.categories.performance.score).greaterThanOrEqual(0.8);
238+
});
193239
}
194240

195241
```
@@ -209,6 +255,7 @@ npm install
209255
npm run build
210256

211257
```
258+
212259
## Run tests
213260
214261
```sh

babel.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

images/grafana_request_time.png

29.3 KB
Loading

0 commit comments

Comments
 (0)