I am testing an application where the alert text says like
Customer added successfully with customer id :13
Now i want to grab 13 and save it in json file.
Somehow its not getting written in json file.
Please help
Below is my code
export function stripcustomerid() {
cy.on('window:alert', (txt) => {
cy.readFile('cypress/fixtures/account_details.json').then((data) => {
var customerno = txt.split(':')
const cno = customerno[1]
data.customerid = cno
cy.writeFile(
'cypress/fixtures/account_details.json',
JSON.stringify(data)
)
})
})
}
Assuming that the txt variable has the string Customer added successfully with customer id :13 and also your account_details.json file has an existing customer id data like customerid: 55, because you are changing this value. Then your code looks correct.
You can replace these three lines:
var customerno = txt.split(':')
const cno = customerno[1]
data.customerid = cno
with
data.customerid = txt.split(':')[1]