11import { browser } from 'protractor' ;
2+ import { HarnessLoader } from '../component-harness' ;
23import { ProtractorHarnessEnvironment } from '../protractor' ;
3- import { MainComponentHarness , WrongComponentHarness } from './harnesses/main-component-harness' ;
4-
5- describe ( 'Protractor Helper Test' , ( ) => {
6- let harness : MainComponentHarness ;
4+ import { MainComponentHarness } from './harnesses/main-component-harness' ;
5+ import { SubComponentHarness } from './harnesses/sub-component-harness' ;
76
7+ describe ( 'ProtractorHarnessEnvironment' , ( ) => {
88 beforeEach ( async ( ) => {
99 await browser . get ( '/component-harness' ) ;
10- harness = await ProtractorHarnessEnvironment . create ( ) . requiredHarness ( MainComponentHarness ) ;
1110 } ) ;
1211
13- describe ( 'Locator' , ( ) => {
14- it ( 'should be able to locate a element based on CSS selector' , async ( ) => {
12+ describe ( 'HarnessLoader' , ( ) => {
13+ let loader : HarnessLoader ;
14+
15+ beforeEach ( async ( ) => {
16+ loader = ProtractorHarnessEnvironment . create ( ) ;
17+ } ) ;
18+
19+ it ( 'should create HarnessLoader' , async ( ) => {
20+ expect ( loader ) . not . toBeNull ( ) ;
21+ } ) ;
22+
23+ it ( 'should find required HarnessLoader for child element' , async ( ) => {
24+ const subcomponentsLoader = await loader . findRequired ( '.subcomponents' ) ;
25+ expect ( subcomponentsLoader ) . not . toBeNull ( ) ;
26+ } ) ;
27+
28+ it ( 'should error after failing to find required HarnessLoader for child element' , async ( ) => {
29+ try {
30+ await loader . findRequired ( 'error' ) ;
31+ fail ( 'Expected to throw' ) ;
32+ } catch ( e ) {
33+ expect ( e . message )
34+ . toBe ( 'Expected to find element matching selector: "error", but none was found' ) ;
35+ }
36+ } ) ;
37+
38+ it ( 'should find optional HarnessLoader for child element' , async ( ) => {
39+ const subcomponentsLoader = await loader . findOptional ( '.subcomponents' ) ;
40+ const nullLoader = await loader . findOptional ( 'wrong-selector' ) ;
41+ expect ( subcomponentsLoader ) . not . toBeNull ( ) ;
42+ expect ( nullLoader ) . toBeNull ( ) ;
43+ } ) ;
44+
45+ it ( 'should find all HarnessLoaders for child elements' , async ( ) => {
46+ const loaders = await loader . findAll ( '.subcomponents,.counters' ) ;
47+ expect ( loaders . length ) . toBe ( 2 ) ;
48+ } ) ;
49+
50+ it ( 'should get first matching component for required harness' , async ( ) => {
51+ const harness = await loader . requiredHarness ( SubComponentHarness ) ;
52+ expect ( harness ) . not . toBeNull ( ) ;
53+ expect ( await ( await harness . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
54+ } ) ;
55+
56+ it ( 'should throw if no matching component found for required harness' , async ( ) => {
57+ const countersLoader = await loader . findRequired ( '.counters' ) ;
58+ try {
59+ await countersLoader . requiredHarness ( SubComponentHarness ) ;
60+ fail ( 'Expected to throw' ) ;
61+ } catch ( e ) {
62+ expect ( e . message )
63+ . toBe ( 'Expected to find element matching selector: "test-sub", but none was found' ) ;
64+ }
65+ } ) ;
66+
67+ it ( 'should get first matching component for optional harness' , async ( ) => {
68+ const countersLoader = await loader . findRequired ( '.counters' ) ;
69+ const harness1 = await loader . optionalHarness ( SubComponentHarness ) ;
70+ const harness2 = await countersLoader . optionalHarness ( SubComponentHarness ) ;
71+ expect ( harness1 ) . not . toBeNull ( ) ;
72+ expect ( await ( await harness1 ! . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
73+ expect ( harness2 ) . toBeNull ( ) ;
74+ } ) ;
75+
76+ it ( 'should get all matching components for all harnesses' , async ( ) => {
77+ const harnesses = await loader . allHarnesses ( SubComponentHarness ) ;
78+ expect ( harnesses . length ) . toBe ( 2 ) ;
79+ } ) ;
80+ } ) ;
81+
82+ describe ( 'ComponentHarness' , ( ) => {
83+ let harness : MainComponentHarness ;
84+
85+ beforeEach ( async ( ) => {
86+ harness = await ProtractorHarnessEnvironment . create ( ) . requiredHarness ( MainComponentHarness ) ;
87+ } ) ;
88+
89+ it ( 'should locate a required element based on CSS selector' , async ( ) => {
1590 const title = await harness . title ( ) ;
1691 expect ( await title . text ( ) ) . toBe ( 'Main Component' ) ;
1792 } ) ;
1893
19- it ( 'should be able to locate all elements based on CSS selector' ,
20- async ( ) => {
21- const labels = await harness . allLabels ( ) ;
22- expect ( labels . length ) . toBe ( 2 ) ;
23- expect ( await labels [ 0 ] . text ( ) ) . toBe ( 'Count:' ) ;
24- expect ( await labels [ 1 ] . text ( ) ) . toBe ( 'AsyncCounter:' ) ;
25- } ) ;
94+ it ( 'should throw when failing to locate a required element based on CSS selector' , async ( ) => {
95+ try {
96+ await harness . errorItem ( ) ;
97+ fail ( 'Expected to throw' ) ;
98+ } catch ( e ) {
99+ expect ( e . message ) . toBe (
100+ 'Expected to find element matching selector: "wrong locator", but none was found' ) ;
101+ }
102+ } ) ;
103+
104+ it ( 'should locate an optional element based on CSS selector' , async ( ) => {
105+ const present = await harness . optionalDiv ( ) ;
106+ const missing = await harness . nullItem ( ) ;
107+ expect ( present ) . not . toBeNull ( ) ;
108+ expect ( await present ! . text ( ) ) . toBe ( 'Hello Yi from Angular 2!' ) ;
109+ expect ( missing ) . toBeNull ( ) ;
110+ } ) ;
111+
112+ it ( 'should locate all elements based on CSS selector' , async ( ) => {
113+ const labels = await harness . allLabels ( ) ;
114+ expect ( labels . length ) . toBe ( 2 ) ;
115+ expect ( await labels [ 0 ] . text ( ) ) . toBe ( 'Count:' ) ;
116+ expect ( await labels [ 1 ] . text ( ) ) . toBe ( 'AsyncCounter:' ) ;
117+ } ) ;
26118
27- it ( 'should be able to locate the sub harnesses' , async ( ) => {
119+ it ( 'should locate required sub harnesses' , async ( ) => {
28120 const items = await harness . getTestTools ( ) ;
29121 expect ( items . length ) . toBe ( 3 ) ;
30122 expect ( await items [ 0 ] . text ( ) ) . toBe ( 'Protractor' ) ;
31123 expect ( await items [ 1 ] . text ( ) ) . toBe ( 'TestBed' ) ;
32124 expect ( await items [ 2 ] . text ( ) ) . toBe ( 'Other' ) ;
33125 } ) ;
34126
35- it ( 'should be able to locate all sub harnesses' , async ( ) => {
127+ it ( 'should throw when failing to locate required sub harnesses' , async ( ) => {
128+ try {
129+ await harness . errorSubComponent ( ) ;
130+ fail ( 'Expected to throw' ) ;
131+ } catch ( e ) {
132+ expect ( e . message ) . toBe (
133+ 'Expected to find element matching selector: "wrong-selector", but none was found' ) ;
134+ }
135+ } ) ;
136+
137+ it ( 'should locate optional sub harnesses' , async ( ) => {
138+ const present = await harness . optionalSubComponent ( ) ;
139+ const missing = await harness . nullComponentHarness ( ) ;
140+ expect ( present ) . not . toBeNull ( ) ;
141+ expect ( await ( await present ! . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
142+ expect ( missing ) . toBeNull ( ) ;
143+ } ) ;
144+
145+ it ( 'should locate all sub harnesses' , async ( ) => {
36146 const alllists = await harness . allLists ( ) ;
37147 const items1 = await alllists [ 0 ] . getItems ( ) ;
38148 const items2 = await alllists [ 1 ] . getItems ( ) ;
@@ -46,9 +156,27 @@ describe('Protractor Helper Test', () => {
46156 expect ( await items2 [ 1 ] . text ( ) ) . toBe ( 'Integration Test' ) ;
47157 expect ( await items2 [ 2 ] . text ( ) ) . toBe ( 'Performance Test' ) ;
48158 } ) ;
159+
160+ it ( 'should wait for async operation to complete' , async ( ) => {
161+ const asyncCounter = await harness . asyncCounter ( ) ;
162+ expect ( await asyncCounter . text ( ) ) . toBe ( '5' ) ;
163+ await harness . increaseCounter ( 3 ) ;
164+ expect ( await asyncCounter . text ( ) ) . toBe ( '8' ) ;
165+ } ) ;
166+
167+ it ( 'can get elements outside of host' , async ( ) => {
168+ const globalEl = await harness . globalEl ( ) ;
169+ expect ( await globalEl . text ( ) ) . toBe ( 'I am a sibling!' ) ;
170+ } ) ;
49171 } ) ;
50172
51- describe ( 'Test element' , ( ) => {
173+ describe ( 'TestElement' , ( ) => {
174+ let harness : MainComponentHarness ;
175+
176+ beforeEach ( async ( ) => {
177+ harness = await ProtractorHarnessEnvironment . create ( ) . requiredHarness ( MainComponentHarness ) ;
178+ } ) ;
179+
52180 it ( 'should be able to clear' , async ( ) => {
53181 const input = await harness . input ( ) ;
54182 await input . sendKeys ( 'Yi' ) ;
@@ -78,8 +206,7 @@ describe('Protractor Helper Test', () => {
78206 const input = await harness . input ( ) ;
79207 await input . sendKeys ( 'Yi' ) ;
80208 expect ( await input . getAttribute ( 'id' ) )
81- . toBe ( await browser . driver . switchTo ( ) . activeElement ( ) . getAttribute (
82- 'id' ) ) ;
209+ . toBe ( await browser . driver . switchTo ( ) . activeElement ( ) . getAttribute ( 'id' ) ) ;
83210 } ) ;
84211
85212 it ( 'should be able to hover' , async ( ) => {
@@ -106,68 +233,4 @@ describe('Protractor Helper Test', () => {
106233 expect ( await title . getCssValue ( 'height' ) ) . toBe ( '50px' ) ;
107234 } ) ;
108235 } ) ;
109-
110- describe ( 'Async operation' , ( ) => {
111- it ( 'should wait for async opeartion to complete' , async ( ) => {
112- const asyncCounter = await harness . asyncCounter ( ) ;
113- expect ( await asyncCounter . text ( ) ) . toBe ( '5' ) ;
114- await harness . increaseCounter ( 3 ) ;
115- expect ( await asyncCounter . text ( ) ) . toBe ( '8' ) ;
116- } ) ;
117- } ) ;
118-
119- describe ( 'Allow null' , ( ) => {
120- it ( 'should allow element to be null when setting allowNull' , async ( ) => {
121- expect ( await harness . nullItem ( ) ) . toBe ( null ) ;
122- } ) ;
123-
124- it ( 'should allow main harness to be null when setting using optionalHarness' ,
125- async ( ) => {
126- const nullMainHarness =
127- await ProtractorHarnessEnvironment . create ( ) . optionalHarness ( WrongComponentHarness ) ;
128- expect ( nullMainHarness ) . toBe ( null ) ;
129- } ) ;
130-
131- it ( 'should allow sub-harness to be null when setting allowNull' ,
132- async ( ) => {
133- expect ( await harness . nullComponentHarness ( ) ) . toBe ( null ) ;
134- } ) ;
135- } ) ;
136-
137- describe ( 'with the global option' , ( ) => {
138- it ( 'should find an element outside the root of the harness' , async ( ) => {
139- const globalEl = await harness . globalEl ( ) ;
140- expect ( await globalEl . text ( ) ) . toBe ( 'I am a sibling!' ) ;
141- } ) ;
142-
143- it ( 'should return null for a selector that does not exist' , async ( ) => {
144- expect ( await harness . nullGlobalEl ( ) ) . toBeNull ( ) ;
145- } ) ;
146-
147- it ( 'should throw an error for a selctor that does not exist ' +
148- 'with allowNull = false' ,
149- async ( ) => {
150- try {
151- await harness . errorGlobalEl ( ) ;
152- fail ( 'Should throw error' ) ;
153- } catch ( err ) {
154- expect ( err . message )
155- . toBe (
156- 'Expected to find element matching selector: "wrong locator", but none was found' ) ;
157- }
158- } ) ;
159- } ) ;
160-
161- describe ( 'Throw error' , ( ) => {
162- it ( 'should show the correct error' , async ( ) => {
163- try {
164- await harness . errorItem ( ) ;
165- fail ( 'Should throw error' ) ;
166- } catch ( err ) {
167- expect ( err . message )
168- . toBe (
169- 'Expected to find element matching selector: "wrong locator", but none was found' ) ;
170- }
171- } ) ;
172- } ) ;
173236} ) ;
0 commit comments