简体   繁体   中英

Boolean method to check value of an Web element

I need to test a dynamic Web Element using Selenium that will change its value after some time(back-end dependent). So, I built a boolean method that returns true if the web element has the value I need and false if that value is never retrieved. I want to check for value change at some intervals (thus, the Thread.sleep between page refreshes). My code always returns true, what am I doing wrong?

public boolean checkStatus() throws InterruptedException {

    for(int i=0; i<2;) {
        if (!serviceStatus.serviceElement().equals("LIVE")) {
            Thread.sleep(5000);
            theBrowser.navigate().refresh();
            i++;
        }else{
            return true;
        }
    }
    return false;
}

The method is used in the main test on the following assert:

    Ensure.that(checkStatus()).isTrue();

I presume serviceElement() is the Webelement,you have to get the text for the element using getText() method and compare with the string you desire("LIVE") in this scenario.You seems to be comparing an Webelement with String here. Below should work if i understood the requirement correctly.

public boolean checkStatus() throws InterruptedException {
      boolean isMatching = false;
    for(int i=0; i<2;) {
        if (!serviceStatus.serviceElement().getText().equals("LIVE")) {
            Thread.sleep(5000);
            theBrowser.navigate().refresh();
            i++;
            isMatching = false;
            break;

        }else{
            isMatching = true;
        }
    }
    return isMatching;
}

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