Javascript cypress:在多个函数中使用一个变量

Javascript cypress:在多个函数中使用一个变量,javascript,cypress,Javascript,Cypress,我想在两个不同的函数中使用一个变量。更确切地说,我想获得标签的一个数字(作为字符串),并将其设置到一个输入字段中。之后,我检查另一个标签的正确(结果)文本 我已经编写了两个正常工作的函数(分别执行),但是我想在第二个函数中使用第一个函数的值(存储在变量中) 因此,我尝试将函数放在一起,但cypress没有找到给定的csspath“#sQuantity”,因为cypress指向(另一个作用域)表中的元素,而我的元素不属于该表。 第一个函数的变量“txtAmountColumn”的给定值在第二个函数

我想在两个不同的函数中使用一个变量。更确切地说,我想获得标签的一个数字(作为字符串),并将其设置到一个输入字段中。之后,我检查另一个标签的正确(结果)文本

我已经编写了两个正常工作的函数(分别执行),但是我想在第二个函数中使用第一个函数的值(存储在变量中)

因此,我尝试将函数放在一起,但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>
CypressError:cy.type()只能接受字符串或数字。你通过了“未定义”


如何使用“txtAmounColumn”进行计算(在两个函数中)?

状态可以在测试用例(
it
块)之间轻松传递,因为回调是串行调用的,一次调用一个。因此,第一个测试用例中的变量集将在第二个测试用例运行时定义:

let值;
描述('测试',()=>{
它('一',()=>{
cy.document().然后(doc=>{
doc.body.innerHTML='42';
});
cy.get('.test')。invoke('text')。然后(val=>{
值=val;
});
});
它('two',()=>{
cy.document().然后(doc=>{
doc.body.innerHTML='';
});
cy.get('.test').type(值)
.invoke('val')。应('eq','42');
});
});
另一方面,如果您试图在单个测试用例中重用变量,那么您可以这样做:

description('test',()=>{
它('test',()=>{
cy.document().然后(doc=>{
doc.body.innerHTML=`
42
`;
});
让价值;
cy.get('.test1')。invoke('text')。然后(val=>{
值=val;
});
然后(()=>{
//请注意,这些命令可能已嵌套到'then'中`
//直接在上面回调,而不需要缓存变量
//根本
cy.get('.test2').type(值)
.invoke('val')。应('eq','42');
});
});
});

请参见我对该模式的详细说明。

它应该有效,但。。您的代码中有一个
type()
,您正在传递一个在第一次测试中未设置的硬编码值
txtAmount
,您是否尝试传递它
txtAmountColumn
?解决方案太简单,不可能是真的,但它会起作用。如果我设置(声明和初始化)两个变量
let txtAmount=txtAmountColumn;让floatPackPrice=parseFloat(txtpacpricecolumn.replace(',','。)
在第二个测试用例开始时,它将按预期工作。为了更好的测试目的(独立的测试用例),我将把它放在一个测试用例里,以避免错误的命名。现在,在您的示例的帮助下,我知道了如何在一个文件或案例中创建和测试演示代码。@jola cool。如果它回答了你的问题,你可以接受它。我把它改写了一点。