Skip to content

Commit

Permalink
Merge pull request #4712 from edalex-yinzi/fix/selenium-test
Browse files Browse the repository at this point in the history
Address issue with 'no node for id' errors due to bug in Chrome Driver.

OEQ-1520
  • Loading branch information
edalex-ian committed Jun 16, 2023
2 parents 8b35b3b + 223ddf0 commit fabfd6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
Expand All @@ -34,10 +35,25 @@ public static ExpectedCondition<Boolean> updateOfElement(WebElement elem) {
return ExpectedConditions.stalenessOf(element);
}

/**
* Creates an {@code ExpectedCondition} which attempts to check if the provided {@code element} is
* displayed, however if that element has become stale, then will use the provided {@code context}
* and {@code locator} to find a target element and check if it is displayed/visible.
*
* @param element a previously found element which will initially be used to see if the target is
* displayed.
* @param context a context to use when {@code element} is stale, to search for target element
* with {@code locator}.
* @param locator a {@code By} instance used to find the target element with {@code context} for
* when {@code element} is stale.
* @return an {@code ExpectedCondition} to check if the target element is displayed/visible.
*/
public static ExpectedCondition<WebElement> updateOfElementLocated(
WebElement element, final SearchContext context, final By locator) {
final WebElement realElement = unwrappedElement(element);
return new ExpectedCondition<WebElement>() {
// Provides the means to toggle between first checking if 'element' is stale, and then if it
// becomes stale later using 'context' and 'locator'.
private boolean checkingStale = true;

@Override
Expand All @@ -46,7 +62,9 @@ public WebElement apply(WebDriver driver) {
try {
realElement.isDisplayed();
return null;
} catch (StaleElementReferenceException se) {
} catch (WebDriverException se) {
// Once the element has become stale, then the provided element/realElement is no longer
// used and every check will instead now rely on context and locator down below.
checkingStale = false;
}
}
Expand Down Expand Up @@ -165,9 +183,7 @@ public Boolean apply(WebDriver driver) {
try {
realElement.isDisplayed();
return false;
} catch (StaleElementReferenceException ser) {
return true;
} catch (NoSuchElementException e) {
} catch (WebDriverException ser) {
return true;
}
}
Expand Down Expand Up @@ -298,7 +314,7 @@ public WebElement apply(WebDriver driver) {
try {
realElement.isDisplayed();
return null;
} catch (StaleElementReferenceException se) {
} catch (WebDriverException se) {
checkingStale = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tle.webtests.framework.PageContext;
import com.tle.webtests.pageobject.AbstractPage;
import com.tle.webtests.pageobject.ExpectWaiter;
import com.tle.webtests.pageobject.ExpectedConditions2;
import com.tle.webtests.pageobject.PrefixedName;
import com.tle.webtests.pageobject.WaitingPageObject;
import java.util.ArrayList;
Expand Down Expand Up @@ -36,11 +37,11 @@ public WaitingPageObject<T> getUpdateWaiter() {
((WrapsElement) getResultsDiv().findElement(By.xpath("*[1]"))).getWrappedElement();
return ExpectWaiter.waiter(
ExpectedConditions.and(
ExpectedConditions.stalenessOf(firstChild),
ExpectedConditions2.stalenessOrNonPresenceOf(firstChild),
ExpectedConditions.visibilityOfElementLocated(
By.xpath("id('searchresults')[div[@class='itemlist'] or h3]"))),
this);
}
};

protected static String getXPathForTitle(String title) {
return "//div/div[contains(@class,'itemresult-wrapper') and .//h3/a[normalize-space(string())="
Expand Down

0 comments on commit fabfd6c

Please sign in to comment.