简体   繁体   English

如何访问在 Cypress/Javascript 的外部 scope 中的 function 内部更新的变量?

[英]How to access a variable which is updated inside the function in outer scope in Cypress/Javascript?

Here is the problem statement.这是问题陈述。 I am trying to find all items on a webpage and find the item with the minimum price using Cypress.我正在尝试查找网页上的所有商品,并使用赛普拉斯找到价格最低的商品。 The problem is, as long as I am inside the inner function, minPrice is correct.问题是,只要我在 function 内部,minPrice 是正确的。 However when I try to print minPrice in the outer function, minPrice again gets assigned to it outer scope value.但是,当我尝试在外部 function 中打印 minPrice 时,minPrice 再次被分配给外部 scope 值。 I am fairly new to JS, so looks like I am missing some basic JS concept here.我对 JS 相当陌生,所以看起来我在这里缺少一些基本的 JS 概念。 I tried a lot of things like variable scoping, async/await (which cypress claims it doesnt need), but no success.我尝试了很多东西,比如变量作用域、异步/等待(赛普拉斯声称它不需要),但没有成功。 Please help!请帮忙!

Below is the code.下面是代码。

getMinPrice(){
//Initialize to a very big number
  var minPrice = 10000;
 
  cy.get('.btn.btn-primary').each(function ($el, index,$list) {

    //For each element, do some parsing to get the price for the item
    var textToParse = $el.attr('onclick');
    var price = textToParse.slice(-4,-1);
    price = parseInt(price,10);
    
   //compare price with current MinPrice
   if (price < minPrice)
          minPrice = price; 
  });
 
  cy.log(minPrice); // Logs 10000 which is not what I am expecting
}

It is because JS works asynchronously your log statement is running before the value of minPrice is updated.这是因为 JS 异步工作,您的日志语句在minPrice的值更新之前运行。 To make sure that the log has the updated minprice, we can use then to make sure the log statement is run only after each has finished executing.为了确保日志具有更新的 minprice,我们可以使用then来确保日志语句仅在每个语句完成执行后才运行。

cy.get('.btn.btn-primary')
  .each(function ($el, index, $list) {
    //For each element, do some parsing to get the price for the item
    var textToParse = $el.attr('onclick')
    var price = textToParse.slice(-4, -1)
    price = parseInt(price, 10)

    //compare price with current MinPrice
    if (price < minPrice) minPrice = price
  })
  .then(() => {
    cy.log(minPrice) //should have the updated value
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外部变量作用域在javascript函数内部不可用 - Outer Variable scope is not available inside the javascript function 如何从 JavaScript 的外部作用域访问变量 - How to access a variable from outer scope in JavaScript Javascript外部范围变量访问 - Javascript outer scope variable access JavaScript 闭包和作用域:如何将外部作用域变量传递给作为参数传递的回调函数? - JavaScript closures and scoping: How to pass an outer scope variable to a callback function which is passed as an argument? 如何使用 JavaScript 中的单击方法访问外部 function 的内部 function? - How to access the inside function of outer function using on click method in JavaScript? 有什么方法可以在 Javascript 中访问外部作用域中的局部作用域变量? - Any way to access local scope variable in outer scope in Javascript? 访问javascript中函数内部函数内的变量? - Access a variable inside a function which is inside a function in javascript? 是否可以在JavaScript函数中从调用者的作用域访问变量? - Is it possible to access a variable from the caller's scope inside a function in JavaScript? 如何从内部函数访问外部作用域? - How to access outer scope from inner function? Javascript:使用&#39;this&#39;访问&#39;private&#39;子函数中的外部范围变量时得到&#39;undefined&#39; - Javascript: got 'undefined' when using 'this' to access outer scope variable in 'private' child function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM