This repository was archived by the owner on Jul 29, 2024. It is now read-only.
File tree 10 files changed +194
-6
lines changed
10 files changed +194
-6
lines changed Original file line number Diff line number Diff line change @@ -8,3 +8,16 @@ Protractor attempts to wait until the page is completely loaded before
8
8
performing any action (such as finding an element or sending a command to
9
9
an element). If your application continuously polls $timeout or $http, it will
10
10
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 ) .
Original file line number Diff line number Diff line change @@ -145,8 +145,9 @@ var startJasmineTests = function() {
145
145
usingServer ( config . seleniumAddress ) .
146
146
withCapabilities ( config . capabilities ) . build ( ) ;
147
147
148
- driver . manage ( ) . timeouts ( ) . setScriptTimeout ( 100000 ) ;
149
148
driver . getSession ( ) . then ( function ( session ) {
149
+ driver . manage ( ) . timeouts ( ) . setScriptTimeout ( 100000 ) ;
150
+
150
151
id = session . getId ( ) ;
151
152
152
153
protractor . setInstance ( protractor . wrapDriver ( driver , config . baseUrl , config . rootElement ) ) ;
@@ -163,11 +164,19 @@ var startJasmineTests = function() {
163
164
164
165
// Let the configuration configure the protractor instance before running
165
166
// 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
+ } ) ;
171
180
} ) ;
172
181
}
173
182
Original file line number Diff line number Diff line change @@ -62,6 +62,8 @@ exports.config = {
62
62
63
63
// A callback function called once protractor is ready and available, and
64
64
// before the specs are executed
65
+ // You can specify a file containing code to run by setting onPrepare to
66
+ // the filename string.
65
67
onPrepare : function ( ) {
66
68
// At this point, global 'protractor' object will be set up, and jasmine
67
69
// will be available. For example, you can add a Jasmine reporter with:
Original file line number Diff line number Diff line change
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 / i n d e x / . test ( url ) ;
31
+ } ) ;
32
+ } ) ;
33
+ } ,
34
+
35
+ baseUrl : 'http://localhost:8000' ,
36
+ } ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 / i n d e x / . 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
+ } ) ;
Original file line number Diff line number Diff line change
1
+ var ptor = protractor . getInstance ( ) ;
2
+ ptor . elem = ptor . findElement ;
3
+ ptor . elems = ptor . findElements ;
4
+ global . by = protractor . By ;
5
+ global . ptor = ptor ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments