Similarly to this question. How add two values[1,2 elements] and verify its equal to result[3 element]
from the below HTML structure I need to add A(element) & B(element) and verify the sum is equal to A+B(element) its complex for me since A & B are dynamic and they can or cannot be present depending on the changes from the user.
I tried to use the below script.
cy.get('[data-cy="Selected in Final InterviewofferBreakUpTableBodyCellRenderer"] >span').invoke('text')
But it's yielding both the text, i only need the number.
I need help in making a test that won't fail even if only one element A or B is present.
You can do something like this:
cy.contains('span', 'Final Interview Selects')
.next()
.invoke('text')
.then((sum) => {
cy.contains('span', 'Selected - Not Offered')
.next()
.invoke('text')
.then((A) => {
cy.contains('span', 'Selected in Final Interview')
.next()
.invoke('text')
.then((B) => {
expect(+sum).to.eq(+A + +B)
})
})
})
You can do something like this if some of the elements are not visible.
const A = 0,
B = 0
cy.get('body')
.then(($body) => {
if (
'$body:not(:contains("Selected - Not Offered"))' &&
'$body:contains("Selected in Final Interview")'
) {
A = 0
cy.contains('span', 'Selected in Final Interview')
.next()
.invoke('text')
.then((num) => {
B = +num
})
}
if (
'$body:contains("Selected - Not Offered")' &&
'$body:not(:contains("Selected in Final Interview"))'
) {
B = 0
cy.contains('span', 'Selected - Not Offered')
.next()
.invoke('text')
.then((num) => {
A = +num
})
}
if (
'$body:not(:contains("Selected - Not Offered"))' &&
'$body:not(:contains("Selected in Final Interview"))'
) {
A = 0
B = 0
}
})
.then(() => {
cy.contains('span', 'Final Interview Selects')
.next()
.invoke('text')
.then((sum) => {
expect(+sum).to.eq(A + B)
})
})