I am getting to a point that i can cy.log() the text that is inside that element but i can't find a simple solution where i can get that text, store it and manipulate it for later tests.
it('login to my eshop that uses virtual currency' , function() {
cy.visit('my favorite shopping site')
cy.get('#username').type('user')
cy.get('#password').type('pass')
cy.get('balance element').then(function(text1){
cy.log(text1.text())
///I see the text but that's pretty much it.
})
})
I do not need an assertion for this particular case. I am trying to get a current balance, do some purchase testing and compare initial balance with remaining balance.
Any suggestions would be greatly appreciated , spent couple of days searching, trying and editing but i am stuck do the async nature of cypress.
CodePudding user response:
Try:
cy.get('balance element').invoke('text').then((text) => {
cy.wrap(text).as('balanceText');
});
Do this in beforeEach
then you can use it in further it
-functions like this:
it('my test', function () {
console.log(this.balanceText);
})
CodePudding user response:
In your case, you can return the value from your then()
command and have the value available in a subsequent command.
it('login to my eshop that uses virtual currency' , function() {
cy.visit('my favorite shopping site')
cy.get('#username').type('user')
cy.get('#password').type('pass')
cy.get('balance element')
.then(function(text1){
return text1.text()
}).then(function(text) {
// code that uses the `text` variable
});
})
CodePudding user response:
Option 1:
let balance;
it('Test case 1 Return variable', () => {
cy.get("balance element").invoke("text").then(cy.log).then((text) => {
balance = text;
return text;
})
});
it('Test case 2 Use variable', () => {
cy.log(balance)
});
Option 2
let balance;
it('Test case 1 Return variable', () => {
cy.get('balance element').invoke("text").then((text) => {
cy.log("Text", text).then(() => {
balance = text;
return balance;
})
});
});
it('Test case 2 Use variable', () => {
cy.log(balance)
});
Option 3 You can use the approach which the log:added event
// top of spec
const logs = []
Cypress.on('log:added', (log) => {
const message = `${log.consoleProps.Command}: ${log.message}`
logs.push(message)
})
Then in your it block
it('writes to logs', () => {
cy.get('balance element').invoke('text').then((text) => {
const balance = text
cy.log('balance', balance)
cy.writeFile('cypress/fixtures/logs.txt', balance)
})
});