Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SeleniumWebDriver循环_Java_Arrays_Loops_Selenium_Webdriver - Fatal编程技术网

Java SeleniumWebDriver循环

Java SeleniumWebDriver循环,java,arrays,loops,selenium,webdriver,Java,Arrays,Loops,Selenium,Webdriver,我在测试用例中有一个数组,然后将它传递到另一个字符串中,从数组中选择一个随机项 我遇到一个问题,当在循环中调用这个数组时,每次都从数组中选择相同的值 这里是正在讨论的数组-它位于基类中 private String asapSelector = "#main-view > div > section:nth-child(1) > section:nth-child(7) > div:nth-child(2) > radio-button-collection >

我在测试用例中有一个数组,然后将它传递到另一个字符串中,从数组中选择一个随机项

我遇到一个问题,当在循环中调用这个数组时,每次都从数组中选择相同的值

这里是正在讨论的数组-它位于基类中

private String asapSelector = "#main-view > div > section:nth-child(1) > section:nth-child(7) > div:nth-child(2) > radio-button-collection > custom-radio-button:nth-child(1) > label";
private String laterSelector = "#main-view > div > section:nth-child(1) > section:nth-child(7) > div:nth-child(2) > radio-button-collection > custom-radio-button:nth-child(2) > label";
String [] dateAndTime = {asapSelector, laterSelector}; //DATE & TIME ARRAY
String randomDateAndTime = (dateAndTime[new Random().nextInt(dateAndTime.length)]); //SELECT RANDOM DATE & TIME FROM ARRAY
另外,调用“randomDateAndTime”的方法位于基类中,看起来像

protected void selectDateTimeRadio() {
    driver.findElement(By.cssSelector(randomDateAndTime)).click(); //fix
        if(randomDateAndTime.equals(asapSelector)) {
            System.out.println("DATE & TIME: ASAP selected");
        }
        else {
            System.out.println("I need to add more 'Later' specific tests here...");
            //driver.findElement(By.xpath("//input[contains(@class, 'hasDatepicker')]")).sendKeys("19/08/2017 00:00");
            System.out.println("DATE & TIME: Later selected");
        }
}
public class makeBooking extends makeBookingBase {

    @Test
    public void makeNewBooking() throws Throwable {

        for (String scope: vehicleType) {
            openEnvironment();
            selectOnBehalfOf("Tommy");
            selectLeadPassenger("Bobby");
            selectNumberOfPassengers();
            selectJourneyTypeRadio();
            selectDateTimeRadio(); //THE METHOD IM STUCK WITH IS CALLED HERE!
                if(!scope.equals("disabled")) {
                    selectVehicleType(scope);
                }
                else {
                    selectVehicleType(scope);
                    selectWheelchairType();
                }
            clickReviewButton();
        }
    }
}
在我的测试用例中,我扩展了基类,并调用了方法,如下所示

protected void selectDateTimeRadio() {
    driver.findElement(By.cssSelector(randomDateAndTime)).click(); //fix
        if(randomDateAndTime.equals(asapSelector)) {
            System.out.println("DATE & TIME: ASAP selected");
        }
        else {
            System.out.println("I need to add more 'Later' specific tests here...");
            //driver.findElement(By.xpath("//input[contains(@class, 'hasDatepicker')]")).sendKeys("19/08/2017 00:00");
            System.out.println("DATE & TIME: Later selected");
        }
}
public class makeBooking extends makeBookingBase {

    @Test
    public void makeNewBooking() throws Throwable {

        for (String scope: vehicleType) {
            openEnvironment();
            selectOnBehalfOf("Tommy");
            selectLeadPassenger("Bobby");
            selectNumberOfPassengers();
            selectJourneyTypeRadio();
            selectDateTimeRadio(); //THE METHOD IM STUCK WITH IS CALLED HERE!
                if(!scope.equals("disabled")) {
                    selectVehicleType(scope);
                }
                else {
                    selectVehicleType(scope);
                    selectWheelchairType();
                }
            clickReviewButton();
        }
    }
}
如上所述,我遇到了一个问题,即每个循环(本例中的vehicleType)都使用相同的数组值(本例中的作用域)

是否有方法为循环的每个实例生成数组的新值?如果可能的话,请你在下面解释一下


谢谢。

我想
randomDateAndTime
有一个类变量,它的值在类启动后就设置好了,它的值也会相同。我建议将该变量放入一个方法中,并在每次调用该方法时返回随机值


希望这有帮助。谢谢。

我想randomDateAndTime是一个类变量,它的值在类启动后设置,并且它将具有相同的值。你能把这个变量放在一个方法中,并在每次调用该方法时返回随机数吗?@santhoshkumar谢谢!多么简单的解决方案,效果很好:)我。不确定我是否理解,但驱动程序有找到多个元素的方法,您可以随机选择它们。驱动程序数据项(按类别(“radioDate”))