Command | Description |
---|---|
cypress open | Opens the Cypress app |
cypress run | Runs Cypress tests (headlessly) |
cypress info | Prints information about Cypress environment |
cypress verify | Checks Cypress installation |
cypress version | Prints version info |
# Create new folder, navigate to it
npm init
npm install -D cypress
# Add npm script for `cypress open` and `cypress run`
# Start cypress
# Select E2e -> configures your cy environment
# Generate new, empty spec for Chrome
describe('My First Test', () => {
it('Does not do much!', () => {
expect(true).to.equal(true)
})
})
# Run test
# Change test so that it fails
# Rerun test
describe('My First Test', () => {
it('Gets, types and asserts', () => {
cy.visit('https://example.cypress.io')
cy.contains('type').click()
// Should be on a new URL which
// includes '/commands/actions'
cy.url().should('include', '/commands/actions')
// Get an input, type into it and verify
// that the value has been updated
cy.get('#email1')
.type('fake@email.com')
.should('have.value', 'fake@email.com')
})
})
# Run test
# Change test so that it fails
# Rerun test
# Install typescript
npm install -D typescript
# Add tsconfig.json in `cypress` folder
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
# Rename spec.cy.js to .ts
# Reopen cypress to see TypeScript test running
$
<!-- HTML -->
<button id="main" class="btn btn-large" name="submission"
role="button" data-testid="submit">
Submit
</button>
// Cypress
cy.get('[data-testid="submit"]').click();
<!-- app.component.html -->
<hello name="{{ name }}"></hello>
<p>Start editing to see some magic happen :)</p>
<button data-test-cy="main-button">Do something</button>
it('Querying', () => {
cy.visit('https://angular-ivy-saxmjf.stackblitz.io')
cy.get('#promptToRun', { timeout: 600000 }).find('button').click()
cy.get('[data-test-cy="main-button"]')
.should('have.text', 'Do something')
})
export {};
declare global {
namespace Cypress {
interface Chainable {
skipStackblitzProtection(): Chainable<Element>;
}
}
}
Cypress.Commands.add('skipStackblitzProtection', () => {
cy.get('#promptToRun', { timeout: 600000 }).find('button').click();
});
import "./commands/skipStackblitzProtection";
it('Querying', () => {
cy.visit('https://angular-ivy-saxmjf.stackblitz.io')
cy.skipStackblitzProtection()
cy.get('[data-test-cy="main-button"]')
.should('have.text', 'Do something')
})
it('Calculates correctly', () => {
cy.visit('https://angular-basic-calculator.stackblitz.io/')
cy.get('.button-1').as('1').click()
cy.get('.button-0').click()
cy.get('.button-a').click()
cy.get('@1').click()
cy.get('.equals').click()
cy.get('.display').should('have.text', 10 + 1)
})
Folder | Description |
---|---|
e2e | Test files |
fixtures | Static data used by tests |
support | Support files running before each spec |