Skip to content

Commit 12d7ab5

Browse files
committed
put this into a shared library
1 parent 3d78d2c commit 12d7ab5

File tree

2 files changed

+146
-97
lines changed

2 files changed

+146
-97
lines changed

test/browser/selenium-init.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
_ = require("jasmine-node");
2+
var server = require("./../../src/server.js");
3+
var webdriver = require('selenium-webdriver');
4+
5+
function start(withDriver) {
6+
if (process.env["TEST_LOC"] === "local") {
7+
server.start({
8+
baseUrl: process.env["BASE_URL"],
9+
port: process.env["PORT"],
10+
sessionSecret: process.env["SESSION_SECRET"],
11+
google: {
12+
clientId: process.env["GOOGLE_CLIENT_ID"],
13+
clientSecret: process.env["GOOGLE_CLIENT_SECRET"],
14+
redirect: "/oauth2callback"
15+
}
16+
}, function(app, server) {
17+
var driver = new webdriver.Builder().
18+
withCapabilities({browserName: "chrome"}).
19+
build();
20+
withDriver(server, process.env["BASE_URL"], driver);
21+
});
22+
}
23+
else {
24+
var driver = new webdriver.Builder().
25+
usingServer("https://ondemand.saucelabs.com/wd/hub").
26+
withCapabilities({
27+
browserName: "internet explorer",
28+
username: process.env["SAUCE_USERNAME"],
29+
accessKey: process.env["SAUCE_ACCESS_KEY"]
30+
}).
31+
build();
32+
withDriver(null, process.env["SAUCE_TEST_TARGET"], driver);
33+
}
34+
}
35+
36+
var googleUsername = process.env["SELENIUM_GOOGLE_USER"];
37+
var googlePassword = process.env["SELENIUM_GOOGLE_PASSWORD"];
38+
39+
function googleLogin(driver) {
40+
driver.wait(function() {
41+
return driver.getTitle().then(function(title) {
42+
return title === 'Sign in - Google Accounts';
43+
});
44+
}, 3000);
45+
// Sometimes email isn't present because the browser remembers which
46+
// Google account we last logged in as
47+
driver.findElement(webdriver.By.id("Email")).getAttribute("class").then(function(cls) {
48+
if(cls.indexOf("hidden") === -1) {
49+
driver.findElement(webdriver.By.id("Email")).sendKeys(googleUsername);
50+
}
51+
driver.findElement(webdriver.By.id("Passwd")).sendKeys(googlePassword);
52+
driver.findElement(webdriver.By.id("signIn")).click();
53+
});
54+
}
55+
function googleLogout(driver) {
56+
driver.get("https://accounts.google.com/Logout");
57+
}
58+
function waitThenClick(driver, query) {
59+
driver.wait(function() {
60+
return driver.isElementPresent(query);
61+
}, 4000);
62+
return driver.findElement(query).click();
63+
}
64+
function contains(str) {
65+
return webdriver.By.xpath("//*[contains(text(), '" + str + "')]")
66+
}
67+
68+
function setupExceptions(test, done) {
69+
webdriver.promise.controlFlow().on('uncaughtException', function(e) {
70+
console.error('Unhandled error: ' + e);
71+
test.fail(new Error("Unhandled exception: " + e));
72+
done();
73+
});
74+
}
75+
76+
function webbit(description, runner, timeout) {
77+
it(description, function(done) {
78+
setupExceptions(this, done);
79+
runner(done);
80+
}, timeout);
81+
}
82+
83+
function waitForPyretLoad(driver, timeout) {
84+
return driver.wait(function() {
85+
console.log("Waiting for loader to disappear...");
86+
return driver.findElement(webdriver.By.id("loader")).getCssValue("display")
87+
.then(function(d) {
88+
console.log("style: ", d);
89+
return d === "none";
90+
});
91+
}, timeout || 3000);
92+
}
93+
94+
module.exports = {
95+
webbit: webbit,
96+
googleLogout: googleLogout,
97+
googleLogin: googleLogin,
98+
contains: contains,
99+
waitThenClick: waitThenClick,
100+
waitForPyretLoad: waitForPyretLoad,
101+
start: start
102+
};

test/browser/signin.js

Lines changed: 44 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,64 @@
11
_ = require("jasmine-node");
2-
var server = require("./../../src/server.js");
2+
var tester = require("./selenium-init.js");
33
var webdriver = require('selenium-webdriver');
44

5-
if (process.env["TEST_LOC"] === "local") {
6-
server.start({
7-
baseUrl: process.env["BASE_URL"],
8-
port: process.env["PORT"],
9-
sessionSecret: process.env["SESSION_SECRET"],
10-
google: {
11-
clientId: process.env["GOOGLE_CLIENT_ID"],
12-
clientSecret: process.env["GOOGLE_CLIENT_SECRET"],
13-
redirect: "/oauth2callback"
14-
}
15-
}, function(app, server) {
16-
var driver = new webdriver.Builder().
17-
withCapabilities({browserName: "chrome"}).
18-
build();
19-
afterServer(server, process.env["BASE_URL"], driver);
20-
});
21-
}
22-
else {
23-
var driver = new webdriver.Builder().
24-
usingServer("https://ondemand.saucelabs.com/wd/hub").
25-
withCapabilities({
26-
browserName: "internet explorer",
27-
username: process.env["SAUCE_USERNAME"],
28-
accessKey: process.env["SAUCE_ACCESS_KEY"]
29-
}).
30-
build();
31-
afterServer(null, process.env["SAUCE_TEST_TARGET"], driver);
32-
}
33-
34-
35-
var googleUsername = process.env["SELENIUM_GOOGLE_USER"];
36-
var googlePassword = process.env["SELENIUM_GOOGLE_PASSWORD"];
5+
var contains = tester.contains;
6+
var waitThenClick = tester.waitThenClick;
7+
var googleLogin = tester.googleLogin;
8+
var googleLogout = tester.googleLogout;
379

38-
function afterServer(maybeServer, baseUrl, driver) {
39-
function googleLogin(driver) {
40-
driver.wait(function() {
41-
return driver.getTitle().then(function(title) {
42-
return title === 'Sign in - Google Accounts';
43-
});
44-
}, 3000);
45-
// Sometimes email isn't present because the browser remembers which
46-
// Google account we last logged in as
47-
driver.findElement(webdriver.By.id("Email")).getAttribute("class").then(function(cls) {
48-
if(cls.indexOf("hidden") === -1) {
49-
driver.findElement(webdriver.By.id("Email")).sendKeys(googleUsername);
50-
}
51-
driver.findElement(webdriver.By.id("Passwd")).sendKeys(googlePassword);
52-
driver.findElement(webdriver.By.id("signIn")).click();
53-
});
54-
}
55-
function waitThenClick(driver, query) {
56-
driver.wait(function() {
57-
return driver.isElementPresent(query);
58-
}, 4000);
59-
return driver.findElement(query).click();
60-
}
10+
tester.start(function(maybeServer, baseUrl, driver) {
6111
describe("Sign in", function() {
62-
webdriver.promise.controlFlow().on('uncaughtException', function(e) {
63-
console.error('Unhandled error: ' + e);
64-
fail();
65-
});
6612

67-
it("Should forget everything it knows", function(done) {
68-
try {
69-
driver.get("https://security.google.com/settings/security/permissions");
70-
googleLogin(driver);
71-
console.log("waiting...");
72-
driver.wait(function() {
73-
console.log("Waiting for page...");
74-
return driver.executeScript("return document.readyState === 'complete'");
75-
}, 3000);
76-
// driver.isElementPresent(webdriver.By.xpath("//*[contains(text(), 'Patch Test')]")).then(function(p) { console.log(p); });
77-
driver.isElementPresent(webdriver.By.xpath("//*[contains(text(), 'Patch Test')]")).then(function(present) {
78-
console.log("present: ", present);
79-
if(present) {
80-
waitThenClick(driver, webdriver.By.xpath("//*[contains(text(), 'Patch Test')]"));
81-
waitThenClick(driver, webdriver.By.xpath("//*[contains(text(), 'Revoke access')]"));
82-
return waitThenClick(driver, webdriver.By.name("ok"));
83-
} else {
84-
// do nothing
85-
}
86-
});
87-
driver.get("https://accounts.google.com/Logout");
88-
driver.call(function() { done(); });
89-
}
90-
catch(e) { console.log(e); }
13+
tester.webbit("Should forget everything it knows", function(done) {
14+
driver.get("https://security.google.com/settings/security/permissions");
15+
googleLogin(driver);
16+
console.log("Waiting to see if Patch permissions are present...");
17+
driver.wait(function() {
18+
return driver.executeScript("return document.readyState === 'complete'");
19+
}, 3000);
20+
driver.isElementPresent(contains("Patch Test")).then(function(present) {
21+
if(present) {
22+
waitThenClick(driver, contains("Patch Test"));
23+
waitThenClick(driver, contains("Revoke access"));
24+
return waitThenClick(driver, webdriver.By.name("ok"));
25+
} else {
26+
// do nothing otherwise
27+
}
28+
});
29+
googleLogout(driver);
30+
driver.call(done);
9131
}, 60000);
9232

93-
it("Should sign up from not being logged in", function(done) {
94-
console.log("sign up");
33+
tester.webbit("Should sign up from not being logged in", function(done) {
9534
driver.get(baseUrl);
9635
driver.findElement(webdriver.By.id('login')).click();
9736
googleLogin(driver);
37+
console.log("Waiting for permission confirmation button...");
9838
driver.wait(function() {
99-
console.log("Waiting for confirmation button to activate...");
10039
return driver.findElement(webdriver.By.id("submit_approve_access")).getAttribute("disabled")
10140
.then(function(disabled) {
10241
return !disabled;
10342
});
10443
}, 3000);
10544
driver.findElement(webdriver.By.id("submit_approve_access")).click();
106-
var loaded = driver.wait(function() {
107-
console.log("Waiting for page to load..");
108-
return driver.findElement(webdriver.By.id("loader")).getCssValue("display")
109-
.then(function(d) {
110-
console.log("style: ", d);
111-
return d === "none";
112-
});
113-
}, 3000);
114-
loaded.then(function() { console.log("done with test"); done(); });
45+
tester.waitForPyretLoad(driver);
46+
driver.call(done);
47+
}, 60000);
48+
49+
tester.webbit("When logging back in, should skip authentication and go straight to my-programs", function(done) {
50+
driver.get(baseUrl);
51+
driver.findElement(webdriver.By.id('login')).click();
52+
tester.waitForPyretLoad(driver);
53+
driver.call(done);
54+
}, 60000);
55+
56+
tester.webbit("If cookies are cleared, should still log in seamlessly and work", function(done) {
57+
driver.manage().deleteAllCookies();
58+
driver.get(baseUrl);
59+
driver.findElement(webdriver.By.id('login')).click();
60+
tester.waitForPyretLoad(driver);
61+
driver.call(done);
11562
}, 60000);
11663

11764
it("Should close the server and the connection to the browser", function(done) {
@@ -122,4 +69,4 @@ function afterServer(maybeServer, baseUrl, driver) {
12269
});
12370

12471
});
125-
}
72+
});

0 commit comments

Comments
 (0)