赛普拉斯:在多个函数中使用变量

编程入门 行业动态 更新时间:2024-10-28 00:23:28
本文介绍了赛普拉斯:在多个函数中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to use one variable in two different functions. More precisely I want to get a number (as a string) of a label and set it into an input field. Afterwards I check another label for the right (resulted) text.

我编写了两个功能正常的函数(单独执行),但是我想使用该值(存储的)

I have written two functions that are working correctly (executed separately) but I want to use the value (stored in a variable) of the first function within the second function.

所以我试图将这些功能放在一起,但是赛普拉斯没有找到给定的csspath'#sQuantity ',因为cypress指向表中的元素(另一个作用域),而我的元素不属于该表。 第一个函数的变量'txtAmountColumn'的给定值在第二个函数中用于某些计算。

So I have tried to put the functions together but then cypress doesn't find the given csspath '#sQuantity' because cypress points to (an other scope) the element within the table and my element doesn't belong to the table. The given value of the variable 'txtAmountColumn' of the first function is used in the second function for some calculations.

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进行计算?

How can I use 'txtAmounColumn' for my calculation(in both functions)?

推荐答案

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

State can be easily passed between test cases (it blocks) because the callbacks are invoked serially, one at a time. Thus, a variable set in the first test case will be defined by the time the second test case runs:

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'); }); });

另一方面,如果您要在单个测试用例中重用变量,那么您可以像这样:

If, on the other hand, you're trying to reuse a variable within a single test case, then you can do so like this:

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'); }); }); });

请参阅我的较旧的答案详细说明这种模式。

See my older answer elaborating on this pattern.

更多推荐

赛普拉斯:在多个函数中使用变量

本文发布于:2023-07-26 07:02:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1214856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   函数   普拉斯

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!