如何从两个元素中获取文本,将它们转换为数字并将它们添加到柏树中?
How to get text from two elements, convert them to a number and add them in cypress?
我正在将测试从 Protractor 转移到 Cypress。
我有如下两个元素。我想获取数字并将它们相加并将它们存储在变量中。
<span id="num-2">20</span>
在量角器中我是这样写的
var totalCount =
parseInt(element(by.id('num-1')).getText()) +
parseInt(element(by.id('num-2')).getText());
我不知道如何在赛普拉斯中做到这一点。
你可以达到aliases/variables and get。诀窍在于 get、invoke 或类似操作实际上是异步的,您不能真正有效地 await
cypress 操作。相反,您可以获取第一个数字,解析它,获取第二个数字,解析它,添加数字和 assert/expect:
const expectedTotal = 42;
// get element with id #num-1
cy.get('#num-1').then(($num1) => {
// parse text to int/number
const num1 = parseInt($num1.text());
// get element id #num2
cy.get('#num-2').then(($num2) => {
// parse text to int/number
const num2 = parseInt($num2.text());
// expect/assert number total equal to some expected total
expect(num1 + num2).toEqual(expectedTotal);
});
});
希望对您有所帮助!
我正在将测试从 Protractor 转移到 Cypress。
我有如下两个元素。我想获取数字并将它们相加并将它们存储在变量中。
<span id="num-2">20</span>
在量角器中我是这样写的
var totalCount =
parseInt(element(by.id('num-1')).getText()) +
parseInt(element(by.id('num-2')).getText());
我不知道如何在赛普拉斯中做到这一点。
你可以达到aliases/variables and get。诀窍在于 get、invoke 或类似操作实际上是异步的,您不能真正有效地 await
cypress 操作。相反,您可以获取第一个数字,解析它,获取第二个数字,解析它,添加数字和 assert/expect:
const expectedTotal = 42;
// get element with id #num-1
cy.get('#num-1').then(($num1) => {
// parse text to int/number
const num1 = parseInt($num1.text());
// get element id #num2
cy.get('#num-2').then(($num2) => {
// parse text to int/number
const num2 = parseInt($num2.text());
// expect/assert number total equal to some expected total
expect(num1 + num2).toEqual(expectedTotal);
});
});
希望对您有所帮助!