我想从定义的数组中选择随机XPath

问题描述 投票:0回答:1

editMethod(){
    var num = Math.floor((Math.random() *4) + 1);

var xPath_arr= [

    '//android.widget.RadioButton[@text="seat1"]',
    '//android.widget.RadioButton[@text="seat2"]',
    '//android.widget.RadioButton[@text="seat3"]',
    '//android.widget.RadioButton[@text="Notselected"]',
    ]

    xPath_arr[num]

}

Then calling in another function like this

selectseat(){

this.editmethod();
this.doneBtn.click();

}

尝试了不同的方法,但是收到了不同的错误,例如

但不起作用

TypeError:无法读取未定义的属性'2'

node.js automation appium webdriver-io appium-android
1个回答
0
投票

enter image description here

const editMethod = () => {
    let num = Math.floor((Math.random() * 3) + 1);
    console.log(num)
    let xPath_arr = [
        '//android.widget.RadioButton[@text="seat1"]',
        '//android.widget.RadioButton[@text="seat2"]',
        '//android.widget.RadioButton[@text="seat3"]',
        '//android.widget.RadioButton[@text="Notselected"]',
    ]
    return xPath_arr[num];
}

// Then calling in another function like this

const selectseat =() => {

    let seatElement = editMethod();
    console.log(locator);

}

selectseat();

注意:

Math.random()* 3应该是因为您的数组仅包含4个元素。

© www.soinside.com 2019 - 2024. All rights reserved.