Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 3c75213

Browse files
committed
chore(test): fix attach session test (#5063)
1 parent 0c6b134 commit 3c75213

File tree

4 files changed

+119
-94
lines changed

4 files changed

+119
-94
lines changed

lib/driverProviders/attachSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class AttachSession extends DriverProvider {
3838
getNewDriver(): WebDriver {
3939
const httpClient = new http.HttpClient(this.config_.seleniumAddress);
4040
const executor = new http.Executor(httpClient);
41-
const newDriver = WebDriver.attachToSession(executor, this.config_.seleniumSessionId);
41+
const newDriver = WebDriver.attachToSession(executor, this.config_.seleniumSessionId, null);
4242
this.drivers_.push(newDriver);
4343
return newDriver;
4444
}

lib/driverProviders/driverProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* It is responsible for setting up the account object, tearing
44
* it down, and setting up the driver correctly.
55
*/
6-
import {Builder, Session, WebDriver} from 'selenium-webdriver';
6+
import {Builder, WebDriver} from 'selenium-webdriver';
77

88
import {BlockingProxyRunner} from '../bpRunner';
99
import {Config} from '../config';

scripts/driverProviderAttachSession.js

Lines changed: 116 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,130 @@
22

33
'use strict';
44

5-
var http = require('http'),
6-
spawn = require('child_process').spawnSync;
5+
const http = require('http');
6+
const child_process = require('child_process');
77

8-
var sessionId = '';
9-
10-
// 1. Create a new selenium session.
11-
var postData = JSON.stringify(
12-
{'desiredCapabilities': {'browserName': 'firefox'}});
13-
var createOptions = {
14-
hostname: 'localhost',
15-
port: 4444,
16-
path: '/wd/hub/session',
17-
method: 'POST',
18-
headers: {
19-
'Content-Type': 'application/x-www-form-urlencoded',
20-
'Content-Length': Buffer.byteLength(postData)
21-
}
22-
};
23-
var req = http.request(createOptions, function(res) {
24-
res.on('data', setBody);
25-
res.on('end', checkSession);
26-
});
27-
req.write(postData);
28-
req.end();
29-
30-
// 2. After making the request to create a selenium session, read the selenium
31-
// session id.
32-
var setBody = function(chunk) {
33-
var body = chunk.toString();
34-
sessionId = JSON.parse(body).sessionId;
8+
// Delete session method to be used at the end of the test as well as
9+
// when the tests fail.
10+
const deleteSession = (sessionId, err) => {
11+
return new Promise(resolve => {
12+
const deleteOptions = {
13+
hostname: 'localhost',
14+
port: 4444,
15+
path: '/wd/hub/session/' + sessionId,
16+
method: 'DELETE'
17+
};
18+
const req = http.request(deleteOptions, res => {
19+
res.on('end', () => {
20+
if (err) {
21+
throw err;
22+
}
23+
resolve();
24+
});
25+
});
26+
req.end();
27+
});
3528
};
3629

37-
// 3. After getting the session id, verify that the selenium session exists.
38-
// If the session exists, run the protractor test.
39-
var checkSession = function() {
40-
var checkOptions = {
41-
hostname: 'localhost',
42-
port: 4444,
43-
path: '/wd/hub/session/' + sessionId,
44-
method: 'GET'
45-
};
46-
var state = '';
47-
var req = http.request(checkOptions, function(res) {
48-
res.on('data', function(chunk) {
49-
state = JSON.parse(chunk.toString()).state;
50-
});
51-
res.on('end', function() {
52-
if (state === 'success') {
53-
var runProtractor = spawn('node',
54-
['bin/protractor', 'spec/driverProviderAttachSessionConf.js',
55-
'--seleniumSessionId=' + sessionId]);
56-
console.log(runProtractor.stdout.toString());
57-
if (runProtractor.status !== 0) {
58-
throw new Error('Protractor did not run properly.');
59-
}
30+
const run = async () => {
31+
// 1. Create a new selenium session.
32+
const sessionId = await new Promise(resolve => {
33+
const postData = JSON.stringify(
34+
{'desiredCapabilities': {'browserName': 'chrome'}});
35+
const createOptions = {
36+
hostname: '127.0.0.1',
37+
port: 4444,
38+
path: '/wd/hub/session',
39+
method: 'POST',
40+
headers: {
41+
'Content-Type': 'application/x-www-form-urlencoded',
42+
'Content-Length': Buffer.byteLength(postData)
6043
}
61-
else {
62-
throw new Error('The selenium session was not created.');
63-
}
64-
checkStoppedSession();
44+
};
45+
let body = '';
46+
const req = http.request(createOptions, (res) => {
47+
res.on('data', (data) => {
48+
body = JSON.parse(data.toString());
49+
});
50+
res.on('end', () => {
51+
resolve(body.sessionId);
52+
});
6553
});
54+
req.write(postData);
55+
req.end();
6656
});
67-
req.end();
68-
};
6957

70-
// 4. After the protractor test completes, check to see that the session still
71-
// exists. If we can find the session, delete it.
72-
var checkStoppedSession = function() {
73-
var checkOptions = {
74-
hostname: 'localhost',
75-
port: 4444,
76-
path: '/wd/hub/session/' + sessionId,
77-
method: 'GET'
78-
};
79-
var state = '';
80-
var req = http.request(checkOptions, function(res) {
81-
res.on('data', function(chunk) {
82-
state = JSON.parse(chunk.toString()).state;
58+
await new Promise(resolve => {
59+
// 2. After getting the session id, verify that the selenium session exists.
60+
// If the session exists, run the protractor test.
61+
const checkOptions = {
62+
hostname: '127.0.0.1',
63+
port: 4444,
64+
path: '/wd/hub/sessions',
65+
method: 'GET'
66+
};
67+
68+
let values = [];
69+
const req = http.request(checkOptions, (res) => {
70+
res.on('data', (chunk) => {
71+
values = JSON.parse(chunk.toString())['value'];
72+
});
73+
res.on('end', () => {
74+
let found = false;
75+
for (let value of values) {
76+
if (value['id'] === sessionId) {
77+
found = true;
78+
}
79+
}
80+
if (found) {
81+
resolve();
82+
} else {
83+
throw new Error('The selenium session was not created.');
84+
}
85+
});
8386
});
84-
res.on('end', function() {
85-
if (state === 'success') {
86-
deleteSession();
87-
}
88-
else {
89-
throw new Error('The selenium session should still exist.');
90-
}
87+
req.end();
88+
});
89+
90+
// 3. Run Protractor and attach to the session.
91+
const runProtractor = child_process.spawnSync('node',
92+
['bin/protractor', 'spec/driverProviderAttachSessionConf.js',
93+
'--seleniumSessionId=' + sessionId]);
94+
console.log(runProtractor.stdout.toString());
95+
if (runProtractor.status !== 0) {
96+
const e = new Error('Protractor did not run properly.');
97+
deleteSession(sessionId, e);
98+
}
99+
100+
// 4. After the protractor test completes, check to see that the session still
101+
// exists. If we can find the session, delete it.
102+
await new Promise(resolve => {
103+
const checkOptions = {
104+
hostname: '127.0.0.1',
105+
port: 4444,
106+
path: '/wd/hub/session/' + sessionId,
107+
method: 'GET'
108+
};
109+
const req = http.request(checkOptions, (res) => {
110+
let state = '';
111+
res.on('data', (chunk) => {
112+
state = JSON.parse(chunk.toString()).state;
113+
});
114+
res.on('end', () => {
115+
if (state === 'success') {
116+
resolve();
117+
}
118+
else {
119+
const e = new Error('The selenium session should still exist.');
120+
deleteSession(sessionId, e);
121+
}
122+
});
91123
});
124+
req.end();
92125
});
93-
req.end();
94-
};
95126

96-
// 5. Delete the selenium session.
97-
var deleteSession = function() {
98-
var deleteOptions = {
99-
hostname: 'localhost',
100-
port: 4444,
101-
path: '/wd/hub/session/' + sessionId,
102-
method: 'DELETE'
103-
};
104-
var req = http.request(deleteOptions);
105-
req.end();
106-
};
127+
// 5. Delete the selenium session.
128+
await deleteSession(sessionId);
129+
}
130+
131+
run().then();

scripts/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const passingTests = [
4343
'node built/cli.js spec/built/noCFBasicConf.js',
4444
'node built/cli.js spec/built/noCFBasicConf.js --useBlockingProxy',
4545
'node built/cli.js spec/built/noCFPluginConf.js',
46-
// //'node scripts/driverProviderAttachSession.js',
46+
'node scripts/driverProviderAttachSession.js',
4747
// 'node scripts/errorTest.js',
4848
// // Unit tests
4949
// 'node node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=scripts/unit_test.json',

0 commit comments

Comments
 (0)