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