From 42368c0b87d12324db2bf4255bb5ccaf3eaca425 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Sat, 26 Sep 2020 18:54:46 +0300 Subject: [PATCH] Using Options classes directly instead of DesiredCapabilities --- .../java/com/aerokube/selenoid/TestProxy.java | 19 +++---- .../com/aerokube/selenoid/misc/TestBase.java | 30 +++++------ .../aerokube/selenoid/misc/WebDriverRule.java | 54 +++++++++---------- 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/test/java/com/aerokube/selenoid/TestProxy.java b/src/test/java/com/aerokube/selenoid/TestProxy.java index 1ff6e44..8699c35 100644 --- a/src/test/java/com/aerokube/selenoid/TestProxy.java +++ b/src/test/java/com/aerokube/selenoid/TestProxy.java @@ -7,6 +7,7 @@ import net.lightbody.bmp.core.har.HarEntry; import net.lightbody.bmp.proxy.CaptureType; import org.junit.*; +import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.Proxy; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; @@ -20,9 +21,9 @@ import static org.junit.Assert.assertThat; public class TestProxy extends TestBase { - + private static final BrowserMobProxy proxy = new BrowserMobProxyServer(); - + @BeforeClass public static void initProxy(){ if (!proxy.isStarted()) { @@ -30,14 +31,14 @@ public static void initProxy(){ proxy.setHarCaptureTypes(CaptureType.REQUEST_HEADERS); } } - + @Before public void startHar(){ if (proxy.isStarted()) { proxy.newHar(); } } - + @Features("Using proxies") @Test public void testProxy() throws Exception { @@ -52,7 +53,7 @@ public void clearHar() { proxy.endHar(); } } - + @AfterClass public static void shutdownProxy() { if (proxy.isStarted()) { @@ -61,11 +62,11 @@ public static void shutdownProxy() { } @Override - protected Function getCapabilitiesProcessor() { + protected Function getCapabilitiesProcessor() { return dc -> new DesiredCapabilities(dc, getProxyCapabilities(dc)); } - private DesiredCapabilities getProxyCapabilities(DesiredCapabilities caps) { + private DesiredCapabilities getProxyCapabilities(MutableCapabilities caps) { DesiredCapabilities ret = new DesiredCapabilities(); String proxyString = getProxyString(); Proxy proxy = new Proxy(); @@ -82,7 +83,7 @@ private DesiredCapabilities getProxyCapabilities(DesiredCapabilities caps) { switches.add("--proxy-server=" + proxyString); caps.setCapability("chrome.switches", switches); } - + ret.setCapability(CapabilityType.PROXY, proxy); return ret; } @@ -90,5 +91,5 @@ private DesiredCapabilities getProxyCapabilities(DesiredCapabilities caps) { private String getProxyString() { return String.format("%s:%d", getLocalHost(), proxy.getPort()); } - + } diff --git a/src/test/java/com/aerokube/selenoid/misc/TestBase.java b/src/test/java/com/aerokube/selenoid/misc/TestBase.java index 2d207a3..3bd786f 100644 --- a/src/test/java/com/aerokube/selenoid/misc/TestBase.java +++ b/src/test/java/com/aerokube/selenoid/misc/TestBase.java @@ -6,8 +6,8 @@ import org.junit.rules.TestRule; import org.junit.rules.Timeout; import org.openqa.selenium.By; +import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; @@ -21,40 +21,40 @@ import java.util.function.Function; public abstract class TestBase { - + private static final Logger LOG = LoggerFactory.getLogger(TestBase.class); - + @Rule public TestRule chain; - + private Timeout timeout; private WebDriverRule webDriverRule; - + public TestBase() { this.webDriverRule = new WebDriverRule(getCapabilitiesProcessor()); this.timeout = new Timeout(30, TimeUnit.SECONDS); this.chain = RuleChain.outerRule(timeout).around(webDriverRule); } - + public WebDriver getDriver() { return webDriverRule.getDriver(); } - + public void waitUntilElementIsPresent(By by) { new WebDriverWait(getDriver(), 5).until(ExpectedConditions.presenceOfElementLocated(by)); } - + public String getPageTitle() { new WebDriverWait(getDriver(), 5).until(ExpectedConditions.not(ExpectedConditions.titleIs(""))); return getDriver().getTitle(); } - + public void openPage(Page page) throws Exception { String pageUrl = webDriverRule.getPageUrl(page); LOG.info(String.format("Opening page at: %s", pageUrl)); getDriver().get(pageUrl); } - + public void fail(String message, Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); @@ -63,15 +63,15 @@ public void fail(String message, Exception e) { String msg = String.format("%s: %s", message, exceptionMessage); fail(msg); } - + public void fail(String msg) { Assert.fail(msg); } - - protected Function getCapabilitiesProcessor() { + + protected Function getCapabilitiesProcessor() { return Function.identity(); } - + protected String getLocalHost() { try { if (System.getProperty("os.name").startsWith("Mac")) { @@ -84,5 +84,5 @@ protected String getLocalHost() { return "localhost"; } } - + } diff --git a/src/test/java/com/aerokube/selenoid/misc/WebDriverRule.java b/src/test/java/com/aerokube/selenoid/misc/WebDriverRule.java index af0ab00..9d77a63 100644 --- a/src/test/java/com/aerokube/selenoid/misc/WebDriverRule.java +++ b/src/test/java/com/aerokube/selenoid/misc/WebDriverRule.java @@ -3,13 +3,11 @@ import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; -import org.openqa.selenium.OutputType; -import org.openqa.selenium.Platform; -import org.openqa.selenium.TakesScreenshot; -import org.openqa.selenium.WebDriver; +import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.opera.OperaOptions; import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import ru.qatools.properties.PropertyLoader; @@ -21,20 +19,20 @@ import java.util.function.Function; public class WebDriverRule implements TestRule { - + private static final TestProperties PROPERTIES = PropertyLoader.newInstance().populate(TestProperties.class); private static final String CHROME = "chrome"; private static final String YANDEX = "yandex"; private static final String OPERA = "opera"; - + private WebDriver driver; - - private final Function capabilitiesProcessor; - - WebDriverRule(Function capabilitiesProcessor) { + + private final Function capabilitiesProcessor; + + WebDriverRule(Function capabilitiesProcessor) { this.capabilitiesProcessor = capabilitiesProcessor; } - + @Override public Statement apply(Statement base, Description description) { return new Statement() { @@ -56,49 +54,49 @@ public void evaluate() throws Throwable { } }; } - + private URL getConnectionUrl() throws MalformedURLException { return new URL(PROPERTIES.getConnectionUrl()); } - - private DesiredCapabilities getDesiredCapabilities() { - DesiredCapabilities caps = new DesiredCapabilities(PROPERTIES.getBrowserName(), PROPERTIES.getBrowserVersion(), Platform.LINUX); - caps.setCapability("screenResolution", "1280x1024x24"); + + private MutableCapabilities getDesiredCapabilities() { switch (PROPERTIES.getBrowserName()) { case CHROME: ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("no-sandbox"); - caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions); - break; + chromeOptions.setCapability("screenResolution", "1280x1024x24"); + return chromeOptions; case YANDEX: ChromeOptions yandexOptions = new ChromeOptions(); yandexOptions.setBinary("/usr/bin/yandex-browser-beta"); yandexOptions.addArguments("no-sandbox"); - caps.setCapability(ChromeOptions.CAPABILITY, yandexOptions); - break; + yandexOptions.setCapability("screenResolution", "1280x1024x24"); + return yandexOptions; case OPERA: OperaOptions operaOptions = new OperaOptions(); + operaOptions.setCapability(CapabilityType.BROWSER_VERSION, PROPERTIES.getBrowserVersion()); operaOptions.setBinary("/usr/bin/opera"); operaOptions.addArguments("no-sandbox"); - caps.setCapability(OperaOptions.CAPABILITY, operaOptions); - break; + operaOptions.setCapability("screenResolution", "1280x1024x24"); + return operaOptions; default: - break; + DesiredCapabilities caps = new DesiredCapabilities(PROPERTIES.getBrowserName(), PROPERTIES.getBrowserVersion(), Platform.LINUX); + caps.setCapability("screenResolution", "1280x1024x24"); + return caps; } - return caps; } - + public WebDriver getDriver() { return driver; } - + String getPageUrl(Page page) { return String.format("%s/%s", PROPERTIES.getBaseUrl(), page.getName()); } - + @Attachment("failure-screenshot") private byte[] takeScreenshot(WebDriver driver) { return ((TakesScreenshot) new Augmenter().augment(driver)).getScreenshotAs(OutputType.BYTES); } - + }