Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Selenium refactoring

I'm novice in java. I have a couple of methods, where are a lot of duplicate code. For example,

 public static void firstMethod() {
        WebElement firstEl = driver.findElement(By.xpath("..."));
        WebElement secondEl = driver.findElement(By.xpath("..."));
        WebElement thirdEl = driver.findElement(By.xpath("..."));
        WebElement fourthEl = driver.findElement(By.xpath("..."));
        WebElement fifthEl = driver.findElement(By.xpath(""..."));
        firstEl.click();
        plusOperation.click();
        thirdEl.click();
        fifthEl.click();
    }

    public static void secondMethod() {
        WebElement firstEl = driver.findElement(By.xpath("..."));
        WebElement secondEl = driver.findElement(By.xpath("..."));
        WebElement thirdEl = driver.findElement(By.xpath("..."));
        WebElement fourthEl = driver.findElement(By.xpath("..."));
        WebElement fifthEl = driver.findElement(By.xpath(""..."));
        firstEl.click();
        secondEl.click();
        thirdEl.click();
        fourthEl.click();
        fifthEl.click();
    }

How I can refactor this code to escape from duplicating?

like image 296
BigZoo Avatar asked Mar 04 '26 16:03

BigZoo


1 Answers

That looks like a case where you'd want to consider setting up a PageFactory for reuse.

https://github.com/SeleniumHQ/selenium/wiki/PageFactory

It extracts the inline definitions to member-variables for better reuse. By default it also lazy-loads the WebElement references on demand which is nice if you have a page that refreshes or changes often.

Another great advantage is that it lets you expose well-defined behavioral methods that assist in understanding the environment model.

From your example above, I think it would look something like this:

Sample Page Model

package testing.selenium.environment.model;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

/**
 * Sample page for PageFactory example.
 *
 */
public class MyPage {
    /** Known first-index reference.*/
    @FindBy(xpath="")
    private WebElement first;
    /** Known second-index reference.*/
    @FindBy(xpath="")
    private WebElement second;
    /** Known third-index reference.*/
    @FindBy(xpath="")
    private WebElement third;
    /** Known fourth-index reference.*/
    @FindBy(xpath="")
    private WebElement fourth;
    /** Known fifth-index reference.*/
    @FindBy(xpath="")
    private WebElement fifth;

    /**
     * Clicks all known elements.
     */
    public void clickAll() {
        first.click();
        second.click();
        third.click();
        fourth.click();
        fifth.click();
    }

    /**
     * Clicks all elements determined to be at 'even' reference locations.
     */
    public void clickEvens() {
        second.click();
        fourth.click();       
    }

    /**
     * Clicks all elements determined to be at 'odd' reference locations.
     */ 
    public void clickOdds() {
        first.click();
        third.click();
        fifth.click();
    }

}

UPDATED TEST

package testing.selenium.environment.model;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;

/**
 * Junit test leveraging PageFactory.
 *
 */
public class TestMyPageBehavior {
    /** Page model being tested.*/
    private MyPage page;

    /**
     * Environment setup.
     */
    @Before
    public void setup() {
        WebDriver driver = new FirefoxDriver(DesiredCapabilities.firefox());
        driver.get("MyPage url"); //Must go to the page first in order for the PageFactory to resolve references correctly.
        page = PageFactory.initElements(driver, MyPage.class);
    }

    /**
     * Verifies behavior of {@link MyPage#clickAll()}
     */
    @Test
    public void testClickAll() {
        page.clickAll();
        /*
         * Assert page state.  You could do this inside the page model possibly; or expose a method that gives you access to 
         * the datasets you need in order to provide the validations.
         */
    }

    /**
     * Verifies behavior of {@link MyPage#clickEvens()}
     */
    @Test
    public void testClickEvens() {
        page.clickEvens();
        /*
         * Assert page state.  You could do this inside the page model possibly; or expose a method that gives you access to 
         * the datasets you need in order to provide the validations.
         */
    }

    /**
     * Verifies behavior of {@link MyPage#clickOdds()}
     */
    @Test
    public void testClickOdds() {
        page.clickOdds();
        /*
         * Assert page state.  You could do this inside the page model possibly; or expose a method that gives you access to 
         * the datasets you need in order to provide the validations.
         */
    }
}
like image 79
Jeremiah Avatar answered Mar 07 '26 06:03

Jeremiah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!