WebDriver Overrider is an open-source Java library that carries out the override WebDriver and WebElements interface.
By using WebDriverOverrider you can override the findElement, getTitle, switchTo and other methods which is in WebDriver Interface.
If you want to override WebElement Interface methods such as click, isDisplayed, isEnabled and other methods you can also do.
I have uploaded the WebDriver Overrider as github package. You can customize the Maven setting.xml to downoad this package. All steps are mentioned on the url Github package configuration
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/freeautomationlearning/SeleniumOverride</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
<dependency>
<groupId>com.freeautomation</groupId>
<artifactId>seleniumoverride</artifactId>
<version>1.1.0</version>
</dependency>
Add the below code to override the WebDriver and WebElement methods
// Create the WebDriver instance
WebDriver d = new FirefoxDriver();
// Create the Customize WebDriver
WebDriver driver = ModifiedRemoteWebDriver.getModifiedDriver(new DriverImplements(d));
public class DriverImplements implements InvocationHandler {
WebDriver driver;
CustomizeMethods obj;
public DriverImplements(WebDriver driver) {
this.driver = driver;
obj = new CustomizdMethodsImplements();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "findElement":
obj.overrideWebelement(driver, args);
return ModifiedRemoteWebDriver.getModifiedElement(new WebelementsImplement());
default:
return method.invoke(driver, args);
}
}
}
public class WebelementsImplement implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "click":
Constants.getElementInstance().click();
return true;
case "isDisplayed":
System.out.println("isDisplay :- "+Constants.getElementInstance().isDisplayed());
return Constants.getElementInstance().isDisplayed();
case "sendKeys":
Constants.getElementInstance().sendKeys((CharSequence[])args[0]);
return true;
default:
return method.invoke(Constants.getElementInstance(), args);
}
}
WebDriver driver;
@BeforeMethod
public void setUp()
{
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver d = new ChromeDriver(options);
driver = ModifiedRemoteWebDriver.getModifiedDriver(new DriverImplements(d));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
}
@Test
public void login()
{
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin123");
driver.findElement(By.xpath("//button[text()=' Login ']")).click();
driver.findElement(By.xpath("//div[@class='oxd-topbar-header-userarea']//i")).click();
driver.findElement(By.xpath("//a[text()='Logout']")).click();
System.out.println("USERNAME : "+driver.findElement(By.name("username")).isDisplayed());
Assert.assertEquals(driver.findElement(By.name("username")).isDisplayed(), true);
}
@AfterMethod
public void tearDown()
{
driver.quit();
}
I am Full Stack Tester. For more info Please go to my channel on YouTube Free Automation Learning
WebDriver Overrider is a project created and maintained by Chirag Singh and licensed under the terms of the MIT License.