49
loading...
This website collects cookies to deliver better user experience
npx ng add @cypress/schematic
and voilá, done. If you are on nx, which I recommend, Cypress is already pre-installed.cy.get("some-selector")
. Then we can run an action on it such as click()
or type("some text")
. A click on a button is cy.get('button').click()
. Isn't that easy?p.message
. It should show "Changes have been saved". We would assert it like that: cy.get('p.message').should('contain.text', 'Changes have been saved');
.describe("Home", () => {
it("should click the button", () => {
cy.visit("");
cy.get("button").click();
cy.get("div.message").should("contain.text", "You clicked me");
})
})
npx cypress open
or npm run cypress:open
to open Cypress. When you click on home.spec.ts
, the test runner opens in another window and immediately runs the test.npm run cypress:open
, we just execute npm run cypress:run
. This runs the test in the headless mode. it("should add a customer", () => {
cy.visit("");
cy.get("a").contains("Customers").click();
cy.get("a").contains("Add Customer").click();
})
cy.get("a")contains("Add Customer")
is continuing to look for a link with the text "Add Customer" for a maximum of 4 seconds. That is not true.data-test
with a unique identifier to my DOM elements. The markup for the two links would look like this:<a data-test="btn-customers" mat-raised-button routerLink="/customer">Customers</a>
<a [routerLink]="['.', 'new']" color="primary" data-test="btn-customers-add"
mat-raised-button
>Add Customer</a>
it("should click on add customers", () => {
cy.visit("");
cy.get("[data-test=btn-customers]").click();
cy.get("[data-test=btn-customers-add]").click();
})
cy.get
have an awaiting feature built in. This means they will keep trying multiple times until an action is doable or the element is found. That constant retrying happens asynchronously. You could read the test case like this:it('should click on add customers', () => {
cy.visit('')
.then(() => cy.get('[data-test=btn-customers]'))
.then((button) => button.click())
.then(() => cy.get('[data-test=btn-customers-add]'))
.then((button) => button.click());
});
it('should click on add customers', async () => {
await cy.visit('');
const button = await cy.get('[data-test=btn-customers]');
await button.click();
const button2 = await cy.get('[data-test=btn-customers-add]');
await button2.click();
});
it('should fail', () => {
let isSuccessful = false;
cy.visit('');
cy.get('button').click();
cy.get('div.message').then(() => {
isSuccessful = true;
});
if (!isSuccessful) {
throw new Error('something is not working');
}
});
then
methods.