Last active
December 11, 2020 09:43
-
-
Save karfau/4f7d4871def9881ca263f892f3285fac to your computer and use it in GitHub Desktop.
some cypress example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("Basic flow control in Cypress", () => { | |
before(() => { | |
cy.window().then(window => { | |
window.document.write(` | |
<ul> | |
<li> | |
<a>1a</a> | |
<a>1b</a> | |
</li> | |
<li> | |
<a>2</a> | |
</li> | |
<li> | |
<a>3</a> | |
</li> | |
</ul> | |
`); | |
}); | |
}); | |
it('should use cypress concepts', () => { | |
/** Let’s say I need to find the `li` element */ | |
cy.get('li') | |
/** that only has a single `a` as its child */ | |
.filter((_, li) => li.querySelectorAll('a').length === 1) | |
/** and I'm only interested in the first of those (`li`s) */ | |
.first() | |
/** and on the `a` element inside it */ | |
.find('a') | |
/** I want to execute an assertion. */ | |
.should('have.html', '2') | |
// can also be written using `expect`, | |
// especially if you need to assert something very custom (which could also be extracted into a method) | |
.should((a) => { | |
expect(a.html()).to.equal('2'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how the test looks in my IDE:
And in
npx cypress open