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

Commit b32f5a5

Browse files
committed
feat(config): add exmples for dealing with log-in
Adds examples for how to log in when the login page is not written in Angular. New examples are in spec/login.
1 parent 835f6f2 commit b32f5a5

File tree

10 files changed

+194
-6
lines changed

10 files changed

+194
-6
lines changed

docs/faq.md

+13
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,16 @@ Protractor attempts to wait until the page is completely loaded before
88
performing any action (such as finding an element or sending a command to
99
an element). If your application continuously polls $timeout or $http, it will
1010
never be registered as completely loaded. Discussion of this is in [issue 59](https://github.com/angular/protractor/issues/49).
11+
12+
13+
_How do I deal with my log-in page?_
14+
15+
If your app needs log-in, there are a couple ways to deal with it. If your login
16+
page is not written with Angular, you'll need to interact with it via
17+
unwrapped webdriver, which can be accessed like `ptor.driver.get()`.
18+
19+
You can put your log-in code into an `onPrepare` function, which will be run
20+
once before any of your tests. See [this example](https://github.com/angular/protractor/blob/master/spec/login/viaConfigConf.js).
21+
22+
If you would like to do your login in your test suite itself, see
23+
[this example](https://github.com/angular/protractor/blob/master/spec/login/viaTestSpec.js).

lib/cli.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ var startJasmineTests = function() {
145145
usingServer(config.seleniumAddress).
146146
withCapabilities(config.capabilities).build();
147147

148-
driver.manage().timeouts().setScriptTimeout(100000);
149148
driver.getSession().then(function(session) {
149+
driver.manage().timeouts().setScriptTimeout(100000);
150+
150151
id = session.getId();
151152

152153
protractor.setInstance(protractor.wrapDriver(driver, config.baseUrl, config.rootElement));
@@ -163,11 +164,19 @@ var startJasmineTests = function() {
163164

164165
// Let the configuration configure the protractor instance before running
165166
// the tests.
166-
if (config.onPrepare) {
167-
config.onPrepare();
168-
}
169-
170-
minijn.executeSpecs(options);
167+
webdriver.promise.controlFlow().execute(function() {
168+
if (config.onPrepare) {
169+
if (typeof config.onPrepare == 'function') {
170+
config.onPrepare();
171+
} else if (typeof config.onPrepare == 'string') {
172+
require(path.resolve(process.cwd(), config.onPrepare));
173+
} else {
174+
throw 'config.onPrepare must be a string or function';
175+
}
176+
}
177+
}).then(function() {
178+
minijn.executeSpecs(options);
179+
});
171180
});
172181
}
173182

referenceConf.js

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ exports.config = {
6262

6363
// A callback function called once protractor is ready and available, and
6464
// before the specs are executed
65+
// You can specify a file containing code to run by setting onPrepare to
66+
// the filename string.
6567
onPrepare: function() {
6668
// At this point, global 'protractor' object will be set up, and jasmine
6769
// will be available. For example, you can add a Jasmine reporter with:

spec/login/viaConfigConf.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This is the configuration file showing how a suite of tests might
2+
// handle log-in using the onPrepare field.
3+
exports.config = {
4+
seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar',
5+
chromeDriver: './selenium/chromedriver',
6+
7+
seleniumAddress: 'http://localhost:4444/wd/hub',
8+
9+
specs: [
10+
'viaConfigSpec.js'
11+
],
12+
13+
capabilities: {
14+
'browserName': 'chrome'
15+
},
16+
17+
onPrepare: function() {
18+
var ptor = protractor.getInstance();
19+
ptor.driver.get('http://localhost:8000/app/login.html');
20+
21+
ptor.driver.findElement(protractor.By.id('username')).sendKeys('Jane');
22+
ptor.driver.findElement(protractor.By.id('password')).sendKeys('1234');
23+
ptor.driver.findElement(protractor.By.id('clickme')).click();
24+
25+
// Login takes some time, so wait until it's done.
26+
// For the test app's login, we know it's done when it redirects to
27+
// index.html.
28+
ptor.wait(function() {
29+
return ptor.driver.getCurrentUrl().then(function(url) {
30+
return /index/.test(url);
31+
});
32+
});
33+
},
34+
35+
baseUrl: 'http://localhost:8000',
36+
};

spec/login/viaConfigSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
describe('pages with login', function() {
2+
var ptor;
3+
4+
beforeEach(function() {
5+
ptor = protractor.getInstance();
6+
})
7+
8+
it('should log in with a non-Angular page', function() {
9+
ptor.get('http://localhost:8000/app/index.html');
10+
11+
var angularElement = ptor.findElement(protractor.By.input('url'));
12+
expect(angularElement.getAttribute('value')).toEqual('/fastcall');
13+
14+
// Make sure the cookie is still set.
15+
ptor.manage().getCookie('testcookie').then(function(cookie) {
16+
expect(cookie.value).toEqual('Jane-1234');
17+
});
18+
});
19+
});

spec/login/viaTestConf.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This is the configuration file showing how a suite of tests might
2+
// handle log-in using the onPrepare field.
3+
exports.config = {
4+
seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar',
5+
chromeDriver: './selenium/chromedriver',
6+
7+
seleniumAddress: 'http://localhost:4444/wd/hub',
8+
9+
specs: [
10+
'viaTestSpec.js'
11+
],
12+
13+
capabilities: {
14+
'browserName': 'chrome'
15+
},
16+
17+
baseUrl: 'http://localhost:8000',
18+
};

spec/login/viaTestSpec.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
describe('pages with login', function() {
2+
var ptor;
3+
4+
beforeEach(function() {
5+
ptor = protractor.getInstance();
6+
})
7+
8+
it('should log in with a non-Angular page', function() {
9+
ptor.driver.get('http://localhost:8000/app/login.html');
10+
11+
ptor.driver.findElement(protractor.By.id('username')).sendKeys('Jane');
12+
ptor.driver.findElement(protractor.By.id('password')).sendKeys('1234');
13+
ptor.driver.findElement(protractor.By.id('clickme')).click();
14+
15+
// Login takes some time, so wait until it's done.
16+
// For the test app's login, we know it's done when it redirects to
17+
// index.html.
18+
ptor.wait(function() {
19+
return ptor.driver.getCurrentUrl().then(function(url) {
20+
return /index/.test(url);
21+
});
22+
});
23+
24+
// The login should have set a cookie. Make sure it's there.
25+
ptor.manage().getCookie('testcookie').then(function(cookie) {
26+
expect(cookie.value).toEqual('Jane-1234');
27+
});
28+
29+
30+
ptor.get('http://localhost:8000/app/index.html');
31+
32+
var angularElement = ptor.findElement(protractor.By.input('url'));
33+
expect(angularElement.getAttribute('value')).toEqual('/fastcall');
34+
35+
// Make sure the cookie is still set.
36+
ptor.manage().getCookie('testcookie').then(function(cookie) {
37+
expect(cookie.value).toEqual('Jane-1234');
38+
});
39+
});
40+
});

spec/onPrepare/startup.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var ptor = protractor.getInstance();
2+
ptor.elem = ptor.findElement;
3+
ptor.elems = ptor.findElements;
4+
global.by = protractor.By;
5+
global.ptor = ptor;

spec/onPrepareStringConf.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Configuration using a string in onPrepare to load a file with code to
2+
// execute once before tests.
3+
exports.config = {
4+
seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar',
5+
chromeDriver: './selenium/chromedriver',
6+
7+
seleniumAddress: 'http://localhost:4444/wd/hub',
8+
9+
// Spec patterns are relative to this directory.
10+
specs: [
11+
'onPrepare/*_spec.js'
12+
],
13+
14+
capabilities: {
15+
'browserName': 'chrome'
16+
},
17+
18+
baseUrl: 'http://localhost:8000',
19+
20+
onPrepare: 'onPrepare/startup.js'
21+
};

testapp/app/login.html

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<html>
2+
<head>
3+
<title>Test Application Login</title>
4+
<script>
5+
function login() {
6+
// Make sure everything works when the login is slow.
7+
window.setTimeout(function() {
8+
// Set a simple cookie.
9+
var username = document.getElementById('username').value;
10+
var password = document.getElementById('password').value;
11+
12+
document.cookie = 'testcookie=' + username + '-' + password;
13+
14+
window.location.assign('index.html');
15+
}, 2000);
16+
};
17+
</script>
18+
</head>
19+
<body>
20+
<div>Login</div>
21+
<div><label>Username</label><input id="username" type="text"/></div>
22+
<div><label>Password</label><input id="password" type="text"/></div>
23+
<div><button id="clickme" onClick="login()">Go</button></div>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)