Selenium Webdriver Overrider
CHIRAG SINGH
Sr. Lead Architect ??||Follow me on YouTube "Free Automation Learning" Channel ???? 8K+ subscribers?? || 14K+ followers ????
Introduction
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.
Setup
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 below url Github package configuration
<settings xmlns="https://maven.apache.org/SETTINGS/1.0.0
? xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="https://maven.apache.org/SETTINGS/1.0.0
????????????????????? https://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>"
2. Added below dependency on the pom.xml
?<dependency
? <groupId>com.freeautomation</groupId>
? <artifactId>seleniumoverride</artifactId>
? <version>1.1.0</version>
</dependency>>
领英推荐
Configuration
Add the below code to override the WebDriver and WebElement methods
// Create the WebDriver instanc
WebDriver d = new FirefoxDriver();
// Create the Customize WebDriver
WebDriver driver = ModifiedRemoteWebDriver.getModifiedDriver(new DriverImplements(d));e
2. Create a Java class to add the Driver Implementation
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);
?? ??? ?}
?? ?}
?? ?
}{
3. Create a Java class to add the WebElement Implementation
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);
?? ??? ?}
?? ?}{
Running the tests
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("https://button[text()=' Login ']")).click();
?? ??? ?driver.findElement(By.xpath("https://div[@class='oxd-topbar-header-userarea']//i")).click();
?? ??? ?driver.findElement(By.xpath("https://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();
?? ?};
#selenium #seleniumautomation #seleniumwebdriver #seleniumtesting #testautomation #testing #qaautomation #freeautomationlearning
Lead Automation Engineer @ GalaxE.Solutions | Master Degree in Computer Science
1 年Very useful
SDET | Framework Development | Automation Testing | Manual Testing | Playwright | Cypress | Selenium | Rest Assured | SQL | No SQL | Java | TypeScript | JavaScript | Python | TestNG | BDD | GIT | Jenkins | Jira | HP ALM
1 年Can't we directly override it instead of fetching a library?
Lead SDET at Iris Software Inc.| Ex IQVIA | Ex Guru.com | Ex Innova Solutions
1 年But I can not understand, what is the use case.
SDET/Automation Consultant
1 年Join our group SDET/Automation Jobs for latest job updates and openings for Software testers, QA, and IT professionals.?https://www.dhirubhai.net/groups/13857028/
Sr. Lead Architect ??||Follow me on YouTube "Free Automation Learning" Channel ???? 8K+ subscribers?? || 14K+ followers ????
1 年Naveen Khunteta Mukesh Otwani Amuthan Sakthivel Wasiq Bhamla Sanjay Kumar Could you please try this library once and provide your feedback to improve it.