我想在两个不同的函数中使用一个变量。更准确地说,我想获取标签的数字(作为字符串)并将其设置为输入字段。然后,我检查另一个标签以获取正确的(结果)文本。
我编写了两个功能正常的函数(分别执行),但是我想在第二个函数中使用第一个函数的值(存储在变量中)。
所以我试图将这些功能放在一起,但是赛普拉斯找不到给定的csspath'#sQuantity',因为赛普拉斯指向了(另一个作用域)表中的元素,而我的元素不属于该表。 第一个函数的变量'txtAmountColumn'的给定值在第二个函数中用于某些计算。
let txtAmountColumn
let txtPackPriceColumn
let txtDiscountColumn
it('get some values', function() {
//go to page
cy.loadpage(txtUrl)
//find product box
cy.get('.ProductSelectionBox table').within(($scaleTable) => {
//find table of scaled discount
cy.get('tbody > tr').eq(1).within((rowTable) => {
//get second row of table
let txtRowTable = rowTable.text()
//get first column (amount) of row
cy.get('td').eq(0).then((lineOfTable) => {
let txtValueOfFirstColumn = lineOfTable.text()
txtAmountColumn = txtValueOfFirstColumn.match(/\d{1,}/)[0]
cy.log(txtAmountColumn)
})
//get second column (price of pack price) of row
cy.get('td').eq(1).then((lineOfTable) => {
let txtValueOfSecondColumn = lineOfTable.text()
txtPackPriceColumn = txtValueOfSecondColumn.match(/[0-9]*,[0-9]*/)[0]
cy.log(txtPackPriceColumn)
})
//get third column (discount in percentage) of row
cy.get('td').eq(2).then((lineOfTable) => {
let txtValueOfThirdColumn = lineOfTable.text()
txtDiscountColumn = txtValueOfThirdColumn.match(/\d{1,}/)[0]
cy.log(txtDiscountColumn)
})
})
})
})
// ToDo: integrate this function within previous function because I need a dynamic value for txtAmount
it('calculate the price', function() {
let txtAmount = 10 //replace this hardcoded value with the determined value of txtAmountColumn
let txtPackPriceColumn = 9.99
//go to the sale
cy.loadpage(txtUrl)
//set amount of products
cy.get('#sQuantity').type(txtAmount).then(() =>{
cy.get('.MainProductCalculatedPriceOverview').then((labelPrice) => {
let txtPrice = labelPrice.text()
//calculate expected price
let calculatedPrice = txtAmount * txtPackPriceColumn
//calculate expected VAT
let calculatedVat = Math.round((calculatedPrice * 1.19)*100)/100
})
})
})
如果我把它们放在一起
<p>CypressError: cy.type() can only accept a String or Number. You passed in: 'undefined'</p>
如何在两个函数中使用“ txtAmounColumn”进行计算?
答案 0 :(得分:0)
状态可以在测试用例(it
块)之间轻松传递,因为回调是串行调用的,一次调用一次。因此,第一个测试用例中设置的变量将由第二个测试用例运行的时间定义:
let value;
describe('test', () => {
it('one', () => {
cy.document().then(doc => {
doc.body.innerHTML = '<div class="test">42</div>';
});
cy.get('.test').invoke('text').then( val => {
value = val;
});
});
it('two', () => {
cy.document().then(doc => {
doc.body.innerHTML = '<input class="test">';
});
cy.get('.test').type(value)
.invoke('val').should('eq', '42');
});
});
另一方面,如果您尝试在单个测试用例中重用变量,则可以这样做:
describe('test', () => {
it('test', () => {
cy.document().then(doc => {
doc.body.innerHTML = `
<div class="test1">42</div>
<input class="test2">
`;
});
let value;
cy.get('.test1').invoke('text').then( val => {
value = val;
});
cy.then(() => {
// note that these commands could have been nested into the `then`
// callback above, directly, without needing to cache the variable
// at all
cy.get('.test2').type(value)
.invoke('val').should('eq', '42');
});
});
});
请参阅我的older answer详细介绍此模式。