Giter Site home page Giter Site logo

saymelak / cypress-xpath Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cypress-io/cypress-xpath

0.0 0.0 0.0 1.67 MB

Adds XPath command to Cypress test runner

Home Page: https://www.cypress.io

License: MIT License

JavaScript 93.70% HTML 6.30%

cypress-xpath's Introduction

cypress-xpath

CircleCI renovate-app badge cypress version

Adds XPath command to Cypress.io test runner

Install with npm

npm install -D cypress-xpath

Install with Yarn

yarn add cypress-xpath --dev

Then include in your project's cypress/support/index.js

require('cypress-xpath')

Use

After installation your cy object will have xpath command.

it('finds list items', () => {
  cy.xpath('//ul[@class="todo-list"]//li')
    .should('have.length', 3)
})

You can also chain xpath off of another command.

it('finds list items', () => {
  cy.xpath('//ul[@class="todo-list"]')
    .xpath('./li')
    .should('have.length', 3)
})

As with other cy commands, it is scoped by cy.within().

it('finds list items', () => {
  cy.xpath('//ul[@class="todo-list"]').within(() => {
    cy.xpath('./li')
      .should('have.length', 3)
  });
})

note: you can test XPath expressions from DevTools console using $x(...) function, for example $x('//div') to find all divs.

See cypress/integration/spec.js

Beware the XPath // trap

In XPath the expression // means something very specific, and it might not be what you think. Contrary to common belief, // means "anywhere in the document" not "anywhere in the current context". As an example:

cy.xpath('//body')
  .xpath('//script')

You might expect this to find all script tags in the body, but actually, it finds all script tags in the entire document, not only those in the body! What you're looking for is the .// expression which means "any descendant of the current node":

cy.xpath('//body')
  .xpath('.//script')

The same thing goes for within:

cy.xpath('//body').within(() => {
  cy.xpath('.//script')
})

This explanation was shamelessly copied from teamcapybara/capybara.

TypeScript and IntelliSense support

To properly load the types for cy.xpath command, add to your spec file the following comment.

/// <reference types="cypress-xpath" />

cypress-xpath intellisense

If using TypeScript, add cypress-xpath to the list of types to be loaded in tsconfig.json

{
  "compilerOptions": {
    "types": ["cypress", "cypress-xpath"]
  }
}

For more, see Intelligent Code Completion

Roadmap

  • wrap returned DOM nodes in jQuery #2
  • retry the assertion that follows #3
  • add TypeScript definitions #4
  • search from the previous subject element #5
  • log or not, depending on user option #19

License

This project is licensed under the terms of the MIT license.

Update badge

To update the Cypress version badge in this README file run

$ npm run badges

Additional info

cypress-xpath's People

Contributors

renovate[bot] avatar renovate-bot avatar bahmutov avatar badeball avatar camineet avatar jennifer-shehane avatar nicholasboll avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.