7. Create your own implementation of a `WebDriver` - Rafal-Laskowski/Metalloid-WebDriver-Pool GitHub Wiki
Metalloid-WebDriver-Pool allows you to override any methods you use with the WebDriver
interface by exposing MetalloidDriver.class
.
Implementation:
public abstract class MetalloidDriver implements WebDriver {
protected final WebDriver driver;
public MetalloidDriver(WebDriver driver) {
this.driver = driver;
}
@Override
public void get(String url) {
driver.get(url);
}
@Override
public String getCurrentUrl() {
return driver.getCurrentUrl();
}
@Override
public String getTitle() {
return driver.getTitle();
}
@Override
public List<WebElement> findElements(By by) {
return driver.findElements(by);
}
@Override
public WebElement findElement(By by) {
return driver.findElement(by);
}
@Override
public String getPageSource() {
return driver.getPageSource();
}
@Override
public void close() {
driver.close();
}
@Override
public void quit() {
driver.quit();
}
@Override
public Set<String> getWindowHandles() {
return driver.getWindowHandles();
}
@Override
public String getWindowHandle() {
return driver.getWindowHandle();
}
@Override
public TargetLocator switchTo() {
return driver.switchTo();
}
@Override
public Navigation navigate() {
return driver.navigate();
}
@Override
public Options manage() {
return driver.manage();
}
}
By extending MetalloidDriver
you can override any of these methods.
Let's suppose you have a class MyDriver
which extends MetalloidDriver
and overrides get()
method
public class MyDriver extends MetalloidDriver {
@Override
public void get(String url) {
System.out.println("Opening url: " + url);
super.get(url);
}
}
How to tell Metalloid-WebDriver-Pool
to create an instance of this class and return it to you?
Before you run the test you need to specify a wrapper like this:
WebDriverPool.registerWrapper(MyDriver.class);
To get the instance of MyDriver.class
you can use:
WebDriverPool.getCustom();
. There is only one flaw.
getCustom()
returns MetalloidDriver
so you have to cast it to your implementation
MyDriver driver = (MyDriver) WebDriverPool.getCustom();
You can create a very elegant solution in MyDriver.class
. Just implement a static method like this:
public class MyDriver extends MetalloidDriver {
public static MyDriver get() {
return (MyDriver) WebDriverPool.getCustom();
}
@Override
public void get(String url) {
System.out.println("Opening url: " + url);
super.get(url);
}
}
Now, you can just use MyDriver driver = MyDriver.get();
to get your own implementation!