【发布时间】:2019-10-31 23:00:59
【问题描述】:
我想在两个不同的函数中使用一个变量。更准确地说,我想获取标签的数字(作为字符串)并将其设置到输入字段中。然后我检查另一个标签是否有正确的(结果)文本。
我编写了两个正常工作的函数(分别执行),但我想在第二个函数中使用第一个函数的值(存储在变量中)。
所以我试图将这些函数放在一起,但是 cypress 没有找到给定的 csspath '#sQuantity',因为 cypress 指向(另一个范围)表中的元素,而我的元素不属于表. 第一个函数的变量“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”进行计算(在两个函数中)?
【问题讨论】:
-
它应该可以工作,但是......你的代码中有一个
type(),你传递给它一个硬编码的值txtAmount,它没有在第一个测试中设置:/,你试过吗通过txtAmountColumn? -
解决方案太简单了,但它会起作用。如果我在第二个测试用例开始时设置(声明和初始化)两个变量
let txtAmount = txtAmountColumn; let floatPackPrice = parseFloat(txtPackPriceColumn.replace(',', '.')),它将按预期工作。为了更好的测试目的(独立的测试用例)我会把它放在一个测试用例中
标签: javascript cypress