I went through your function and I think there are several issues with the usage. This might be a cypress related issue or the wait until plugin.
Firstly,
In your code, I think with elem you try to pass something like cy.get("path")
The problem is when you first invoke elem and try to re-use elem, it becomes null. So if you expect cy.get('path') to run twice, it's not happening.
Secondly,
For some reason, in your wait untill,
cy.waitUntil(() => elem.invoke('attr', 'value').then((val) => val != initialVal))
//This part becomes true all the time.
elem.invoke('attr', 'value').then((val) => val != initialVal))
Thirdly,
invoke('attr', 'value')
will not capture the changing attributes of the object.
Please note these observations are gathered from trying out what you tried to do. Need to extra research to find out the exact behaviour of the functions.
Considering the above facts, I did a few changes in the code.
- The main update of the changes are passing the CSS path instead of a cypress object (elem)
- using js query selector operations to get the changing value ( to take the initial value we can use invoke )
Example 1: Getting the new value inside waitUntil and check for a variable change
const CheckElementChange = (path) => {
//get the initial value of your object
cy.get(path).invoke('attr', 'value').then($initialVal => {
//It's better if you can do your click operation here
//Wait untill the element changes
cy.waitUntil(() =>
cy.get(path).then($newVal =>
$newVal[0].value !== $initialVal),
//optional timeouts and error messages
{
errorMsg: "was expeting some other Value but got : " + $initialVal,
timeout: 10000,
interval: 500
}
).then(() => {
cy.log("Foudn a difference in values")
})
})
}
Example 2: move waitUntill after the new value extraction and wait till newVal !== initialVal to be true
const CheckElementChangew = (path) => {
//get the initial value of your object
cy.get(path).invoke('attr', 'value').then($initialVal => {
//It's better if you can do your click operation here
//Wait untill the element changes
cy.get(path).then($newVal => {
cy.waitUntil(() => $newVal[0].value !== $initialVal, {
//optional timeouts and error messages
errorMsg: "was expeting some other Value but got : " + $initialVal,
timeout: 10000,
interval: 500
}).then(() => {
cy.log("Foudn a difference in values")
})
})
})
}
Usage :
it('test',() => {
CheckElementChange2("#fname");
})
Note: If you expect the value change upon your click, unless the value change has a time gap of a few seconds, It's better if you can do the click after the extraction of the initial value ( as commented on the codes above )