简体   繁体   中英

Selenium Webdriver Loop

I have an array within my test case which is then passed into another string to select a random item from the array.

I'm having a problem whereby when this array is called within a loop, the same value from the array is chosen each time.

Here is the array in question - this sits within a base class.

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

Also, the method which calls 'randomDateAndTime' sits within the base class and looks like

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

Within my test case, I extend the base class, and call the method, which looks like

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

As previously mentioned above, I'm having a problem whereby the same value from the array (scope in this instance) is being used for each loop (vehicleType in this instance)

Is there a way to generate a new value of the array for each instance of the loop? If possible, can you please explain below.

Thanks.

I guess randomDateAndTime have a class variable and its value have been set once the class has been initiated and it will have the same value thorough. I suggest to put that variable inside a method and return the random every time the method has been called.

Hope this helps. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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