1
1
// Based off of spec/basic/elements_spec.js
2
- import { promise as ppromise , browser , element , by , By , $ , $$ , ExpectedConditions , ElementFinder } from '../../..' ;
2
+ import * as q from 'q' ;
3
+
4
+ import { $ , $$ , browser , by , By , element , ElementArrayFinder , ElementFinder , ExpectedConditions , promise as ppromise , WebElement } from '../../..' ;
5
+
6
+ describe ( 'verify control flow is off' , function ( ) {
7
+ it ( 'should have set webdriver.promise.USE_PROMISE_MANAGER' , ( ) => {
8
+ expect ( ( ppromise as any ) . USE_PROMISE_MANAGER ) . toBe ( false ) ;
9
+ } ) ;
10
+
11
+ it ( 'should not wait on one command before starting another' , async function ( ) {
12
+ // Wait forever
13
+ browser . controlFlow ( ) . wait (
14
+ ( browser . controlFlow ( ) as any ) . promise ( ( ) : void => undefined ) as ppromise . Promise < void > ) ;
15
+
16
+ // then return
17
+ await browser . controlFlow ( ) . execute ( ( ) => ppromise . when ( null ) ) ;
18
+ } ) ;
19
+ } ) ;
3
20
4
21
describe ( 'ElementFinder' , function ( ) {
5
22
it ( 'should return the same result as browser.findElement' , async function ( ) {
6
23
await browser . get ( 'index.html#/form' ) ;
7
24
const nameByElement = element ( by . binding ( 'username' ) ) ;
8
25
9
- await expect ( nameByElement . getText ( ) ) . toEqual (
10
- browser . findElement ( by . binding ( 'username' ) ) . getText ( ) ) ;
26
+ await expect ( nameByElement . getText ( ) )
27
+ . toEqual ( browser . findElement ( by . binding ( 'username' ) ) . getText ( ) ) ;
11
28
} ) ;
12
29
13
30
it ( 'should wait to grab the WebElement until a method is called' , async function ( ) {
@@ -32,38 +49,65 @@ describe('ElementFinder', function() {
32
49
33
50
await expect ( name . getText ( ) ) . toEqual ( 'Anon' ) ;
34
51
35
- await ( ( usernameInput . clear ( ) as any ) as ElementFinder ) . sendKeys ( 'Jane' ) ;
52
+ await ( ( usernameInput . clear ( ) as any ) as ElementFinder ) . sendKeys ( 'Jane' ) ;
36
53
await expect ( name . getText ( ) ) . toEqual ( 'Jane' ) ;
37
54
} ) ;
38
55
39
- it ( 'chained call should wait to grab the WebElement until a method is called' ,
40
- async function ( ) {
56
+ it ( 'should run chained element actions in sequence' , function ( done : any ) {
57
+ // Testing private methods is bad :(
58
+ let els = new ElementArrayFinder ( browser , ( ) => {
59
+ return ppromise . when ( [ null as WebElement ] ) ;
60
+ } ) ;
61
+ let applyAction_ : ( actionFn : ( value : WebElement , index : number , array : WebElement [ ] ) => any ) =>
62
+ ElementArrayFinder = ( ElementArrayFinder as any ) . prototype . applyAction_ ;
63
+ let order : string [ ] = [ ] ;
64
+
65
+ let deferredA = q . defer < void > ( ) ;
66
+ els = applyAction_ . call ( els , ( ) => {
67
+ return deferredA . promise . then ( ( ) => {
68
+ order . push ( 'a' ) ;
69
+ } ) ;
70
+ } ) ;
71
+ let deferredB = q . defer < void > ( ) ;
72
+ els = applyAction_ . call ( els , ( ) => {
73
+ return deferredB . promise . then ( ( ) => {
74
+ order . push ( 'b' ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+ deferredB . resolve ( ) ;
79
+ setTimeout ( async function ( ) {
80
+ deferredA . resolve ( ) ;
81
+ await els ;
82
+ expect ( order ) . toEqual ( [ 'a' , 'b' ] ) ;
83
+ done ( ) ;
84
+ } , 100 ) ;
85
+ } ) ;
86
+
87
+ it ( 'chained call should wait to grab the WebElement until a method is called' , async function ( ) {
41
88
// These should throw no error before a page is loaded.
42
- const reused = element ( by . id ( 'baz' ) ) .
43
- element ( by . binding ( 'item.reusedBinding' ) ) ;
89
+ const reused = element ( by . id ( 'baz' ) ) . element ( by . binding ( 'item.reusedBinding' ) ) ;
44
90
45
91
await browser . get ( 'index.html#/conflict' ) ;
46
92
47
93
await expect ( reused . getText ( ) ) . toEqual ( 'Inner: inner' ) ;
48
94
await expect ( reused . isPresent ( ) ) . toBe ( true ) ;
49
95
} ) ;
50
96
51
- it ( 'should differentiate elements with the same binding by chaining' ,
52
- async function ( ) {
53
- await browser . get ( 'index.html#/conflict' ) ;
97
+ it ( 'should differentiate elements with the same binding by chaining' , async function ( ) {
98
+ await browser . get ( 'index.html#/conflict' ) ;
54
99
55
- const outerReused = element ( by . binding ( 'item.reusedBinding' ) ) ;
56
- const innerReused =
57
- element ( by . id ( 'baz' ) ) . element ( by . binding ( 'item.reusedBinding' ) ) ;
100
+ const outerReused = element ( by . binding ( 'item.reusedBinding' ) ) ;
101
+ const innerReused = element ( by . id ( 'baz' ) ) . element ( by . binding ( 'item.reusedBinding' ) ) ;
58
102
59
- await expect ( outerReused . getText ( ) ) . toEqual ( 'Outer: outer' ) ;
60
- await expect ( innerReused . getText ( ) ) . toEqual ( 'Inner: inner' ) ;
61
- } ) ;
103
+ await expect ( outerReused . getText ( ) ) . toEqual ( 'Outer: outer' ) ;
104
+ await expect ( innerReused . getText ( ) ) . toEqual ( 'Inner: inner' ) ;
105
+ } ) ;
62
106
63
107
it ( 'should chain deeper than 2' , async function ( ) {
64
108
// These should throw no error before a page is loaded.
65
- const reused = element ( by . css ( 'body' ) ) . element ( by . id ( 'baz' ) ) .
66
- element ( by . binding ( 'item.reusedBinding' ) ) ;
109
+ const reused =
110
+ element ( by . css ( 'body' ) ) . element ( by . id ( 'baz' ) ) . element ( by . binding ( 'item.reusedBinding' ) ) ;
67
111
68
112
await browser . get ( 'index.html#/conflict' ) ;
69
113
@@ -108,11 +152,13 @@ describe('ElementFinder', function() {
108
152
await browser . get ( 'index.html#/form' ) ;
109
153
110
154
const invalidElement = element ( by . binding ( 'INVALID' ) ) ;
111
- const successful = invalidElement . getText ( ) . then ( function ( ) {
112
- return true ;
113
- } as any as ( ( ) => ppromise . Promise < void > ) , function ( ) {
114
- return false ;
115
- } as any as ( ( ) => ppromise . Promise < void > ) ) ;
155
+ const successful = invalidElement . getText ( ) . then (
156
+ function ( ) {
157
+ return true ;
158
+ } as any as ( ( ) => ppromise . Promise < void > ) ,
159
+ function ( ) {
160
+ return false ;
161
+ } as any as ( ( ) => ppromise . Promise < void > ) ) ;
116
162
await expect ( successful ) . toEqual ( false ) ;
117
163
} ) ;
118
164
@@ -139,9 +185,7 @@ describe('ElementFinder', function() {
139
185
const name = element ( by . binding ( 'username' ) ) ;
140
186
141
187
await expect ( name . getText ( ) ) . toEqual ( 'Anon' ) ;
142
- await expect (
143
- name . getText ( ) . then ( null , function ( ) { } )
144
- ) . toEqual ( 'Anon' ) ;
188
+ await expect ( name . getText ( ) . then ( null , function ( ) { } ) ) . toEqual ( 'Anon' ) ;
145
189
146
190
} ) ;
147
191
0 commit comments