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

Various fixes to align the pw implementation with Selenium's expected… #109

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
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
50 changes: 36 additions & 14 deletions src/main/java/org/brit/driver/PlaywrightiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;

import java.lang.reflect.Field;
import java.net.URL;
Expand Down Expand Up @@ -210,7 +211,12 @@ public String getCurrentUrl() {

@Override
public String getTitle() {
return page.title();
// Selenide uses getTitle() to check if the browser is alive, so it will be nice to properly re-throw the expected exception.
try {
return page.title();
} catch (PlaywrightException e) {
throw new UnreachableBrowserException(e.getMessage(), e);
}
}

@Override
Expand Down Expand Up @@ -364,14 +370,11 @@ public Set<String> getWindowHandles() {
}
List<Page> pages = PlaywrightiumDriver.this.page.context().pages();
for (Page pageTemp : pages) {
Field guid = null;
try {
guid = pageTemp.getClass().getSuperclass().getDeclaredField("guid");
Field guid = pageTemp.getClass().getSuperclass().getDeclaredField("guid");
guid.setAccessible(true);
handles.add(guid.get(pageTemp).toString());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -549,25 +552,42 @@ public class PlaywrightWebdriverTargetLocator implements TargetLocator {

@Override
public WebDriver frame(int index) {
Frame frame = page.frames().get(index);
Frame frame;
try {
frame = page.frames().get(index);
} catch (IndexOutOfBoundsException | TimeoutError e) {
throw new NoSuchFrameException("No frame at index " + index);
}
setMainFrame(frame);
return PlaywrightiumDriver.this;
}

@Override
public WebDriver frame(String nameOrId) {
Locator frameLocator = page.locator("[name='%s'], #%s".formatted(nameOrId, nameOrId));
Frame frame = page.frame(frameLocator.getAttribute("name"));
Frame frame;
try {
Locator frameLocator = page.locator("[name='%s'], #%s".formatted(nameOrId, nameOrId));
frame = page.frame(frameLocator.getAttribute("name"));
} catch (TimeoutError e) {
throw new NoSuchFrameException(nameOrId);
}
setMainFrame(frame);
return PlaywrightiumDriver.this;
}

@Override
public WebDriver frame(WebElement frameElement) {
PlaywrightWebElement element = (PlaywrightWebElement) frameElement;
String nameOrId = element.getLocator().getAttribute("id");
if (nameOrId == null) {
nameOrId = element.getLocator().getAttribute("name");
String nameOrId;
try {
nameOrId = frameElement.getAttribute("id");
if (nameOrId == null || nameOrId.isEmpty()) {
nameOrId = frameElement.getAttribute("name");
}
} catch (TimeoutError e) {
throw new NoSuchFrameException(frameElement.toString(), e);
}
if (nameOrId == null || nameOrId.isEmpty() ) {
throw new NoSuchFrameException(frameElement.toString());
}
return frame(nameOrId);
}
Expand Down Expand Up @@ -602,12 +622,14 @@ public WebDriver window(String nameOrHandle) {
if (guid.get(pageElement).toString().equals(nameOrHandle) || nameOrHandle.equals(evaluate)) {
pageElement.bringToFront();
PlaywrightiumDriver.this.page = pageElement;
return PlaywrightiumDriver.this;
}

} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return PlaywrightiumDriver.this;
throw new NoSuchWindowException("No such window: " + nameOrHandle);
}

@Override
Expand Down
Loading