I have this test where it logs via cy.request() in and creates the session object in before each:
cy.request({
method: 'POST',
url: `${URL}/sessions`,
body: {
userName: 'xxx',
password: 'yyy',
},
});
Then during the first test it logs out via the interface:
cy.page()
.get('button[logout="UserMainMenu"]')
.should('be.lengthOf', 1).click({ force: true })...
That works until now. But then the next test starts, doing the beforeEach() again.
Then it tries to restore the session object. But can't and after a time out recreates it.
I would like to avoid this timeout, and started to add the following commands:
cy.clearAllCookies();
cy.clearAllLocalStorage();
cy.clearAllSessionStorage();
How ever this does not make a difference. It still recreates:
Current version of cypress: 12.4.1
OK. I found it: to clear the complete session, it needs this command:
Cypress.session.clearAllSavedSessions()
(a logout via interface and login again (with same user) seems to inactivate the session)
Related
I want to verify if the API request contains what it needs to contain, but not sure what I am doing wrong as I have always this error:
cy.wait() timed out waiting 5000ms for the 1st request to the route: apiCheck. No request ever occurred.
I have a link in the main menu, after clicking that link the new page opens and API call is iniciated immediately http://localhost:8081/currencies
So in Cypress I have this:
cy.get('ul li[title="Menu item"]>a').click();
cy.intercept({
method: 'GET',
url: '/currencies',
}).as('apiCheck')
cy.wait('#apiCheck').then((interception) => {
expect(interception.response.statusCode).to.equal(200);
expect(interception.response.body[0]).to.have.property('geographyName', 'EMEA')
})
As you can see from Cypress test runner screenshot, the request is there and has status 200, but still it says no request ever occurred.
ah, well, seems I need to call cy.intercept before the link is clicked, then it seems working.
When i click on some url it triggers one signature url under Developer tools->network tools.
Can i use cypress to test anything which is triggered in Developer tools.
Did you take a look at Intercept command? You can grab just about any network request with it.
Most basic form:
cy.intercept({
method: 'GET',
url: '/users*',
hostname: 'localhost',
}).as('usersRequest')
cy.wait('#usersRequest')
.then(interception => {
// interception has similar data to Chrome devtools
})
I am working on a project to automate using Cypress. In this project, I need to create an order for a patient. When I click on the submit button it will call the following API https://ibis-dev.droicelabs.us/api/dispenser/orders/ using the POST method and return one unique order that I want to get.
I have registered cy.intercept on top of my test like this:
cy.intercept({
method: 'POST',
url: 'https://ibis-dev.droicelabs.us/api/dispenser/orders/',
}).as('ordersCall')
And when the submit button is clicked I have used:
cy.clickOnElementUsingXpath(practicePageSelectors.submit_CreateOrderButton); // click on submit button
cy.wait('#ordersCall')
.its('response.body')
.then((body) => {
// parsing might be not needed always, depends on the api response
const bodyData = JSON.parse(body)
cy.log(bodyData)
})
But it returns the following error:
Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: ordersCall. No request ever occurred in cy.wait('#ordersCall')
Can anyone help me to get an orderID? Is there any other way to get the orderID?
After checking the provided images in the question comments, the error is as follows: Your intercept command in your Cypress test is waiting for requests to be made to your DEV environment, but looking at your last image from the console in the Cypress test runner your requests are being made to the QA environment.
So you either have to adjust your Interceptor like this:
cy.intercept({
method: 'POST',
url: 'https://ibis-qa.droicelabs.us/api/dispenser/orders/',
}).as('ordersCall')
or think about using relative paths for API calls to be independent from the environment:
cy.intercept({
method: 'POST',
url: '/api/dispenser/orders/',
}).as('ordersCall')
I have the following:
describe('Page not authenticated', () => {
it('Page has authentication', () => {
cy.visit('https://myauthsite.com/logout');
cy.visit('/', { failOnStatusCode: true });
cy.url().should('include', 'https://myauthsite.com/');
});
})
For internal reasons, the package is set up so that it always has a persistent session, so I need a test to visit a specific URL that would explicitly logout and "kill" the session and then visit the main page that I'm trying to test to ensure that it does redirect me to the right page. However, I'm running into this error
You may only `cy.visit()` same-origin URLs within a single test.
The previous URL you visited was:
> 'https://myauthsite.com'
You're attempting to visit this URL:
> 'https://myactualsite.com'
You may need to restructure some of your test code to avoid this problem.
https://on.cypress.io/cannot-visit-different-origin-domain
A lot of the workarounds that's listed mostly addresses checking for the attribute instead of visiting the url, or splitting the visits into two different test case, but in my case I really need it to be in its own encapsulated test, is that not possible?
cy.request() isn't bound by the same cross-origin request policies, so if you can log out via an HTTP request that should work.
I tried using chromeWebSecurity=false, but that didn't help.
I'm currently using the API login, setting the cookies, and later read "Google Sign in with Cypress"''
cy.request({
method:"POST",
url:API_URL,
headers: {
'Content-Type': 'application/json',
},
body: api_body}).then((response =>{
cy.log(response.body)
cy.setCookie("token_name",response.body['token']);
cy.visit(destination_url);
}))
API_URL is your API login URL:
token_name: Login manually to UI, Using the web developer tool you can get the required token_name
This resolved my multi-domain login issue, and you can use it for multi-domain navigation.
I am testing a signin page for keystonejs (http://demo.keystonejs.com/) and cannot get the request to succeed with cypress. A signin request without cypress has the following request headers:
while a request with a cypress test has the following request headers:
The only difference I can see is that the cookie is not set in the cypress test request. And because of that the request gets a 403. I am actually using a local version of that server in which I have the email/password configured as the one in the images. The demo site uses a different set provided in that page.
The test is simply:
describe('The Keystone Admin UI Signin Page', function () {
before(function() {
cy.visit('http://localhost:3000/keystone/signin')
})
it('should signin successfully with valid email/password', function () {
cy.get('#signin-view input[name=email]').clear().type('user#test.e2e');
cy.get('#signin-view input[name=password]').clear().type('test');
cy.get('#signin-view button[type=submit]').click();
cy.get('#react-root').should('be.visible');
})
})
Anyway to get around this?
I know this is an older question but I'll post this in case it helps anyone.
The key to fixing this issue for me was that Cypress clears all cookies before each test (https://docs.cypress.io/api/commands/clearcookies.html#).
So in the example above (and in my experience):
The first test (loading the page) is run and passes fine - at this point the browser has set the csrftoken cookie
The second test (submitting the form) runs but before this happens Cypress has deleted the csrftoken cookie - hence the error
To fix this I had to whitelist the csrftoken cookie in Cypress config which means that it will be exempt from this clearing process:
Cypress.Cookies.defaults({
whitelist: "csrftoken"
})
You can do this inline in your test file but the docs recommend using a support file:
A great place to put this configuration is in your cypress/support/index.js file, since it is loaded before any test files are evaluated.
Hope this helps, Stephen.
Following on from FlatSteve's answer, using with Laravel and updated prop names use;
Cypress.Cookies.defaults({
preserve: ["XSRF-TOKEN", "your_laravel_session_name", "remember_token"]
});