Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/main/java/io/github/lambdatest/SmartUIResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public static JSONObject smartuiResults(WebDriver driver) throws Exception {

try {
// Extract sessionId from the driver (null-safe)
org.openqa.selenium.remote.SessionId sid = ((org.openqa.selenium.remote.RemoteWebDriver) driver).getSessionId();
org.openqa.selenium.remote.RemoteWebDriver remoteDriver = SmartUIUtil.unwrapRemoteWebDriver(driver);
if (remoteDriver == null) {
throw new IllegalArgumentException("Driver must be a RemoteWebDriver, or wrap one, to extract sessionId");
}
org.openqa.selenium.remote.SessionId sid = remoteDriver.getSessionId();
if (sid == null) {
throw new IllegalStateException("Unable to get sessionId from the driver");
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/github/lambdatest/SmartUISnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public static JSONObject smartuiSnapshot(WebDriver driver, String snapshotName,
((JavascriptExecutor) driver).executeScript(domString);

// Append sessionId to options
String sessionId = ((org.openqa.selenium.remote.RemoteWebDriver) driver).getSessionId().toString();
if (!sessionId.isEmpty()) {
options.put("sessionId", sessionId);
org.openqa.selenium.remote.RemoteWebDriver remoteDriver = SmartUIUtil.unwrapRemoteWebDriver(driver);
if (remoteDriver != null && remoteDriver.getSessionId() != null) {
options.put("sessionId", remoteDriver.getSessionId().toString());
} else {
log.fine("Driver is not a RemoteWebDriver; snapshot sent without sessionId");
}

// Resolve any WebElement objects in element/ignoreDOM/selectDOM to CSS selectors
Expand Down Expand Up @@ -143,7 +145,7 @@ public static JSONObject smartuiSnapshot(WebDriver driver, String snapshotName,
}

} catch (Exception e) {
log.severe(String.format(Constants.Errors.SMARTUI_SNAPSHOT_FAILED, snapshotName));
log.severe(Constants.Errors.SMARTUI_SNAPSHOT_FAILED + " for '" + snapshotName + "': " + e);
return null;
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/github/lambdatest/utils/SmartUIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import io.github.lambdatest.models.*;
import com.google.gson.Gson;
import io.github.lambdatest.constants.Constants;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.decorators.Decorated;


public class SmartUIUtil {
Expand All @@ -30,6 +34,24 @@ public SmartUIUtil(String proxyProtocol, String proxyHost, int proxyPort, boolea
this.httpClient = new HttpClientUtil(proxyProtocol, proxyHost, proxyPort, allowInsecure);
}

// Selenium decorators (EventFiringDecorator) hand back a proxy that is not a RemoteWebDriver; unwrap until the real one appears
public static RemoteWebDriver unwrapRemoteWebDriver(WebDriver driver) {
Object current = driver;
for (int depth = 0; current != null && depth < 8; depth++) {
if (current instanceof RemoteWebDriver) {
return (RemoteWebDriver) current;
}
if (current instanceof WrapsDriver) {
current = ((WrapsDriver) current).getWrappedDriver();
} else if (current instanceof Decorated) {
current = ((Decorated<?>) current).getOriginal();
} else {
return null;
}
}
return null;
}

public boolean isSmartUIRunning() {
try {
httpClient.isSmartUIRunning();
Expand Down