Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#101 driver.findElement() and element.findElement() must throw NoSuchElementException if element is not found. #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/org/brit/driver/PlaywrightiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.brit.options.TracingOptions;
import org.brit.permission.Permissions;
import org.openqa.selenium.*;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.interactions.Interactive;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.logging.Logs;
Expand All @@ -33,6 +34,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.microsoft.playwright.options.WaitForSelectorState.ATTACHED;

public class PlaywrightiumDriver extends RemoteWebDriver implements TakesScreenshot, Interactive {
private Playwright playwright;
private BrowserContext browserContext;
Expand All @@ -42,7 +45,7 @@ public class PlaywrightiumDriver extends RemoteWebDriver implements TakesScreens
private Page activePage;

private Frame mainFrameCopy = null;

private static final Locator.WaitForOptions elementExists = new Locator.WaitForOptions().setState(ATTACHED);
private PlaywrightiumOptions options;


Expand Down Expand Up @@ -222,7 +225,12 @@ public List<WebElement> findElements(By by) {

@Override
public WebElement findElement(By by) {
return new PlaywrightWebElement(getLocatorFromBy(by).first());
try {
getLocatorFromBy(by).first().waitFor(elementExists);
} catch (TimeoutError e) {
throw new NoSuchElementException("Unable to locate element: " + by, e);
}
return new PlaywrightWebElement(getLocatorFromBy(by).first());
}

private Locator getLocatorFromBy(By by) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/brit/element/PlaywrightWebElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.microsoft.playwright.options.WaitForSelectorState.ATTACHED;

/**
* @author Serhii Bryt
* WebElement implementation
* @see org.openqa.selenium.WebElement
*/
public class PlaywrightWebElement extends RemoteWebElement {

private static final Locator.WaitForOptions elementExists = new Locator.WaitForOptions().setState(ATTACHED);
Locator locator;
ElementHandle elementHandle;

Expand Down Expand Up @@ -138,6 +141,11 @@ public List<WebElement> findElements(By by) {

@Override
public WebElement findElement(By by) {
try {
getLocatorFromBy(by).first().waitFor(elementExists);
} catch (TimeoutError e) {
throw new NoSuchElementException("Unable to locate element: " + by, e);
}
return new PlaywrightWebElement(getLocatorFromBy(by).first());
}

Expand Down
Loading