2

I'm new to coding and I have the following issue while automating with Selenium using Java:

I'm testing a SaaS solution and I need to refresh my page until a certain item appears in my inbox. I cannot simply use the findelement statement since the item only appears after some time and onlywhen the page has been refreshed. Furthermore all inbox items have a unique sequential number in the title. This unique number I have saved as a string variable and I want to use this string variable to see if the inbox item has appeared after refreshing the page some times.

Code for finding unique string:

//Get expense report number
    String filename = strng;
    String ExpenseReportNumber = StringUtils.substringBefore(filename, " submitted"); // returns Expense Report #XXXX

Now I need to create a loop in which the site keeps refreshing until it gets a hit on the string variable. When it finds the string variable I then can select the top element on the inbox and continue my test.

Can someone assist me with this issue or give me advice on how to create the same outcome but then with a better approach? Much appreciated!

MJM1987MJM
  • 31
  • 1
  • 4

3 Answers3

1
Wait<WebDriver> wait12 = new FluentWait<WebDriver>(driver)
            .withTimeout(600, TimeUnit.SECONDS)
            .pollingEvery(15, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement ToDoExpense = wait12.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            driver.navigate().refresh();
            return driver.findElement(By.partialLinkText(ExpenseReportNumber));
                              }
    });

I have found the solution myself

MJM1987MJM
  • 31
  • 1
  • 4
  • 1
    Please be sure to accept this answer when you can so that the question gets marked as answered. Thanks! – JeffC Nov 11 '15 at 07:04
  • This ends up in the page either looping constantly - or a StaleElementReferenceException so can not be considered an answer. – user3259647 Nov 21 '17 at 22:08
1
public WebDriverWait wait = new WebDriverWait(driver, 10); //throws exception if element is not found within 10 seconds
driver.navigate().refresh();
wait.until(ExpectedConditions.presenceOfElementLocated(By.partialLinkText(ExpenseReportNumber)));
//continue your test
Shaz
  • 597
  • 4
  • 5
0

I worked it out like this:

do
{
    driver.refresh();
    driver.waitForPageToLoad();
    driver.wait(1000);
} while (driver.findElements(By.locator(element)).size() < 1);
Zymus
  • 1,673
  • 1
  • 18
  • 38