Basic cypress example is aborted after page visit - cypress

My basic test is aborted after the reddit page is visited.
Could you tell me how to keep the log window open?
cy.wait() is also not working. Could you tell me why?
/// <reference types="cypress" />
describe('My First Test', () => {
it('Does not do much!', () => {
expect(true).to.equal(true)
cy.visit('www.reddit.com')
cy.url().contains('reddit')
cy.wait(10000)
cy.log("hello")
cy.wait(10000)
})
})

Related

Blank page after running Cypress tests

Whenever I run a new cypress test, the page that is supposed to show the UI is blank. Even when I click on each part of each test it remains blank. Please see image below.
image
Cypress package version: 10.4.0
Node v16.16.0
Code:
describe("home page", () => {
beforeEach(() => {
cy.visit("http://localhost:3000")
})
context("Hero section", () => {
it("the h1 contains the correct text", () => {
cy.getByData("hero-heading").contains(
"Testing Next.js Applications with Cypress"
)
})
it("the features on the homepage are correct", () => {
cy.get("dt").eq(0).contains("4 Courses")
cy.get("dt").eq(1).contains("25+ Lessons")
cy.get("dt").eq(2).contains("Free and Open Source")
})
})
context("Courses section", () => {
it("CourseL Testing Your First Next.js Application", () => {
cy.getByData('course-0')
.find('a')
.eq(3)
.contains('Get started')
})
})
})
/// <reference types="cypress" />
Cypress.Commands.add('getByData', (selector) => {
return cy.get(`[data-test=${selector}]`);
});
I faced the same issue in Cypress version 12.0.0 and I solved it by adding this configuration to cypress.config.js
testIsolation: false,
Try adding 's' to http; this might solve that else here is similar issue reported which might give you clue to your problem https://github.com/cypress-io/cypress/issues/4235
You might have put the it() describe() statements in the wrong place. Try creating the most simple test file possible or better still use an example test that cypress provides strip it down and continue to test it until it is barebones.
I have a "solution" in my tests - it seems that the it steps lose the URL.
Remove all the it steps:
describe('Register Native', () => {
// it("Verify1", () => {
: a
// })
// it("Verify2", () => {
: b
// })
})
Now I have a structure like this (only one it step):
describe('Registrer Native', () => {
it('Whole test- Without IT parts', () => {
: a
: b
: c
})
})
It is not an optimal solution as I now have a long test without intermediary it sections.

After sign-in test case, next page should be signup page but it got failed and navigate to sign-in page for signup test case

In this script, I write first test case for sign-in and next one for sign-up. after signin page should navigate to signup page. After sign-in test case while executing signup test case it navigate again to sign-in page.
Why didn't it navigate to sign-up page after sign-in test case. Why it again start from sign-in page.
/// <reference types="cypress" />
context('Actions', () => {
let sidata, sudata
beforeEach(() => {
cy.fixture('signupdata').then(function(sdata) {
sidata=sdata
})
cy.fixture('signindata').then(function(sdata) {
sudata=sdata
})
})
before(() => {
cy.visit('link')
})
it('sigin', () => {
cy.go('forward')
cy.get('#userName-email').type(sudata.PaUsername)
cy.get('#signupSubmitId').click()
cy.wait(40000)
cy.get('#signupSubmitId').click()
})
it('sigup - must check', () => {
cy.get('#emailID').should('be.disabled')
cy.get('input[type=email]').should('contain.value',sudata.PaUsername)
})
it('sigup', () => {
cy.get('#firstName').clear().type('Madhavan')
cy.get('#lastName').clear().type('Prabakaran')
cy.get('#userGender').clear().select('Male')
cy.get('#userdob').clear().type('1999-06-19')
cy.get('#contactCountryCode').clear().select('+91(IN)')
cy.get('#contactMobileNumber').clear().type(sidata.PaMobile)
cy.get('#optin-email').check()
cy.get('#optin-mobile').check()
cy.get('#signupSubmitId')
})
})
Every it( , () => {}) block is treated as a separate test case hence the test for Sign up start's from Sign In Page as a new browser is launched it would be better to club the Sign In and Sign Up code to a single it() block.

Cypress cy.wrap() after page refresh in iframe

I have the following Cypress test:
describe(`sign in`, () => {
const getIframe = () => {
return cy
.get('iframe[id=my-iframe]')
.its('0.contentDocument.body')
.should('not.be.empty')
.then(cy.wrap)
.as('iFrame')
}
it('signs in', () => {
cy.visit(PATH.SIGN_IN)
getIframe()
cy.get('#iFrame').find('input[name=email]').click().type('user#test.test')
cy.get('#iFrame').find('input[name=password]').click().type('test-password')
cy.get('#iFrame').find('button').contains(/^Sign in$/).click()
// signin is successful and the contents of the page refresh to show a page containing <h3>Hello user</h3>
cy.get('#iFrame').find('h3').contains('Hello user').should('be.visible')
// the above line fails...
})
})
The test fails on the last line. Even though the contents inside the iframe successfully refresh and Hello user is visible, Cypress is not able to find this text inside cy.get('#iFrame').
How should I handle this situation correctly with Cypress?

Cypress with Auth0.com login and redirects

We have changed our login page so it's now redirecting to auth0.com and then back to our domain after you login to auth0. The issue is now when I login I get redirected to our QA environment which requires authentication so once the test submits the form I get a 401.
Before auth0 I was getting around the 401 by overwritting the visit function passing in a auth header.
If I try to visit our QA environment first before going to our login page I get
You may only cy.visit() same-origin URLs within a single test.
I've seen other questions asked about auth0 but not with also requiring authentication in the redirect, is it possible to still run tests on our QA environment?
While the Cypress team works to resolve max 1 site... support visiting multiple superdomains in one test on work around I have used in the past is this: detailed again below.
In commands.js
// -- Save localStorage between tests
let LOCAL_STORAGE_MEMORY = {};
Cypress.Commands.add('saveLocalStorage', () => {
Object.keys(localStorage).forEach(key => {
LOCAL_STORAGE_MEMORY[key] = localStorage[key];
});
});
Cypress.Commands.add('restoreLocalStorage', () => {
Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
});
});
// -- Visit multiple domains in one test
Cypress.Commands.add('forceVisit', url => {
cy.window().then(win => {
return win.open(url, '_self');
});
});
In test.spec.js
/// <reference types="cypress" />
context('Force Visit', () => {
it('should be able to visit and assert on two domains', () => {
cy.forceVisit('https://example.cypress.io');
cy.title().should('eq', 'Cypress.io: Kitchen Sink');
cy.forceVisit('https://www.google.com');
cy.title().should('eq', 'Google');
});
});
context('Auth Flow', () => {
before(() => {
cy.forceVisit('<auth url>');
cy.get('<username input>').type('<username>');
cy.get('<password input>').type('<password>');
cy.intercept('POST', '<auth login request>').as('auth');
cy.get('<submit button>').click();
cy.wait('#auth');
});
afterEach(() => {
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
// Preserve Cookies between tests
Cypress.Cookies.defaults({
preserve: /[\s\S]*/,
});
});
it('should be able to start in authorized state', () => {
cy.visit('<site url>');
});
});

Cypress test stops to execute in the middle of code

Here is my code:
context('Actions', () => {
beforeEach(() => {
cy.visit('http://www.staging.365dropship.com')
cy.contains('Login').click()
cy.wait(10000)
.get('#fancybox-frame').then(($iframe) => {
const $body = $iframe.contents().find('body')
cy.wrap($body).find('#login-username').type('testtust100#gmail.com')
cy.wrap($body).find('#login-password').type('1234567')
cy.wrap($body).find('button').click()
})
})
it('continue', () => {
cy.wait(10000)
cy.url().should('include', '/owner/index')
cy.get('a.icon-sale').click()
cy.wait(10000)
})
})
On the first wait after login test stops execution with no reason and no errors in logs... Can somebody help me where I make a mistake?
that is the last screen I caught before test stops, the wait is not complete
after I got this
The mentioned test works fine at my place. I guess the credentials are known invalid, but it fails at the point where I expected it to fail:

Resources