简体   繁体   English

自定义函数以选择并单击Nightwatch中的随机元素

[英]custom function to select and click a random element in Nightwatch

I'm new to e2e testing (and JS). 我是e2e测试(和JS)的新手。 I use Nightwatch framework. 我使用Nightwatch框架。

I'm trying to create a function that selects a random element from a list of elements with the same selector, and clicks on it. 我正在尝试创建一个函数,该函数从具有相同选择器的元素列表中选择一个随机元素,然后单击它。

This is what I tried: 这是我尝试的:

  pickOne(selector, target) {
    this.api.elements(selector, target, function(res) {
       optionsLength = Math.floor((Math.random() * res.value.length) + 1);
    });
    this.api.waitForElementVisible(`${target}:nth-child(${optionsLength})`);
    this.api.click(`${target}:nth-child(${optionsLength}) .action-button`);
  }

But in this case, optionsLength is not defined . 但是在这种情况下, optionsLength is not defined

Math.floor((Math.random() * res.value.length) + 1) returns a number. Math.floor((Math.random() * res.value.length) + 1)返回一个数字。 I want to use this number outside of the function. 我想在函数之外使用此数字。

I've tried to store the full function in a variable, as in: 我试图将完整功能存储在一个变量中,如下所示:

const optionsLength = this.api.elements(selector, element, function(res) {
       Math.floor((Math.random() * res.value.length) + 1);
    });

But that way optionsLength logs [object Object] instead a number. 但是那样, optionsLength记录了[object Object]而不是一个数字。

Define 2nd and 3rd functions in callback is not good, it would lead to callback hell. 在回调函数中定义2nd和3rd函数不是很好,这会导致回调地狱。

You should use .perform() api instead , it will chain your functions : 您应该改用.perform() api,它将链接您的函数:

pickOne(selector, target) {    
            this.api.elements(selector, target, function(res) {
               optionsLength = Math.floor((Math.random() * res.value.length) + 1);
            })
             .perform(function (client, done) {
                 console.log(optionsLength) ;// ***you can access optionsLength in above function***

                 done();
            })

This is docs from nightwatch Perform 这是Nightwatch Perform的文档

Declaring the function with arrow syntax and wrapping everything into the callback has solved the (scope) issue. 使用箭头语法声明该函数并将所有内容包装到回调中已解决了(作用域)问题。

Arrow syntax allows to keep calling this.api.x in the callback. 箭头语法允许在回调中继续调用this.api.x

  pickOne(selector, target) {
     this.api.elements(selector, target, res => {
        optionsLength = Math.floor((Math.random() * res.value.length) + 1);
        this.api.waitForElementVisible(`${target}:nth-child(${optionsLength})`);
        this.api.click(`${target}:nth-child(${optionsLength}) .action-button`);
    });
  }
function postimgname() {
var date = new Date();
var str =$scope.data_user.user_id + "" +date.getFullYear() + "" + (date.getMonth() + 1) + "" + date.getDate() + "" +  date.getHours() + "" + date.getMinutes() + "" + date.getSeconds() 
+ "" + Math.floor(Math.random() * 6) + 1 ;
return str;

} }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM