繁体   English   中英

Selenium Webdriver循环

[英]Selenium Webdriver Loop

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

我有一个问题,当在循环中调用此数组时,每次都会从数组中选择相同的值。

这是有问题的数组-它位于基类中。

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();
        }
    }
}

如前所述,我遇到了一个问题,即每个循环(此实例中的vehicleType)使用数组中的相同值(在此实例中为scope)

有没有一种方法可以为循环的每个实例生成数组的新值? 如果可能,请您在下面说明。

谢谢。

我猜randomDateAndTime有一个类变量,一旦启动该类,就已经设置了它的值,它将彻底具有相同的值。 我建议将该变量放入方法中,并在每次调用该方法时返回随机值。

希望这可以帮助。 谢谢。

暂无
暂无

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

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