gray-kilobyte-89541
10/26/2022, 10:32 AMquaint-hospital-22081
10/26/2022, 10:43 AMstale-optician-85950
10/26/2022, 11:00 AMlimited-fish-66138
10/26/2022, 1:21 PMfast-artist-45202
10/26/2022, 2:12 PMcy.request
to hit an API endpoint that should return all the account info in the response. I need to create disposable accounts on the fly, then have Cypress get the account ID number so that we can use an e2e endpoint to make changes to the account to get it in the state it needs to be in for testing.
I'm using this:
cy.request({
method: "GET",
url: "https://my.api.url/customers/me",
headers: {
"Content-Type": "application/vnd.api+json",
},
}).then((response) => {
cy.log(response.body);
});
I'm getting a 200 response in Cypress's ui, but I should be getting an array full of customer info, but instead I'm not seeing anything in the console or network tab, and the log only says {data: Object{3}}
Can someone tell me what I'm doing wrong? Thanks much!fresh-doctor-14925
10/26/2022, 2:13 PMcy.log
with console.log
and have the developer tools openfast-artist-45202
10/26/2022, 2:16 PMcy.request
to an e2e endpoint that uses that customer ID number. Is there a way to make that visible to cypress so I can use it in this way?fresh-doctor-14925
10/26/2022, 2:19 PMfast-artist-45202
10/26/2022, 2:19 PM{data: Object{3}}
, it's already available to cypress even though I don't see it! So, since we're getting the data object, and if I console log it, I can figure out exactly how it's formatted and check for that valuefast-artist-45202
10/26/2022, 2:20 PMfast-artist-45202
10/26/2022, 2:53 PMfast-artist-45202
10/26/2022, 2:55 PMcy.intercept("GET", "*/customers/me").as("customerInfo");
cy.request({
method: "GET",
url: "https://my.api.site/customers/me",
}).then((response) => {
console.log(response.body);
});
cy.get("@customerInfo")
.its("response.body")
.invoke("attr", "customerId")
.then(($customerID) => {
console.log($customerID);
});
And I'm definitely seeing the info I need, it's structured like this:brash-mechanic-5372
10/26/2022, 3:08 PMacceptable-solstice-90676
10/26/2022, 3:16 PMe2e.js
file (see file attached), that works pretty good for me, but I face a problem, if a test fails or for some reason is not finished, and the next one is going to run, Cypress is logging in with the account of the test that failed, even if I have one of these options set in before/beforeEach/after/afterEach:
- cy.clearCookies
- cy.clearCookie('sessionID')
-
Cypress.session.clearAllSavedSessions()`
I logging as follow:
Cypress.Commands.add('loginByAPI', (email, password='mypassword') => {
cy.clearCookies('sessionID') //added this to try here
cy.request({
method: 'POST',
url: Cypress.env('accountAPIUrl') + '/security/login2',
body:
{
"email": email,
"password": password,
}
}).then((response) => {
expect(response.status).to.eq(200)
cy.log({ name: 'loginResponse', message: response.body.sessionID })
cy.log({ name: 'loginResponse', message: response.body.email })
cy.setCookie('sessionID', response.body.sessionID)
cy.visit(/
);
})
})fast-artist-45202
10/26/2022, 3:30 PMcy.request({
method: "GET",
url: "https://my.api.site/customers/me",
}).then((response) => {
console.log(response.body.data.attributes.customerId);
});
It's printing the Id properly, so I can go from there and assign the value to a variable. Didn't realize I had to chain every layer of the response body, but now I doearly-article-61542
10/26/2022, 4:30 PMts
cy.get('.search-button').click() // sometimes fails
// want to check if element is present before
But
if I use
ts
if(Cypress.$('.search-button').length) {
// the block is never executed
}
gray-kilobyte-89541
10/26/2022, 4:49 PMearly-article-61542
10/26/2022, 4:55 PMacoustic-lock-46014
10/26/2022, 5:10 PMCypress.Commands.add('yourCustomCommand', () => {
cy.wait(4000)
cy.get('body')
.then($body => {
if (${elementYouWantToClick/wait}.length) {
return true;
}
return false;
})
.then(boolean => {
if(boolean){
cy.get(${elementYouWantToClick).click();
} else {
cy.wait(100)
}
});
})
gray-kilobyte-89541
10/26/2022, 5:14 PMtall-forest-29063
10/26/2022, 5:33 PMtall-forest-29063
10/26/2022, 5:35 PMtall-forest-29063
10/26/2022, 5:42 PMtall-forest-29063
10/26/2022, 6:09 PMfresh-doctor-14925
10/26/2022, 6:50 PMfresh-doctor-14925
10/26/2022, 6:58 PMtall-forest-29063
10/26/2022, 9:15 PMstale-optician-85950
10/26/2022, 9:25 PMbillowy-librarian-83114
10/26/2022, 10:01 PMcy.get('@item')
.click()
.find('input')
.should('be.visible')
.type(`${rowData['Value']['value']}{enter}`, { delay: 190 })
cy.get('@item').should('have.text', rowData['Value']['value'])
billowy-librarian-83114
10/26/2022, 10:16 PMcy.get('@item')
.click()
.find('input')
.should('be.visible')
.type(rowData['Value']['value'])
cy.wait(1000)
cy.get('@item')
.find('input')
.type(`{enter}`)
cy.wait(1000)
cy.get('@item').should('have.text', rowData['Value']['value'])