Skip to content

Commit

Permalink
[java] Do not start multiple browsers in tests when not necessary usi…
Browse files Browse the repository at this point in the history
…ng @NoDriverBeforeTest

This should also decrease the number of times browsers are opened and closed without doing anything.
  • Loading branch information
titusfortner committed Jun 8, 2022
1 parent da43407 commit 263d346
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.chrome;

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
Expand All @@ -30,6 +31,7 @@
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverBeforeTest;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.time.Duration;
Expand All @@ -47,23 +49,26 @@ public class ChromeDriverFunctionalTest extends JUnit4TestBase {
private final String CLIPBOARD_WRITE = "clipboard-write";

@Test
@NoDriverBeforeTest
public void builderGeneratesDefaultChromeOptions() {
WebDriver driver = ChromeDriver.builder().build();
driver.quit();
localDriver = ChromeDriver.builder().build();
Capabilities capabilities = ((ChromeDriver) localDriver).getCapabilities();

assertThat(localDriver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ZERO);
assertThat(capabilities.getCapability("browserName")).isEqualTo("chrome");
}

@Test
@NoDriverBeforeTest
public void builderOverridesDefaultChromeOptions() {
ChromeOptions options = new ChromeOptions();
options.setImplicitWaitTimeout(Duration.ofMillis(1));
WebDriver driver = ChromeDriver.builder().oneOf(options).build();
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));

driver.quit();
localDriver = ChromeDriver.builder().oneOf(options).build();
assertThat(localDriver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
}

@Test
public void builderWithClientConfigthrowsException() {
public void builderWithClientConfigThrowsException() {
ClientConfig clientConfig = ClientConfig.defaultConfig().readTimeout(Duration.ofMinutes(1));
RemoteWebDriverBuilder builder = ChromeDriver.builder().config(clientConfig);

Expand All @@ -88,27 +93,23 @@ public void canSetPermission() {
}

@Test
@NoDriverBeforeTest
public void canSetPermissionHeadless() {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);

//TestChromeDriver is not honoring headless request; using ChromeDriver instead
WebDriver driver = new WebDriverBuilder().get(options);
try {
HasPermissions permissions = (HasPermissions) driver;
localDriver = new WebDriverBuilder().get(options);
HasPermissions permissions = (HasPermissions) localDriver;

driver.get(pages.clicksPage);
assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("prompt");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("prompt");
localDriver.get(pages.clicksPage);
assertThat(checkPermission(localDriver, CLIPBOARD_READ)).isEqualTo("prompt");
assertThat(checkPermission(localDriver, CLIPBOARD_WRITE)).isEqualTo("prompt");

permissions.setPermission(CLIPBOARD_READ, "granted");
permissions.setPermission(CLIPBOARD_WRITE, "granted");
permissions.setPermission(CLIPBOARD_READ, "granted");
permissions.setPermission(CLIPBOARD_WRITE, "granted");

assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("granted");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("granted");
} finally {
driver.quit();
}
assertThat(checkPermission(localDriver, CLIPBOARD_READ)).isEqualTo("granted");
assertThat(checkPermission(localDriver, CLIPBOARD_WRITE)).isEqualTo("granted");
}

public String checkPermission(WebDriver driver, String permission){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.build.InProject;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverBeforeTest;
import org.openqa.selenium.testing.TestUtilities;

import java.io.IOException;
Expand All @@ -36,26 +39,18 @@ public class ChromeOptionsFunctionalTest extends JUnit4TestBase {

private static final String EXT_PATH = "common/extensions/webextensions-selenium-example.crx";

private ChromeDriver driver = null;

@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}

@Test
@NoDriverBeforeTest
public void canStartChromeWithCustomOptions() {
ChromeOptions options = new ChromeOptions();
if (TestUtilities.isOnTravis()) {
options.setHeadless(true);
}
options.addArguments("user-agent=foo;bar");
driver = new ChromeDriver(options);
localDriver = new ChromeDriver(options);

driver.get(pages.clickJacker);
Object userAgent = driver.executeScript("return window.navigator.userAgent");
localDriver.get(pages.clickJacker);
Object userAgent = ((ChromeDriver) localDriver).executeScript("return window.navigator.userAgent");
assertThat(userAgent).isEqualTo("foo;bar");
}

Expand All @@ -69,50 +64,52 @@ public void optionsStayEqualAfterSerialization() {
}

@Test
@NoDriverBeforeTest
public void canSetAcceptInsecureCerts() {
ChromeOptions options = new ChromeOptions();
if (TestUtilities.isOnTravis()) {
options.setHeadless(true);
}
options.setAcceptInsecureCerts(true);
driver = new ChromeDriver(options);
localDriver = new ChromeDriver(options);

assertThat(driver.getCapabilities().getCapability(ACCEPT_INSECURE_CERTS)).isEqualTo(true);
assertThat(((ChromeDriver) localDriver).getCapabilities().getCapability(ACCEPT_INSECURE_CERTS)).isEqualTo(true);
}

@Test
@NoDriverBeforeTest
public void canAddExtensionFromFile() {
ChromeOptions options = new ChromeOptions();
if (TestUtilities.isOnTravis()) {
options.setHeadless(true);
}
options.addExtensions(InProject.locate(EXT_PATH).toFile());
driver = new ChromeDriver(options);
localDriver = new ChromeDriver(options);

driver.get(pages.echoPage);
localDriver.get(pages.echoPage);

WebElement footerElement = driver.findElement(By.id("webextensions-selenium-example"));
WebElement footerElement = localDriver.findElement(By.id("webextensions-selenium-example"));

String footText = footerElement.getText();
assertThat(footText).isEqualTo("Content injected by webextensions-selenium-example");
}

@Test
@NoDriverBeforeTest
public void canAddExtensionFromStringEncodedInBase64() throws IOException {
ChromeOptions options = new ChromeOptions();
if (TestUtilities.isOnTravis()) {
options.setHeadless(true);
}
options.addEncodedExtensions(Base64.getEncoder().encodeToString(
Files.readAllBytes(InProject.locate(EXT_PATH))));
driver = new ChromeDriver(options);
localDriver = new ChromeDriver(options);

driver.get(pages.echoPage);
localDriver.get(pages.echoPage);

WebElement footerElement = driver.findElement(By.id("webextensions-selenium-example"));
WebElement footerElement = localDriver.findElement(By.id("webextensions-selenium-example"));

String footText = footerElement.getText();
assertThat(footText).isEqualTo("Content injected by webextensions-selenium-example");
}

}
47 changes: 24 additions & 23 deletions java/test/org/openqa/selenium/edge/EdgeDriverFunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.edge;

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
Expand All @@ -29,6 +30,7 @@
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverBeforeTest;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.time.Duration;
Expand All @@ -46,23 +48,26 @@ public class EdgeDriverFunctionalTest extends JUnit4TestBase {
private final String CLIPBOARD_WRITE = "clipboard-write";

@Test
public void builderGeneratesDefaultChromeOptions() {
WebDriver driver = EdgeDriver.builder().build();
driver.quit();
@NoDriverBeforeTest
public void builderGeneratesDefaultEdgeOptions() {
localDriver = EdgeDriver.builder().build();
Capabilities capabilities = ((EdgeDriver) localDriver).getCapabilities();

assertThat(localDriver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ZERO);
assertThat(capabilities.getCapability("browserName")).isEqualTo("msedge");
}

@Test
public void builderOverridesDefaultChromeOptions() {
@NoDriverBeforeTest
public void builderOverridesDefaultEdgeOptions() {
EdgeOptions options = new EdgeOptions();
options.setImplicitWaitTimeout(Duration.ofMillis(1));
WebDriver driver = EdgeDriver.builder().oneOf(options).build();
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));

driver.quit();
localDriver = EdgeDriver.builder().oneOf(options).build();
assertThat(localDriver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
}

@Test
public void builderWithClientConfigthrowsException() {
public void builderWithClientConfigThrowsException() {
ClientConfig clientConfig = ClientConfig.defaultConfig().readTimeout(Duration.ofMinutes(1));
RemoteWebDriverBuilder builder = EdgeDriver.builder().config(clientConfig);

Expand All @@ -87,27 +92,23 @@ public void canSetPermission() {
}

@Test
@NoDriverBeforeTest
public void canSetPermissionHeadless() {
EdgeOptions options = new EdgeOptions();
options.setHeadless(true);

//TestEdgeDriver is not honoring headless request; using EdgeDriver instead
WebDriver driver = new WebDriverBuilder().get(options);
try {
HasPermissions permissions = (HasPermissions) driver;
localDriver = new WebDriverBuilder().get(options);
HasPermissions permissions = (HasPermissions) localDriver;

driver.get(pages.clicksPage);
assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("prompt");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("prompt");
localDriver.get(pages.clicksPage);
assertThat(checkPermission(localDriver, CLIPBOARD_READ)).isEqualTo("prompt");
assertThat(checkPermission(localDriver, CLIPBOARD_WRITE)).isEqualTo("prompt");

permissions.setPermission(CLIPBOARD_READ, "granted");
permissions.setPermission(CLIPBOARD_WRITE, "granted");
permissions.setPermission(CLIPBOARD_READ, "granted");
permissions.setPermission(CLIPBOARD_WRITE, "granted");

assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("granted");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("granted");
} finally {
driver.quit();
}
assertThat(checkPermission(localDriver, CLIPBOARD_READ)).isEqualTo("granted");
assertThat(checkPermission(localDriver, CLIPBOARD_WRITE)).isEqualTo("granted");
}

public String checkPermission(WebDriver driver, String permission){
Expand Down
48 changes: 18 additions & 30 deletions java/test/org/openqa/selenium/edge/EdgeOptionsFunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,35 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.build.InProject;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverBeforeTest;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.io.IOException;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Base64;

public class EdgeOptionsFunctionalTest extends JUnit4TestBase {

private static final String EXT_PATH = "common/extensions/webextensions-selenium-example.crx";

private WebDriver edgeDriver = null;

@After
public void tearDown() {
if (edgeDriver != null) {
edgeDriver.quit();
}
}

@Test
public void canStartChromeWithCustomOptions() {
@NoDriverBeforeTest
public void canStartEdgeWithCustomOptions() {
EdgeOptions options = new EdgeOptions();
options.addArguments("user-agent=foo;bar");
edgeDriver = new WebDriverBuilder().get(options);
localDriver = new WebDriverBuilder().get(options);

edgeDriver.get(pages.clickJacker);
Object userAgent = ((JavascriptExecutor) edgeDriver).executeScript("return window.navigator.userAgent");
localDriver.get(pages.clickJacker);
Object userAgent = ((JavascriptExecutor) localDriver).executeScript("return window.navigator.userAgent");
assertThat(userAgent).isEqualTo("foo;bar");
}

Expand All @@ -74,45 +61,46 @@ public void optionsStayEqualAfterSerialization() {
}

@Test
@NoDriverBeforeTest
public void canSetAcceptInsecureCerts() {
EdgeOptions options = new EdgeOptions();
options.setAcceptInsecureCerts(true);
edgeDriver = new WebDriverBuilder().get(options);
System.out.println(((HasCapabilities) edgeDriver).getCapabilities());
localDriver = new WebDriverBuilder().get(options);
System.out.println(((HasCapabilities) localDriver).getCapabilities());

assertThat(((HasCapabilities) edgeDriver).getCapabilities().getCapability(ACCEPT_INSECURE_CERTS)).isEqualTo(true);
assertThat(((HasCapabilities) localDriver).getCapabilities().getCapability(ACCEPT_INSECURE_CERTS)).isEqualTo(true);
}

@Test
@NotYetImplemented
@NoDriverBeforeTest
public void canAddExtensionFromFile() {
EdgeOptions options = new EdgeOptions();
options.addExtensions(InProject.locate(EXT_PATH).toFile());
edgeDriver = new WebDriverBuilder().get(options);
localDriver = new WebDriverBuilder().get(options);

edgeDriver.get(pages.echoPage);
localDriver.get(pages.echoPage);

WebElement footerElement = driver.findElement(By.id("webextensions-selenium-example"));
WebElement footerElement = localDriver.findElement(By.id("webextensions-selenium-example"));

String footText = footerElement.getText();
assertThat(footText).isEqualTo("Content injected by webextensions-selenium-example");

}

@Test
@NotYetImplemented
@NoDriverBeforeTest
public void canAddExtensionFromStringEncodedInBase64() throws IOException {
EdgeOptions options = new EdgeOptions();
options.addEncodedExtensions(Base64.getEncoder().encodeToString(
Files.readAllBytes(InProject.locate(EXT_PATH))));
edgeDriver = new WebDriverBuilder().get(options);
localDriver = new WebDriverBuilder().get(options);

edgeDriver.get(pages.echoPage);
localDriver.get(pages.echoPage);

WebElement footerElement = driver.findElement(By.id("webextensions-selenium-example"));
WebElement footerElement = localDriver.findElement(By.id("webextensions-selenium-example"));

String footText = footerElement.getText();
assertThat(footText).isEqualTo("Content injected by webextensions-selenium-example");
}

}
Loading

0 comments on commit 263d346

Please sign in to comment.