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

selenium: allow to log browser's console.log #6102

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
4 changes: 4 additions & 0 deletions addOns/selenium/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Allow to log browser's `console.log`, done at DEBUG level with the name `org.zaproxy.webdriver`.

### Changed
- Use WebDriver BiDi with Chrome.

## [15.32.0] - 2025-01-10
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
import org.parosproxy.paros.Constant;
Expand Down Expand Up @@ -92,6 +93,10 @@ public class ExtensionSelenium extends ExtensionAdaptor {

private static final Logger LOGGER = LogManager.getLogger(ExtensionSelenium.class);

private static final Logger WEBDRIVER_LOGGER = LogManager.getLogger("org.zaproxy.webdriver");

private static final String BIDI_CAPABILITIY = "webSocketUrl";

private static final List<Class<? extends Extension>> EXTENSION_DEPENDENCIES =
List.of(ExtensionNetwork.class);

Expand Down Expand Up @@ -1037,6 +1042,11 @@ private static void addChromeExtensions(ChromeOptions options) {
.collect(Collectors.toList()));
}

private static RemoteWebDriver configureDriver(RemoteWebDriver driver) {
driver.script().addConsoleMessageHandler(e -> WEBDRIVER_LOGGER.debug(e.getText()));
return driver;
}

private static WebDriver getWebDriverImpl(
int requester,
Browser browser,
Expand All @@ -1048,6 +1058,7 @@ private static WebDriver getWebDriverImpl(
case CHROME:
case CHROME_HEADLESS:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability(BIDI_CAPABILITIY, true);
if (enableExtensions) {
addChromeExtensions(chromeOptions);
}
Expand All @@ -1064,12 +1075,12 @@ private static WebDriver getWebDriverImpl(

addChromeArguments(chromeOptions);
consumer.accept(chromeOptions);
return new ChromeDriver(chromeOptions);
return configureDriver(new ChromeDriver(chromeOptions));
case FIREFOX:
case FIREFOX_HEADLESS:
FirefoxOptions firefoxOptions = new FirefoxOptions();
// Use WebDriver BiDi
firefoxOptions.setCapability("webSocketUrl", true);
firefoxOptions.setCapability(BIDI_CAPABILITIY, true);
// Force the use of just BiDi, should not be required once Selenium stops
// adding the preference https://github.com/SeleniumHQ/selenium/issues/14885
firefoxOptions.addPreference("remote.active-protocols", "1");
Expand Down Expand Up @@ -1141,7 +1152,7 @@ private static WebDriver getWebDriverImpl(
if (enableExtensions) {
addFirefoxExtensions(driver);
}
return driver;
return configureDriver(driver);
case HTML_UNIT:
DesiredCapabilities htmlunitCapabilities = new DesiredCapabilities();
setCommonOptions(htmlunitCapabilities, proxyAddress, proxyPort);
Expand Down
Loading