-
-
Save ckzgraphics/c4186ffca0b2336cf1a3f5f3fc7dd761 to your computer and use it in GitHub Desktop.
Lean BrowserFactory Pattern with Selenium WebDriver
This file contains hidden or 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
<!-- https://mvnrepository.com/artifact/org.reflections/reflections --> | |
<dependency> | |
<groupId>org.reflections</groupId> | |
<artifactId>reflections</artifactId> | |
<version>0.9.12</version> | |
</dependency> |
This file contains hidden or 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
public interface Browser { | |
MutableCapabilities getOptions(boolean isHeadless); | |
WebDriver getDriver(boolean isHeadless); | |
} |
This file contains hidden or 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
public class Chrome implements Browser { | |
// ChromeOptions extends MutableCapabilities satisfying interface declaration | |
@Override | |
public ChromeOptions getOptions(boolean isHeadless) { | |
var options = new ChromeOptions(); | |
options.setHeadless(isHeadless); | |
// Do Chrome specific settings | |
return options; | |
} | |
@Override | |
public WebDriver getDriver(boolean isHeadless) { | |
return new ChromeDriver(getOptions(isHeadless)); | |
} | |
// Add other methods | |
} |
This file contains hidden or 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
import org.reflections.Reflections; | |
public final class BrowserFactory { | |
private boolean isHeadless; | |
public BrowserFactory setHeadless(boolean isHeadless) { | |
this.isHeadless = isHeadless; | |
return this; | |
} | |
public WebDriver getInstance(String browserName) throws Exception { | |
// Returns a List of classes that derives from Browser using Generics and Reflection | |
// The ? is a bounded wildcard which denotes an unknown type | |
List<Class<? extends Browser>> browsers = new ArrayList<>( | |
new Reflections(Browser.class.getPackageName()).getSubTypesOf(Browser.class)); | |
// Filters the List for the class containing the browserName, instantiates the class, and gets the WebDriver | |
return browsers.stream().filter(b -> b.getSimpleName().toLowerCase().contains(browserName.toLowerCase())) | |
.findFirst().orElseThrow(() -> new RuntimeException("Browser not supported!")).getConstructor() | |
.newInstance().getDriver(isHeadless); | |
} | |
} |
This file contains hidden or 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
public abstract class BaseTest { | |
protected WebDriver driver; | |
@BeforeEach | |
public void setUp() throws Exception { | |
// Set environment variables | |
driver = new BrowserFactory() | |
.setHeadless(Boolean.parseBoolean(System.getenv("SELENIUM_HEADLESS"))) | |
.getInstance(System.getenv("SELENIUM_BROWSER")); | |
// More code | |
} | |
@AfterEach | |
public void tearDown() { | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment