Skip to content

Commit

Permalink
Refactoring - Change Browser Type in test (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: RBRi <[email protected]>
  • Loading branch information
mabartos and rbri authored Nov 4, 2021
1 parent f303197 commit fdd38c4
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.Browser;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,10 +41,9 @@ public class BrowserVersionDeterminer {
* Determine browser by its capabilities
*/
public static BrowserVersion determine(Capabilities capabilities) {
String capBrowserName = capabilities.getBrowserName();
if (!BrowserType.HTMLUNIT.equals(capBrowserName)) {
if (!Browser.HTMLUNIT.is(capabilities)) {
throw new IllegalArgumentException("When building an HtmlUntDriver, the capability browser name must be set to '"
+ BrowserType.HTMLUNIT + "' but was '" + capBrowserName + "'.");
+ Browser.HTMLUNIT.browserName() + "' but was '" + capabilities.getBrowserName() + "'.");
}

String browserName;
Expand All @@ -66,7 +65,7 @@ public static BrowserVersion determine(Capabilities capabilities) {

final BrowserVersion result = browsers.stream()
.filter(Objects::nonNull)
.filter(item -> browserName.equals(item.getBrowserType()))
.filter(item -> item.getBrowser().is(browserName))
.findFirst()
.map(item -> item.getBrowserVersion(browserVersion))
.orElse(BrowserVersion.getDefault());
Expand All @@ -84,7 +83,7 @@ public static BrowserVersion determine(Capabilities capabilities) {
* Basic browser info
*/
protected interface BrowserInfo {
String getBrowserType();
Browser getBrowser();

BrowserVersion getBrowserVersion();

Expand All @@ -95,8 +94,8 @@ default BrowserVersion getBrowserVersion(String versionNumeric) {

protected static class Chrome implements BrowserInfo {
@Override
public String getBrowserType() {
return BrowserType.CHROME;
public Browser getBrowser() {
return Browser.CHROME;
}

@Override
Expand All @@ -107,8 +106,8 @@ public BrowserVersion getBrowserVersion() {

protected static class IE implements BrowserInfo {
@Override
public String getBrowserType() {
return BrowserType.IE;
public Browser getBrowser() {
return Browser.IE;
}

@Override
Expand All @@ -119,8 +118,8 @@ public BrowserVersion getBrowserVersion() {

protected static class Firefox implements BrowserInfo {
@Override
public String getBrowserType() {
return BrowserType.FIREFOX;
public Browser getBrowser() {
return Browser.FIREFOX;
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.junit.Test;
import org.openqa.selenium.environment.webserver.Page;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.testing.Ignore;
Expand All @@ -54,6 +54,8 @@
import org.openqa.selenium.testing.SwitchToTopAfterTest;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.testing.drivers.BrowserToCapabilities;
import org.openqa.selenium.testing.drivers.BrowserType;

@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=1500")
public class AlertsTest extends JUnit4TestBase {
Expand Down Expand Up @@ -633,7 +635,7 @@ public void browserVersionAndBooleanConstructor() {

@Test(expected = UnhandledAlertException.class)
public void capabilitiesConstructor() {
HtmlUnitDriver driver = new HtmlUnitDriver(new DesiredCapabilities(BrowserType.HTMLUNIT, null, Platform.ANY));
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserToCapabilities.of(BrowserType.HTML_UNIT));

driver.get(alertPage("cheese"));
driver.findElement(By.id("alert")).click();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/openqa/selenium/ClickScrollingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.SwitchToTopAfterTest;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.BrowserType;

@Ignore(value = HTMLUNIT, reason = "Scrolling requires rendering")
public class ClickScrollingTest extends JUnit4TestBase {
Expand Down Expand Up @@ -227,10 +227,10 @@ private void onlyPassIfNotOnMac(int mozIssue, Runnable toCheck) {
toCheck.run();
assumeFalse(
"It appears https://github.com/mozilla/geckodriver/issues/" + mozIssue + " is fixed",
Platform.getCurrent() == Platform.MAC && Browser.detect() == Browser.ff);
Platform.getCurrent() == Platform.MAC && BrowserType.detect() == BrowserType.FIREFOX);
} catch (Throwable e) {
// Swallow the exception, as this is expected for Firefox on OS X
if (!(Platform.getCurrent() == Platform.MAC && Browser.detect() == Browser.ff)) {
if (!(Platform.getCurrent() == Platform.MAC && BrowserType.detect() == BrowserType.FIREFOX)) {
throw e;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/openqa/selenium/TypingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.BrowserType;

public class TypingTest extends JUnit4TestBase {

Expand Down Expand Up @@ -254,7 +254,7 @@ private static void checkRecordedKeySequence(WebElement element, int expectedKey
@Test
@Ignore(IE)
public void testShouldReportKeyCodeOfArrowKeys() {
assumeFalse(Browser.detect() == Browser.opera &&
assumeFalse(BrowserType.detect() == BrowserType.OPERA &&
getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.javascriptPage);
Expand All @@ -280,7 +280,7 @@ public void testShouldReportKeyCodeOfArrowKeys() {

@Test
public void testShouldReportKeyCodeOfArrowKeysUpDownEvents() {
assumeFalse(Browser.detect() == Browser.opera &&
assumeFalse(BrowserType.detect() == BrowserType.OPERA &&
getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.javascriptPage);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/openqa/selenium/WindowSwitchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.SwitchToTopAfterTest;
import org.openqa.selenium.testing.TestUtilities;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.BrowserType;

import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -81,7 +81,7 @@ protected void finished(Description description) {
@NoDriverAfterTest(failedOnly = true)
@Test
public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOperations() {
assumeFalse(Browser.detect() == Browser.opera &&
assumeFalse(BrowserType.detect() == BrowserType.OPERA &&
TestUtilities.getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.xhtmlTestPage);
Expand Down Expand Up @@ -193,7 +193,7 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() {
@NotYetImplemented(SAFARI)
public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToHang()
throws Exception {
assumeFalse(Browser.detect() == Browser.opera &&
assumeFalse(BrowserType.detect() == BrowserType.OPERA &&
TestUtilities.getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.xhtmlTestPage);
Expand Down Expand Up @@ -225,7 +225,7 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH
@Test
@NotYetImplemented(SAFARI)
public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {
assumeFalse(Browser.detect() == Browser.opera &&
assumeFalse(BrowserType.detect() == BrowserType.OPERA &&
TestUtilities.getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.xhtmlTestPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.testing.drivers.BrowserToCapabilities;
import org.openqa.selenium.testing.drivers.BrowserType;
import com.gargoylesoftware.htmlunit.BrowserVersion;

/**
Expand All @@ -42,7 +41,7 @@ public class HtmlUnitCapabilitiesTest {

@Test
public void configurationViaDirectCapabilities() {
DesiredCapabilities ieCapabilities = new DesiredCapabilities(BrowserType.IE, "", Platform.ANY);
DesiredCapabilities ieCapabilities = BrowserToCapabilities.of(BrowserType.IE);

try {
BrowserVersionDeterminer.determine(ieCapabilities);
Expand All @@ -56,31 +55,25 @@ public void configurationViaDirectCapabilities() {

@Test
public void configurationOfFirefoxDefaultViaRemote() {
DesiredCapabilities firefoxCapabilities =
new DesiredCapabilities(BrowserType.HTMLUNIT, "firefox", Platform.ANY);

DesiredCapabilities firefoxCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox");
assertEquals(FIREFOX, BrowserVersionDeterminer.determine(firefoxCapabilities));
}

@Test
public void configurationOfFirefox78ViaRemote() {
DesiredCapabilities firefoxCapabilities =
new DesiredCapabilities(BrowserType.HTMLUNIT, "firefox-78", Platform.ANY);

DesiredCapabilities firefoxCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78");
assertEquals(FIREFOX_78, BrowserVersionDeterminer.determine(firefoxCapabilities));
}

@Test
public void configurationOfIEViaRemote() {
DesiredCapabilities ieCapabilities =
new DesiredCapabilities(BrowserType.HTMLUNIT, "internet explorer", Platform.ANY);

DesiredCapabilities ieCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "internet explorer");
assertEquals(INTERNET_EXPLORER, BrowserVersionDeterminer.determine(ieCapabilities));
}

@Test
public void tetsDefautlBrowserVersion() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);

assertEquals(BrowserVersion.getDefault(), BrowserVersionDeterminer.determine(capabilities));
}
Expand All @@ -101,7 +94,7 @@ public void htmlUnitReportsCapabilities() {
public void configurationOfBrowserLanguage() {
String browserLanguage = "es-ES";

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setCapability(BROWSER_LANGUAGE_CAPABILITY, browserLanguage);

assertEquals(browserLanguage, BrowserVersionDeterminer.determine(capabilities).getBrowserLanguage());
Expand Down
38 changes: 16 additions & 22 deletions src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.junit.Test;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import org.openqa.selenium.testing.drivers.BrowserToCapabilities;
import org.openqa.selenium.testing.drivers.BrowserType;

public class HtmlUnitDriverTest {

Expand Down Expand Up @@ -228,7 +230,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesJsTrue() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);

new HtmlUnitDriver(capabilities){
Expand All @@ -247,7 +249,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesJsFalse() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false);

new HtmlUnitDriver(capabilities){
Expand All @@ -266,8 +268,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesVersionString() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion("firefox");
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox");

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -278,8 +279,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion("googlechrome");
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "googlechrome");

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -290,8 +290,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion("MicrosoftEdge");
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "MicrosoftEdge");

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -302,8 +301,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion("firefox-78");
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78");

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -317,8 +315,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesVersion() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(BrowserType.FIREFOX);
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.FIREFOX.browserName());

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -329,8 +326,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(BrowserType.CHROME);
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.CHROME.browserName());

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -341,8 +337,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(BrowserType.EDGE);
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.EDGE.browserName());

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -353,8 +348,7 @@ protected WebClient modifyWebClient(WebClient client){
}
};

capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(BrowserType.FIREFOX + "-78");
capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78");

new HtmlUnitDriver(capabilities){
@Override
Expand All @@ -368,7 +362,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesJsEnabledTrue() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setJavascriptEnabled(true);

new HtmlUnitDriver(capabilities){
Expand All @@ -387,7 +381,7 @@ protected WebClient modifyWebClient(WebClient client){

@Test
public void ctorWebClientCapabilitiesJsEnabledFalse() {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setJavascriptEnabled(false);

new HtmlUnitDriver(capabilities){
Expand All @@ -409,7 +403,7 @@ public void ctorWebClientCapabilitiesProxy() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("hostname:1234");

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT);
capabilities.setCapability(CapabilityType.PROXY, proxy);

new HtmlUnitDriver(capabilities){
Expand Down
Loading

0 comments on commit fdd38c4

Please sign in to comment.