From 83dd7cb91b0ea8241b469fc3d112945fe72ad7cb Mon Sep 17 00:00:00 2001 From: Olha Omelianchuk Date: Mon, 6 Sep 2021 16:11:40 +0200 Subject: [PATCH] #198 improve support of driver arguments --- .../multibrowser/wrappers/ChromeBuilder.java | 54 ++++++++++++ .../multibrowser/wrappers/GeckoBuilder.java | 83 +++++++++++++++++++ .../multibrowser/wrappers/IEBuilder.java | 55 ++++++++++++ .../multibrowser/wrappers/SafariBuilder.java | 46 ++++++++++ 4 files changed, 238 insertions(+) create mode 100644 src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/ChromeBuilder.java create mode 100644 src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/GeckoBuilder.java create mode 100644 src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/IEBuilder.java create mode 100644 src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/SafariBuilder.java diff --git a/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/ChromeBuilder.java b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/ChromeBuilder.java new file mode 100644 index 000000000..637618e9e --- /dev/null +++ b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/ChromeBuilder.java @@ -0,0 +1,54 @@ +package com.xceptance.neodymium.module.statement.browser.multibrowser.wrappers; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.openqa.selenium.chrome.ChromeDriverService; +import org.openqa.selenium.chrome.ChromeDriverService.Builder; +import org.openqa.selenium.net.PortProber; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class ChromeBuilder extends Builder +{ + private File firefoxBinary; + + private int port; + + private List arguments; + + public ChromeDriverService createDriverService(List arguments) + { + firefoxBinary = findDefaultExecutable(); + port = PortProber.findFreePort(); + this.arguments = arguments; + try + { + return new ChromeDriverService(firefoxBinary, port, createArgs(), ImmutableMap.copyOf(System.getenv())); + } + catch (IOException e) + { + e.printStackTrace(); + return null; + } + } + + @Override + protected ImmutableList createArgs() + { + ImmutableList.Builder argsBuilder = ImmutableList.builder(); + argsBuilder.addAll(super.createArgs()); + argsBuilder.add(String.format("--port=%d", port)); + if (firefoxBinary != null) + { + argsBuilder.add("-b"); + argsBuilder.add(firefoxBinary.getPath()); + } // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability. + if (arguments != null) + { + argsBuilder.addAll(arguments); + } + return argsBuilder.build(); + } +} diff --git a/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/GeckoBuilder.java b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/GeckoBuilder.java new file mode 100644 index 000000000..e6d1cfe55 --- /dev/null +++ b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/GeckoBuilder.java @@ -0,0 +1,83 @@ +package com.xceptance.neodymium.module.statement.browser.multibrowser.wrappers; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +import org.openqa.selenium.firefox.GeckoDriverService; +import org.openqa.selenium.firefox.GeckoDriverService.Builder; +import org.openqa.selenium.net.PortProber; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteStreams; + +public class GeckoBuilder extends Builder +{ + private File firefoxBinary; + + private int port; + + private List arguments; + + public GeckoDriverService createDriverService(List arguments) + { + firefoxBinary = findDefaultExecutable(); + port = PortProber.findFreePort(); + this.arguments = arguments; + var logPathArgs = arguments.stream().filter(arg -> arg.contains("--log-path")).collect(Collectors.toList()); + this.arguments.removeAll(logPathArgs); + var service = createDriverService(firefoxBinary, port, createArgs(), ImmutableMap.copyOf(System.getenv())); + + if (!logPathArgs.isEmpty()) + { + String firefoxLogFile = logPathArgs.get(0).replaceAll("--log-path=", "").trim(); + if (firefoxLogFile != null) + { + if ("/dev/stdout".equals(firefoxLogFile)) + { + service.sendOutputTo(System.out); + } + else if ("/dev/stderr".equals(firefoxLogFile)) + { + service.sendOutputTo(System.err); + } + else if ("/dev/null".equals(firefoxLogFile)) + { + service.sendOutputTo(ByteStreams.nullOutputStream()); + } + else + { + try + { + service.sendOutputTo(new FileOutputStream(firefoxLogFile)); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + } + return service; + } + + @Override + protected ImmutableList createArgs() + { + ImmutableList.Builder argsBuilder = ImmutableList.builder(); + // argsBuilder.addAll(super.createArgs()); + argsBuilder.add(String.format("--port=%d", port)); + if (firefoxBinary != null) + { + argsBuilder.add("-b"); + argsBuilder.add(firefoxBinary.getPath()); + } // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability. + if (arguments != null && !arguments.isEmpty()) + { + argsBuilder.addAll(arguments); + } + return argsBuilder.build(); + } +} diff --git a/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/IEBuilder.java b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/IEBuilder.java new file mode 100644 index 000000000..0934613ab --- /dev/null +++ b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/IEBuilder.java @@ -0,0 +1,55 @@ +package com.xceptance.neodymium.module.statement.browser.multibrowser.wrappers; + +import java.io.File; +import java.util.List; + +import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.ie.InternetExplorerDriverService; +import org.openqa.selenium.ie.InternetExplorerDriverService.Builder; +import org.openqa.selenium.net.PortProber; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class IEBuilder extends Builder +{ + private File firefoxBinary; + + private int port; + + private List arguments; + + public InternetExplorerDriverService createDriverService(List arguments) + { + firefoxBinary = findDefaultExecutable(); + port = PortProber.findFreePort(); + this.arguments = arguments; + try + { + return createDriverService(firefoxBinary, port, createArgs(), ImmutableMap.copyOf(System.getenv())); + } + catch (WebDriverException e) + { + e.printStackTrace(); + return null; + } + } + + @Override + protected ImmutableList createArgs() + { + ImmutableList.Builder argsBuilder = ImmutableList.builder(); + argsBuilder.addAll(super.createArgs()); + argsBuilder.add(String.format("--port=%d", port)); + if (firefoxBinary != null) + { + argsBuilder.add("-b"); + argsBuilder.add(firefoxBinary.getPath()); + } // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability. + if (arguments != null) + { + argsBuilder.addAll(arguments); + } + return argsBuilder.build(); + } +} diff --git a/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/SafariBuilder.java b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/SafariBuilder.java new file mode 100644 index 000000000..daaa9d334 --- /dev/null +++ b/src/main/java/com/xceptance/neodymium/module/statement/browser/multibrowser/wrappers/SafariBuilder.java @@ -0,0 +1,46 @@ +package com.xceptance.neodymium.module.statement.browser.multibrowser.wrappers; + +import java.io.File; +import java.util.List; + +import org.openqa.selenium.net.PortProber; +import org.openqa.selenium.safari.SafariDriverService; +import org.openqa.selenium.safari.SafariDriverService.Builder; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class SafariBuilder extends Builder +{ + private File firefoxBinary; + + private int port; + + private List arguments; + + public SafariDriverService createDriverService(List arguments) + { + firefoxBinary = findDefaultExecutable(); + port = PortProber.findFreePort(); + this.arguments = arguments; + return createDriverService(firefoxBinary, port, createArgs(), ImmutableMap.copyOf(System.getenv())); + } + + @Override + protected ImmutableList createArgs() + { + ImmutableList.Builder argsBuilder = ImmutableList.builder(); + argsBuilder.addAll(super.createArgs()); + argsBuilder.add(String.format("--port=%d", port)); + if (firefoxBinary != null) + { + argsBuilder.add("-b"); + argsBuilder.add(firefoxBinary.getPath()); + } // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability. + if (arguments != null) + { + argsBuilder.addAll(arguments); + } + return argsBuilder.build(); + } +}