cypress

How to check if element exists using Cypress.io

两盒软妹~` 提交于 2019-12-22 06:59:06
问题 How to check if element is present or not, so that certain steps can be performed if element is present. Else certain different steps can be performed if element is not present. I tried something like below but it didn't work: Cypress.Commands.add('deleteSometheingFunction', () => { cy.get('body').then($body => { if ($body.find(selectors.ruleCard).length) { let count = 0; cy.get(selectors.ruleCard) .each(() => count++) .then(() => { while (count-- > 0) { cy.get('body') ... .... } }); } }); })

Cypress get href attribute

老子叫甜甜 提交于 2019-12-22 01:53:16
问题 I have a test case in which i have a link which opens in a new tab, and since cypress doesn't support multi tab, i wanna get href attribute of that link and then open it in the same tab, i`m trying to do it this way, but for some reason it doesn't work. it('Advertise link should refer to Contact page', () => { var link = document.querySelector("div.footer-nav > ul > li:nth-child(2) > a").href; cy.visit(link); cy.title().should('include', 'Text'); }); 回答1: The code below should do what you're

Preserve cookies / localStorage session across tests in Cypress

谁说我不能喝 提交于 2019-12-21 13:43:10
问题 I want to save/persist/preserve a cookie or localStorage token that is set by a cy.request() , so that I don't have to use a custom command to login on every test. This should work for tokens like jwt (json web tokens) that are stored in the client's localStorage. 回答1: From the Cypress docs For persisting cookies : By default, Cypress automatically clears all cookies before each test to prevent state from building up. You can whitelist specific cookies to be preserved across tests using the

How to get the text input field value to a const and log that value in Cypress.io

一世执手 提交于 2019-12-21 09:35:16
问题 How to get the text input field value to a 'const' variable in Cypress, so that I can log that variable using cy.log(). The below code doesn't log anything, can someone familiar with Cypress.io please advise cy.get('input[name="email"]').then(($text)=>{ const txt = $text.text() cy.log(txt) }) 回答1: Using invoke('val') instead of invoke('text') worked for my case. Reminder of the html tag <input type="text" class="form-control" name="email"> Cypress code cy.get('input[name="email"]') .invoke(

How do I enter data into a form input in an iframe using cypress?

馋奶兔 提交于 2019-12-20 23:28:24
问题 I have been trying to test a stripe checkout form using cypress.io If anyone has managed to get this to work please let me know. I found a thread on the matter here https://github.com/cypress-io/cypress/issues/136 and based on this I came up with: cy.get('iframe.stripe_checkout_app') .wait(10000) .then($iframe => { const iframe = $iframe.contents() const myInput0 = iframe.find('input:eq(0)') const myInput1 = iframe.find('input:eq(1)') const myInput2 = iframe.find('input:eq(2)') const myButton

In Cypress, set a token in localStorage before test

空扰寡人 提交于 2019-12-20 10:00:56
问题 I want to login and set a localStorage token on the client (specifically jwt) How can I accomplish this using cy.request , as suggested in the Cypress Documentation? 回答1: Here's an example of adding a command cy.login() that you can use in any Cypress test, or put in a beforeEach hook. Cypress.Commands.add('login', () => { cy.request({ method: 'POST', url: 'http://localhost:3000/api/users/login', body: { user: { email: 'jake@jake.jake', password: 'jakejake', } } }) .then((resp) => { window

Cypress.io How to handle async code

一笑奈何 提交于 2019-12-20 01:14:05
问题 I'm in the middle of process of moving our old capybara tests to cypress.io as our application is going SPA way. In our case we have over 2000 tests covering a lot of features. So common pattern to test feature is to have an user with created and published offer. On the beginning I wrote case where cypress were going trough page and clicking everything. It worked but I saw that offer create + publish took almost 1,5 minute to finish. And sometimes we need multiple offers. So we have a test

Debugging Cypress tests in Visual Studio Code

痞子三分冷 提交于 2019-12-19 03:10:10
问题 I want to use VS Code to edit and debug Cypress tests. It seems like this should be simple; the cypress docs mention VS Code directly (but give no clues about how to configure VS Code's launch.json file for debugging either there or on the debugging page). I have a launch.json configuration that starts cypress/electron, but VS Code gives this error: Cannot connect to runtime process… connect ECONNREFUSED 127.0.0.1:5858 Then shuts it down. Looking at the sample electron for VS Code project

Debugging Cypress tests in Visual Studio Code

这一生的挚爱 提交于 2019-12-19 03:10:07
问题 I want to use VS Code to edit and debug Cypress tests. It seems like this should be simple; the cypress docs mention VS Code directly (but give no clues about how to configure VS Code's launch.json file for debugging either there or on the debugging page). I have a launch.json configuration that starts cypress/electron, but VS Code gives this error: Cannot connect to runtime process… connect ECONNREFUSED 127.0.0.1:5858 Then shuts it down. Looking at the sample electron for VS Code project

In cypress, how do I wait for a page to load?

我只是一个虾纸丫 提交于 2019-12-19 00:19:31
问题 Don't tell anyone, but our app is not yet single-page. I can wait on a given XHR request by giving the route an alias, but how do I wait until some navigation completes and the browser is safely on a new page? 回答1: You can add some assert inside: cy.click('#someButtonToNavigateOnNewPage'); cy.location('pathname', {timeout: 60000}) .should('include', '/newPage'); cy.click('#YouAreOnNewPage'); You can change default timeout - by default it's 4000 ms (4 secs) - to ensure that user navigated the