Skip to content

Commit

Permalink
Using Options classes directly instead of DesiredCapabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Sep 26, 2020
1 parent 08e04c1 commit 42368c0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
19 changes: 10 additions & 9 deletions src/test/java/com/aerokube/selenoid/TestProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,24 +21,24 @@
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()) {
proxy.start();
proxy.setHarCaptureTypes(CaptureType.REQUEST_HEADERS);
}
}

@Before
public void startHar(){
if (proxy.isStarted()) {
proxy.newHar();
}
}

@Features("Using proxies")
@Test
public void testProxy() throws Exception {
Expand All @@ -52,7 +53,7 @@ public void clearHar() {
proxy.endHar();
}
}

@AfterClass
public static void shutdownProxy() {
if (proxy.isStarted()) {
Expand All @@ -61,11 +62,11 @@ public static void shutdownProxy() {
}

@Override
protected Function<DesiredCapabilities, DesiredCapabilities> getCapabilitiesProcessor() {
protected Function<MutableCapabilities, MutableCapabilities> 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();
Expand All @@ -82,13 +83,13 @@ private DesiredCapabilities getProxyCapabilities(DesiredCapabilities caps) {
switches.add("--proxy-server=" + proxyString);
caps.setCapability("chrome.switches", switches);
}

ret.setCapability(CapabilityType.PROXY, proxy);
return ret;
}

private String getProxyString() {
return String.format("%s:%d", getLocalHost(), proxy.getPort());
}

}
30 changes: 15 additions & 15 deletions src/test/java/com/aerokube/selenoid/misc/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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<DesiredCapabilities, DesiredCapabilities> getCapabilitiesProcessor() {

protected Function<MutableCapabilities, MutableCapabilities> getCapabilitiesProcessor() {
return Function.identity();
}

protected String getLocalHost() {
try {
if (System.getProperty("os.name").startsWith("Mac")) {
Expand All @@ -84,5 +84,5 @@ protected String getLocalHost() {
return "localhost";
}
}

}
54 changes: 26 additions & 28 deletions src/test/java/com/aerokube/selenoid/misc/WebDriverRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DesiredCapabilities, DesiredCapabilities> capabilitiesProcessor;
WebDriverRule(Function<DesiredCapabilities, DesiredCapabilities> capabilitiesProcessor) {

private final Function<MutableCapabilities, MutableCapabilities> capabilitiesProcessor;

WebDriverRule(Function<MutableCapabilities, MutableCapabilities> capabilitiesProcessor) {
this.capabilitiesProcessor = capabilitiesProcessor;
}

@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
Expand All @@ -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);
}

}

0 comments on commit 42368c0

Please sign in to comment.