-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(selenium): apply Selenium 3 and 4 workaround for switchTo()
when using Safari Driver
#412
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,4 @@ updates: | |
maven-low-risk: | ||
update-types: | ||
- "minor" | ||
- "patch" | ||
- "patch" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
selenium/src/test/java/com/deque/html/axecore/selenium/WebDriverExtensionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package com.deque.html.axecore.selenium; | ||
|
||
import com.deque.html.axecore.extensions.WebDriverExtensions; | ||
import java.util.ArrayList; | ||
import org.junit.Assert; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chrome.ChromeOptions; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.openqa.selenium.firefox.FirefoxOptions; | ||
import org.openqa.selenium.safari.SafariDriver; | ||
|
||
public class WebDriverExtensionsTest { | ||
private enum Browser { | ||
CHROME, | ||
FIREFOX, | ||
SAFARI | ||
} | ||
|
||
private ArrayList<Object> tryOpenAboutBlank(Browser browser) { | ||
WebDriver webDriver = null; | ||
switch (browser) { | ||
case CHROME: | ||
webDriver = new ChromeDriver(new ChromeOptions().addArguments("--headless")); | ||
break; | ||
case FIREFOX: | ||
webDriver = new FirefoxDriver(new FirefoxOptions().addArguments("--headless")); | ||
break; | ||
case SAFARI: | ||
// SafariDriver does not support headless mode | ||
webDriver = new SafariDriver(); | ||
break; | ||
} | ||
|
||
Exception exception = null; | ||
|
||
String addr = "http://localhost:8001"; | ||
webDriver.get(addr + "/index.html"); | ||
|
||
try { | ||
WebDriverExtensions.openAboutBlank(webDriver); | ||
} catch (Exception e) { | ||
exception = e; | ||
} | ||
|
||
// store exception and the current url in the webDriver to be checked later | ||
ArrayList<Object> exceptionAndUrl = new ArrayList<>(); | ||
|
||
exceptionAndUrl.add(exception); | ||
exceptionAndUrl.add(webDriver.getCurrentUrl()); | ||
|
||
webDriver.quit(); | ||
|
||
return exceptionAndUrl; | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowGivenChromedriver() { | ||
ArrayList<Object> exceptionAndUrl = tryOpenAboutBlank(Browser.CHROME); | ||
|
||
Exception exception = (Exception) exceptionAndUrl.get(0); | ||
String url = (String) exceptionAndUrl.get(1); | ||
|
||
Assert.assertNull(exception); | ||
Assert.assertEquals(url, "about:blank"); | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowGivenGeckodriver() { | ||
ArrayList<Object> exceptionAndUrl = tryOpenAboutBlank(Browser.FIREFOX); | ||
|
||
Exception exception = (Exception) exceptionAndUrl.get(0); | ||
String url = (String) exceptionAndUrl.get(1); | ||
|
||
Assert.assertNull(exception); | ||
Assert.assertEquals(url, "about:blank"); | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowGivenSafariDriver() { | ||
// if OS is windows or linux, skip this test as Safari is not available | ||
String os = System.getProperty("os.name").toLowerCase(); | ||
Assume.assumeFalse(os.contains("windows")); | ||
Assume.assumeFalse(os.contains("linux")); | ||
|
||
ArrayList<Object> exceptionAndUrl = tryOpenAboutBlank(Browser.SAFARI); | ||
|
||
Exception exception = (Exception) exceptionAndUrl.get(0); | ||
String url = (String) exceptionAndUrl.get(1); | ||
|
||
Assert.assertNull(exception); | ||
Assert.assertEquals(url, "about:blank"); | ||
} | ||
|
||
@Test | ||
public void shouldThrowWhenSwitchToFails() { | ||
// Create a mock driver to throw an exception when switchTo() is called | ||
// This is to simulate `switchTo()` failing and throwing an exception | ||
// We expect the exception to be caught and handled correctly. | ||
class MockedDriver extends ChromeDriver { | ||
public MockedDriver(ChromeOptions chromeOptions) { | ||
super(chromeOptions); | ||
} | ||
|
||
@Override | ||
public WebDriver.TargetLocator switchTo() { | ||
throw new RuntimeException("BOOM!"); | ||
} | ||
} | ||
|
||
MockedDriver webDriver = new MockedDriver(new ChromeOptions().addArguments("--headless")); | ||
webDriver.get("http://localhost:8001/index.html"); | ||
|
||
Exception exception = | ||
Assert.assertThrows( | ||
Exception.class, | ||
() -> { | ||
WebDriverExtensions.openAboutBlank(webDriver); | ||
}); | ||
|
||
Assert.assertTrue(exception.getMessage().contains("switchToWindow failed.")); | ||
} | ||
|
||
@Test | ||
public void shouldThrowWhenUnableToDetermineWindowHandle() { | ||
class MockedDriver extends ChromeDriver { | ||
public MockedDriver(ChromeOptions chromeOptions) { | ||
super(chromeOptions); | ||
} | ||
|
||
@Override | ||
public Object executeScript(String script, Object... args) { | ||
// Note: This is to simulate another window being created along with the about:blank | ||
// window. This is to simulate the case where the about:blank window is not the | ||
// only window being created and the window handle cannot be determined. | ||
super.executeScript(script, args); | ||
return super.executeScript(script, args); | ||
Zidious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
MockedDriver webDriver = new MockedDriver(new ChromeOptions().addArguments("--headless")); | ||
webDriver.get("http://localhost:8001/index.html"); | ||
|
||
RuntimeException exception = | ||
Assert.assertThrows( | ||
RuntimeException.class, | ||
() -> { | ||
WebDriverExtensions.openAboutBlank(webDriver); | ||
}); | ||
|
||
Assert.assertEquals(exception.getCause().getMessage(), "Unable to determine window handle"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed. This is already handled by the driver's
get
method as per the W3C spec. This is causing unwanted side effects using the Safari driver