|
3 | 3 |
|
4 | 4 | describe('cypress-xpath', () => { |
5 | 5 | it('adds xpath command', () => { |
6 | | - expect(cy) |
7 | | - .property('xpath') |
8 | | - .to.be.a('function') |
9 | | - }) |
| 6 | + expect(cy).property('xpath').to.be.a('function'); |
| 7 | + }); |
10 | 8 |
|
11 | 9 | context('elements', () => { |
12 | 10 | beforeEach(() => { |
13 | | - cy.visit('cypress/integration/index.html') |
14 | | - }) |
| 11 | + cy.visit('cypress/e2e/index.html'); |
| 12 | + }); |
15 | 13 |
|
16 | 14 | it('finds h1', () => { |
17 | | - cy.xpath('//h1').should('have.length', 1) |
18 | | - }) |
| 15 | + cy.xpath('//h1').should('have.length', 1); |
| 16 | + }); |
19 | 17 |
|
20 | 18 | it('returns jQuery wrapped elements', () => { |
21 | | - cy.xpath('//h1').then(el$ => { |
22 | | - expect(el$).to.have.property('jquery') |
23 | | - }) |
24 | | - }) |
| 19 | + cy.xpath('//h1').then((el$) => { |
| 20 | + expect(el$).to.have.property('jquery'); |
| 21 | + }); |
| 22 | + }); |
25 | 23 |
|
26 | 24 | it('returns primitives as is', () => { |
27 | | - cy.xpath('string(//h1)').then(el$ => { |
28 | | - expect(el$).to.not.have.property('jquery') |
29 | | - }) |
30 | | - }) |
| 25 | + cy.xpath('string(//h1)').then((el$) => { |
| 26 | + expect(el$).to.not.have.property('jquery'); |
| 27 | + }); |
| 28 | + }); |
31 | 29 |
|
32 | 30 | it('provides jQuery wrapped elements to assertions', () => { |
33 | | - cy.xpath('//h1').should(el$ => { |
34 | | - expect(el$).to.have.property('jquery') |
35 | | - }) |
36 | | - }) |
| 31 | + cy.xpath('//h1').should((el$) => { |
| 32 | + expect(el$).to.have.property('jquery'); |
| 33 | + }); |
| 34 | + }); |
37 | 35 |
|
38 | 36 | it('gets h1 text', () => { |
39 | 37 | cy.xpath('//h1/text()') |
40 | 38 | .its('0.textContent') |
41 | | - .should('equal', 'cypress-xpath') |
42 | | - }) |
| 39 | + .should('equal', 'cypress-xpath'); |
| 40 | + }); |
43 | 41 |
|
44 | 42 | it('retries until element is inserted', () => { |
45 | 43 | // the element will be inserted after 1 second |
46 | | - cy.xpath('string(//*[@id="inserted"])').should('equal', 'inserted text') |
47 | | - }) |
| 44 | + cy.xpath('string(//*[@id="inserted"])').should('equal', 'inserted text'); |
| 45 | + }); |
48 | 46 |
|
49 | 47 | describe('chaining', () => { |
50 | 48 | it('finds h1 within main', () => { |
51 | 49 | // first assert that h1 doesn't exist as a child of the implicit document subject |
52 | | - cy.xpath('./h1').should('not.exist') |
| 50 | + cy.xpath('./h1').should('not.exist'); |
53 | 51 |
|
54 | | - cy.xpath('//main').xpath('./h1').should('exist') |
55 | | - }) |
| 52 | + cy.xpath('//main').xpath('./h1').should('exist'); |
| 53 | + }); |
56 | 54 |
|
57 | 55 | it('finds body outside of main when succumbing to // trap', () => { |
58 | 56 | // first assert that body doesn't actually exist within main |
59 | | - cy.xpath('//main').xpath('.//body').should('not.exist') |
| 57 | + cy.xpath('//main').xpath('.//body').should('not.exist'); |
60 | 58 |
|
61 | | - cy.xpath('//main').xpath('//body').should('exist') |
62 | | - }) |
| 59 | + cy.xpath('//main').xpath('//body').should('exist'); |
| 60 | + }); |
63 | 61 |
|
64 | 62 | it('finds h1 within document', () => { |
65 | | - cy.document().xpath('//h1').should('exist') |
66 | | - }) |
| 63 | + cy.document().xpath('//h1').should('exist'); |
| 64 | + }); |
67 | 65 |
|
68 | 66 | it('throws when subject is more than a single element', (done) => { |
69 | 67 | cy.on('fail', (err) => { |
70 | | - expect(err.message).to.eq('xpath() can only be called on a single element. Your subject contained 2 elements.') |
71 | | - done() |
72 | | - }) |
| 68 | + expect(err.message).to.eq( |
| 69 | + 'xpath() can only be called on a single element. Your subject contained 2 elements.' |
| 70 | + ); |
| 71 | + done(); |
| 72 | + }); |
73 | 73 |
|
74 | | - cy.get('main, div').xpath('foo') |
75 | | - }) |
76 | | - }) |
| 74 | + cy.get('main, div').xpath('foo'); |
| 75 | + }); |
| 76 | + }); |
77 | 77 |
|
78 | 78 | describe('within()', () => { |
79 | 79 | it('finds h1 within within-subject', () => { |
80 | 80 | // first assert that h1 doesn't exist as a child of the implicit document subject |
81 | | - cy.xpath('./h1').should('not.exist') |
| 81 | + cy.xpath('./h1').should('not.exist'); |
82 | 82 |
|
83 | 83 | cy.xpath('//main').within(() => { |
84 | | - cy.xpath('./h1').should('exist') |
85 | | - }) |
86 | | - }) |
| 84 | + cy.xpath('./h1').should('exist'); |
| 85 | + }); |
| 86 | + }); |
87 | 87 |
|
88 | 88 | it('finds body outside of within-subject when succumbing to // trap', () => { |
89 | 89 | // first assert that body doesn't actually exist within main |
90 | 90 | cy.xpath('//main').within(() => { |
91 | | - cy.xpath('.//body').should('not.exist') |
| 91 | + cy.xpath('.//body').should('not.exist'); |
92 | 92 | }); |
93 | 93 |
|
94 | 94 | cy.xpath('//main').within(() => { |
95 | | - cy.xpath('//body').should('exist') |
| 95 | + cy.xpath('//body').should('exist'); |
96 | 96 | }); |
97 | | - }) |
98 | | - }) |
| 97 | + }); |
| 98 | + }); |
99 | 99 |
|
100 | 100 | describe('primitives', () => { |
101 | 101 | it('counts h1 elements', () => { |
102 | | - cy.xpath('count(//h1)').should('equal', 1) |
103 | | - }) |
| 102 | + cy.xpath('count(//h1)').should('equal', 1); |
| 103 | + }); |
104 | 104 |
|
105 | 105 | it('returns h1 text content', () => { |
106 | | - cy.xpath('string(//h1)').should('equal', 'cypress-xpath') |
107 | | - }) |
| 106 | + cy.xpath('string(//h1)').should('equal', 'cypress-xpath'); |
| 107 | + }); |
108 | 108 |
|
109 | 109 | it('returns boolean', () => { |
110 | | - cy.xpath('boolean(//h1)').should('be.true') |
111 | | - cy.xpath('boolean(//h2)').should('be.false') |
112 | | - }) |
113 | | - }) |
| 110 | + cy.xpath('boolean(//h1)').should('be.true'); |
| 111 | + cy.xpath('boolean(//h2)').should('be.false'); |
| 112 | + }); |
| 113 | + }); |
114 | 114 |
|
115 | 115 | describe('typing', () => { |
116 | 116 | it('works on text input', () => { |
117 | | - cy.xpath('//*[@id="name"]').type('World') |
118 | | - cy.contains('span#greeting', 'Hello, World') |
119 | | - }) |
120 | | - }) |
| 117 | + cy.xpath('//*[@id="name"]').type('World'); |
| 118 | + cy.contains('span#greeting', 'Hello, World'); |
| 119 | + }); |
| 120 | + }); |
121 | 121 |
|
122 | 122 | describe('clicking', () => { |
123 | 123 | it('on button', () => { |
124 | 124 | // this button invokes window.alert when clicked |
125 | | - const alert = cy.stub() |
126 | | - cy.on('window:alert', alert) |
| 125 | + const alert = cy.stub(); |
| 126 | + cy.on('window:alert', alert); |
127 | 127 | cy.xpath('//*[@id="first-button"]') |
128 | 128 | .click() |
129 | 129 | .then(() => { |
130 | | - expect(alert).to.have.been.calledOnce |
131 | | - }) |
132 | | - }) |
133 | | - }) |
134 | | - }) |
| 130 | + expect(alert).to.have.been.calledOnce; |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
135 | 135 |
|
136 | 136 | context('logging', () => { |
137 | 137 | beforeEach(() => { |
138 | | - cy.visit('cypress/integration/index.html') |
139 | | - }) |
| 138 | + cy.visit('cypress/e2e/index.html'); |
| 139 | + }); |
140 | 140 |
|
141 | 141 | it('should log by default', () => { |
142 | | - cy.spy(Cypress, 'log').log(false) |
| 142 | + cy.spy(Cypress, 'log').log(false); |
143 | 143 |
|
144 | 144 | cy.xpath('//h1').then(() => { |
145 | | - expect(Cypress.log).to.be.calledWithMatch({ name: 'xpath' }) |
146 | | - }) |
147 | | - }) |
| 145 | + expect(Cypress.log).to.be.calledWithMatch({ name: 'xpath' }); |
| 146 | + }); |
| 147 | + }); |
148 | 148 |
|
149 | 149 | it('logs the selector when not found', (done) => { |
150 | | - cy.xpath('//h1') // does exist |
| 150 | + cy.xpath('//h1'); // does exist |
151 | 151 | cy.on('fail', (e) => { |
152 | 152 | const isExpectedErrorMessage = (message) => |
153 | 153 | message.includes('Timed out retrying') && |
154 | | - message.includes('Expected to find element: `//h2`, but never found it.') |
| 154 | + message.includes( |
| 155 | + 'Expected to find element: `//h2`, but never found it.' |
| 156 | + ); |
155 | 157 |
|
156 | 158 | if (!isExpectedErrorMessage(e.message)) { |
157 | | - console.error('Cypress test failed with an unexpected error message') |
158 | | - console.error(e) |
159 | | - return done(e) |
| 159 | + console.error('Cypress test failed with an unexpected error message'); |
| 160 | + console.error(e); |
| 161 | + return done(e); |
160 | 162 | } |
161 | 163 | // no errors, the error message for not found selector is correct |
162 | | - done() |
163 | | - }) |
164 | | - cy.xpath('//h2', { timeout: 100 }) // does not exist |
165 | | - }) |
| 164 | + done(); |
| 165 | + }); |
| 166 | + cy.xpath('//h2', { timeout: 100 }); // does not exist |
| 167 | + }); |
166 | 168 |
|
167 | 169 | it('should not log when provided log: false', () => { |
168 | | - cy.spy(Cypress, 'log').log(false) |
| 170 | + cy.spy(Cypress, 'log').log(false); |
169 | 171 |
|
170 | 172 | cy.xpath('//h1', { log: false }).then(() => { |
171 | | - expect(Cypress.log).to.not.be.calledWithMatch({ name: 'xpath' }) |
172 | | - }) |
173 | | - }) |
174 | | - }) |
175 | | -}) |
| 173 | + expect(Cypress.log).to.not.be.calledWithMatch({ name: 'xpath' }); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments