0

In one of the application workflows I have more than 10 pages to navigate.

I have to keep clicking on the 'Next' button continuously - it makes an AJAX call to re-load new content and 'Next' button will also be reloaded. The number of pages to navigate is not always 10. It might be anything from 10-100.

My test will be complete whenever there is a webelement found with the id 'testcomplete'.

So Currently i use ExpectedConditions()

WebDriverWait wait = new WebDriverWait(driver, 30);

//Keep clicking next
while(isNextPresent()){
        NextButton.click();
}

//testcomplete reached here
System.out.println("test complete");




private boolean isNextPresent(){
    try{
        WebElement element = wait.until(ExpectedConditions.visibilityOf(NextButton));
        return true;
    }catch(Exception e){
        //
    }
    return false;
}

Currently my code works fine. But i am trying to improve it. I hate the unnecessary wait of 30 seconds when the element with the id 'testcomplete' is present. Because that time 'NextButton' will not be present.

How can I improve this isNextPresent function? Ie, to return false immediately when there is 'testcomplete' instead of waiting for 30 seconds?

Note: I have tagged protractor as well because I also have a similar requirement in protractor.

KitKarson
  • 5,211
  • 10
  • 51
  • 73
  • What do you mean "the unnecessary wait of 30 seconds"? Doesn't that function execute instantly after `visibilityOf` returns true, with a maximum timeout of 30 seconds? – Gunderson May 17 '16 at 19:51
  • @Gunderson, as i had mentioned, it works fine. But in the end, at one point, next button will not be there. instead there will be another element called testcomplete. That time it waits for 30 seconds. – KitKarson May 17 '16 at 19:55

2 Answers2

1

You can combine the conditions of both elements and take an action depending on the fact which one first returns true for 'visibilityOf(myElement)'. Maybe something like this in pseudo (sorry, no IDE around):

loop(i < 30){
      // wait NextBtn for 1 sec, if true click and break
      // wait TestCopmlete for 1 sec
}
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
  • Now I see. Your function is doing what it should. IMHO you should use another one to poll the DOM for the other element as well. Possible solution is wrapping both checks in a loop and checking each condition every second or so. – ekostadinov May 17 '16 at 20:14
  • Yes, Thanks for your input. I was just curious if there is any easy way to do this. – KitKarson May 17 '16 at 20:21
  • I think my question is duplicate of http://stackoverflow.com/questions/14840884/wait-untilexpectedconditions-visibilityof-element1-or-element2 – KitKarson May 17 '16 at 20:40
1

Use EC.or(), a la:

wait.until(ExpectedConditions.or(
    ExpectedConditions.visibilityOf(NextButton),
    ExpectedConditions.visibilityOf(element(by.id('testcomplete')))
));

Then after this comes back, expect the desired state:

expect(NextButton.isDisplayed()).toBeTruthy();
Keith Tyler
  • 719
  • 4
  • 18