Skip to content

Instantly share code, notes, and snippets.

@karfau
Last active December 11, 2020 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karfau/4f7d4871def9881ca263f892f3285fac to your computer and use it in GitHub Desktop.
Save karfau/4f7d4871def9881ca263f892f3285fac to your computer and use it in GitHub Desktop.
some cypress example
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');
});
});
});
@karfau
Copy link
Author

karfau commented Dec 11, 2020

This is how the test looks in my IDE:
image

And in npx cypress open
cypress-functional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment