Skip to content

Commit

Permalink
feat: add testcase: 05
Browse files Browse the repository at this point in the history
  • Loading branch information
nxhawk committed Aug 5, 2024
1 parent 997a157 commit 4d484af
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 1 deletion.
1 change: 0 additions & 1 deletion cypress/e2e/advanced/cookies.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ context('Cookies', () => {
expect(cookies[1]).to.have.property('value', 'value')
expect(cookies[1]).to.have.property('httpOnly', false)
expect(cookies[1]).to.have.property('secure', false)
expect(cookies[1]).to.have.property('domain', 'example.com')
expect(cookies[1]).to.have.property('path')
})
})
Expand Down
30 changes: 30 additions & 0 deletions cypress/e2e/advanced/location.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
context('Location', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/location')
})

it('cy.hash() - get the current URL hash', () => {
// https://on.cypress.io/hash
cy.hash().should('be.empty')
})

it('cy.location() - get window.location', () => {
// https://on.cypress.io/location
cy.location().should((location) => {
expect(location.hash).to.be.empty
expect(location.href).to.eq('https://example.cypress.io/commands/location')
expect(location.host).to.eq('example.cypress.io')
expect(location.hostname).to.eq('example.cypress.io')
expect(location.origin).to.eq('https://example.cypress.io')
expect(location.pathname).to.eq('/commands/location')
expect(location.port).to.eq('')
expect(location.protocol).to.eq('https:')
expect(location.search).to.be.empty
})
})

it('cy.url() - get the current URL', () => {
// https://on.cypress.io/url
cy.url().should('eq', 'https://example.cypress.io/commands/location')
})
});
32 changes: 32 additions & 0 deletions cypress/e2e/advanced/misc.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
context('Misc', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/misc')
})

context('Cypress.Screenshot', function () {
it('cy.screenshot() - take a screenshot', () => {
// https://on.cypress.io/screenshot
cy.screenshot('my-image')
})

it('Cypress.Screenshot.defaults() - change default config of screenshots', function () {
Cypress.Screenshot.defaults({
blackout: ['.foo'],
capture: 'viewport',
clip: { x: 0, y: 0, width: 200, height: 200 },
scale: false,
disableTimersAndAnimations: true,
screenshotOnRunFailure: true,
onBeforeScreenshot () { },
onAfterScreenshot () { },
})
})
})

it('cy.wrap() - wrap an object', () => {
// https://on.cypress.io/wrap
cy.wrap({ foo: 'bar' })
.should('have.property', 'foo')
.and('include', 'bar')
})
})
46 changes: 46 additions & 0 deletions cypress/e2e/advanced/navigation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
context('Navigation', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io')
cy.get('.navbar-nav').contains('Commands').click()
cy.get('.dropdown-menu').contains('Navigation').click()
})

it('cy.go() - go back or forward in the browser\'s history', () => {
cy.location('pathname').should('include', 'navigation')

cy.go('back')
cy.location('pathname').should('not.include', 'navigation')

cy.go('forward')
cy.location('pathname').should('include', 'navigation')

// clicking back
cy.go(-1)
cy.location('pathname').should('not.include', 'navigation')

// clicking forward
cy.go(1)
cy.location('pathname').should('include', 'navigation')
})


it('cy.reload() - reload the page', () => {
// https://on.cypress.io/reload
cy.reload()

// reload the page without using the cache
cy.reload(true)
})

it('cy.visit() - visit a remote url', () => {
cy.visit('https://example.cypress.io/commands/navigation', {
timeout: 50000, // increase total time for the visit to resolve
onBeforeLoad: function(contentWindow){
// contentWindow is the remote page's window object
},
onLoad: function(contentWindow){
// contentWindow is the remote page's window object
}
})
})
})
97 changes: 97 additions & 0 deletions cypress/e2e/advanced/querying.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
context('Querying', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/querying')
})

it('cy.get() - query DOM elements', () => {
cy.get('#query-btn').should('contain', 'Button')

cy.get('.query-btn').should('contain', 'Button')

cy.get('#querying .well>button:first').should('contain', 'Button')

// Use CSS selectors just like jQuery

cy.get('[data-test-id="test-example"]').should('have.class', 'example')

// 'cy.get()' yields jQuery object, you can get its attribute
// by invoking `.attr()` method
cy.get('[data-test-id="test-example"]')
.invoke('attr', 'data-test-id')
.should('equal', 'test-example')

// or you can get an element's CSS property
cy.get('[data-test-id="test-example"]')
.invoke('css', 'position')
.should('equal', 'static')

cy.get('[data-test-id="test-example"]')
.should('have.attr', 'data-test-id', 'test-example')
.and('have.css', 'position', 'static')
})

it('cy.contains() - query DOM elements with matching content', () => {
cy.get('.query-list').contains('banana').should('have.class', 'thrid')

// we can pass a regexp to `.contains()`
cy.get('.query-list')
.contains(/^b\w+/).should('have.class', 'third')

cy.get('.query-list')
.contains('apples').should('have.class', 'first')

// passing a selector to contains will
// yield the selector containing the text
cy.get('#querying')
.contains('ul', 'oranges')
.should('have.class', 'query-list')

cy.get('.query-button')
.contains('Save Form')
.should('have.class', 'btn')
})

it('.within() - query DOM elements within a specific element', () => {
// https://on.cypress.io/within
cy.get('.query-form').within(() => {
cy.get('input:first').should('have.attr', 'placeholder', 'Email')
cy.get('input:last').should('have.attr', 'placeholder', 'Password')
})
})

it('cy.root() - query the root DOM element', () => {
// https://on.cypress.io/root

// By default, root is the document
cy.root().should('match', 'html')

cy.get('.query-ul').within(() => {
// In this within, the root is now the ul DOM element
cy.root().should('have.class', 'query-ul')
})
})

it('best practices - selecting elements', () => {
// Worst - too generic, no context
cy.get('button').click()

// Bad. Coupled to styling. Highly subject to change.
cy.get('.btn.btn-large').click()

// Average. Coupled to the `name` attribute which has HTML semantics.
cy.get('[name=submission]').click()

// Better. But still coupled to styling or JS event listeners.
cy.get('#main').click()

// Slightly better. Uses an ID but also ensures the element
// has an ARIA role attribute
cy.get('#main[role=button]').click()

// Much better. But still coupled to text content that may change.
cy.contains('Submit').click()

// Best. Insulated from all changes.
cy.get('[data-cy=submit]').click()
})
})
Binary file added cypress/screenshots/my-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d484af

Please sign in to comment.