Code Monkey home page Code Monkey logo

cypress-get-it's Introduction

cypress-get-it CircleCI renovate-app badge

Get elements by data attribute by creating a Cy command on the fly

NPM

Why

According to the Cypress best practices for Selecting Elements using dedicated test id data attributes is the most stable way of finding elements during end-to-end tests. Yet typing the full selector is ... annoying.

<div data-test-id="foo">foo</div>
cy.get('[data-test-id="foo"]').should('have.text', 'foo')

Who has time for this!

So you could make a little utility function

const tid = s => `[data-test-id="${s}"]`
cy.get(tid('foo')).should('have.text', 'foo')

Hmm, sure, but what if your HTML markup has different data attributes? Like this

<div data-test-id="foo">foo</div>
<div data-test="bar">bar</div>
<div test-id="baz">baz</div>

Do you need to write 3 different helper functions? No. With cypress-get-it you can create data attribute selector methods with descriptive names. In fact, the actual selector will be created from the method name on the fly. You want to get element with data-test-id=foo? Just use camel cased words and call cy.getDataTestId('foo'). The above elements become

cy.getDataTestId('foo').should('have.text', 'foo')
cy.getDataTest('bar').should('have.text', 'bar')
cy.getTestId('baz').should('have.text', 'baz')

cypress-get-it

So any element attribute like foo-bar-baz="something" could be fetched with descriptive cy.getFooBarBaz("something").

How it works

Load this plugin from your support file and it will replace global cy instance with an ES6 Proxy which will intercept any methods calls to get... and will call cy.get with the right selector automatically.

Install and use

npm i -D cypress-get-it

Require this module from your cypress/support/index.js

require('cypress-get-it')

Small print

Author: Gleb Bahmutov <[email protected]> ยฉ 2019

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2019 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cypress-get-it's People

Contributors

bahmutov avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

probil

cypress-get-it's Issues

Hide all the console.log() with config

Could we hide all the logging in https://github.com/bahmutov/cypress-get-it/blob/master/index.js? They are quite annoying while debugging with print statements.

Cannot convert a Symbol value to a string

Hi, Thanks for this package.

Trying to use it for data-test-id selector.

Code:

// New customer signup
cy.getDataTestID("navigation-sign-out").should("be.visible")
.click();
// cy.get("[data-test-id$='navigation-sign-out']")
// .should("be.visible")
// .click();

Error

image

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Found renovate config warnings

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency semantic-release to version 17.0.8 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 4.12.1 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 10.11.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 11.2.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 12.17.4 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 13.12.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 5.6.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 7.7.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 8.7.0 ๐ŸŒŸ
  • chore(deps): update dependency cypress to version 9.7.0 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 18.0.1 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 19.0.5 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 20.1.3 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 21.1.2 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 22.0.12 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 23.1.1 ๐ŸŒŸ
  • chore(deps): update dependency semantic-release to version 24.0.0 ๐ŸŒŸ
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Detected dependencies

npm
package.json
  • cypress 4.2.0
  • semantic-release 17.0.4

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:babel6). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

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.