如何单击按钮直到依赖值达到某个值? [英] How to click on Button until a dependent value reaches a certain value?

查看:29
本文介绍了如何单击按钮直到依赖值达到某个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用量角器,我尝试自动化 UI 的一部分,其中使用加减按钮选择了最小最大范围(即 100'000-200'000).

有一个 inputAmount(即 250'000),定义要选择的范围.现在我应该点击 plus-button 直到我得到一个范围 200'000-300'000,由动态调整的 <div class="currentMax">100'000<;/div>.

我的问题是将当前选定范围的 currentMax 从 String 转换为数字,以便我可以将其与 inputAmount 进行比较.HTML 如下所示:

<div class="max">100'000</div><div class="slider-buttons"><div class="slider-button"><div class="slider-button-plus"></div>

<div class="slider-button"><div class="slider-button-minus"></div>

</html>

我的量角器代码如下所示:

this.selectAmountRange = function() {var currentMax = $('.max').getText();//我如何得到这个值作为数字?var btnPlus = $('.slider-buttons .slider-button-plus');for (i = currentMax; i <= inputAmount; i = currentMax) {btnPlus.click();浏览器等待(300);};};

一个问题是 currentMax 是用'"分隔的千位分隔符,所以我不能直接转换它.另一个问题是,getText() 返回了一个 promise,但即使我找到了一些示例,但到目前为止我还是无法获取字符串,甚至删除了分隔符.

我试过了,但没有用.

var currentMax = $('.max').getText().then(function(value){value.replace(/\'/,'');});

由于我对 JavaScript 和 Protractor 还很陌生,我认为这只是我做错了一些语法.感谢您提供任何建议并推动实现可行功能.

解决方案

由于 javascript 是异步的,您需要在访问 promise 的返回值之前解析 promise.在您的情况下,您需要实现递归以使您的方法单击滑块直到设置所需的值.看看下面的例子.

var selectAmountRange = function(inputAmount) {$('.max').getText().then(function(currentMax){var btnPlus = $('.slider-buttons .slider-button-plus');if(currentMax < selectAmountRange){btnPlus.click();浏览器睡眠(3000);selectAmountRange(inputAmount);//调用相同的方法,直到选择所需的inputAmount.}};};

With Protractor I'm trying to automate a part of a UI, in which with plus-minus buttons a min-max-range (i.e. 100'000-200'000) is selected.

There is an inputAmount (i.e. 250'000), defining the range to select. Now I should click to the plus-button until I get a range 200'000-300'000, identified by the dynamically adjusted <div class="currentMax">100'000</div>.

My issue is to convert the currentMax of the currently selected range from String to a number, so I can compare it to the inputAmount. The HTML looks like this:

<html>
<div class="max">100'000</div>
<div class="slider-buttons">
  <div class="slider-button">
    <div class="slider-button-plus"></div>
  </div>
  <div class="slider-button">
    <div class="slider-button-minus"></div>
  </div>
</div>
</html>

My protractor code looks something like this:

this.selectAmountRange = function() {
    var currentMax = $('.max').getText();  //How do I get this value as number?
    var btnPlus = $('.slider-buttons .slider-button-plus');
    for (i = currentMax; i <= inputAmount; i = currentMax) {
        btnPlus.click();
        browser.wait(300);
    };
};

One problem is that currentMax is thousand-delimited with "'", so I can't convert it straight forward. The other problem is, that getText() returns a promise, but even though I found some examples I failed so far to get the string and even remove the delimiter.

I tried this, but didn't work.

var currentMax = $('.max').getText().then(function(value){
    value.replace(/\'/,'');
    });

As I'm quite new to JavaScript and Protractor, I assume it's just some syntax I'm doing wrong. Grateful for any advice and a nudge towards a doable function.

解决方案

Since javascript is asynchronous, you need to resolve the promise before accessing the return value from the promise. In your case, you need to implement recursion to make your method to click the slider until required value is set. Have a look at below example.

var selectAmountRange = function(inputAmount) {
     $('.max').getText().then(function(currentMax){
         var btnPlus = $('.slider-buttons .slider-button-plus');
          if(currentMax < selectAmountRange){
              btnPlus.click();
              browser.sleep(3000);
              selectAmountRange(inputAmount); //call the same method untill required inputAmount is selcted.
          }
     }; 
};

这篇关于如何单击按钮直到依赖值达到某个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆