Java/Selenium重构

Java/Selenium重构,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我是java新手。 我有几个方法,其中有很多重复的代码。 比如说, public static void firstMethod() { WebElement firstEl = driver.findElement(By.xpath("...")); WebElement secondEl = driver.findElement(By.xpath("...")); WebElement thirdEl = driver.findElement

我是java新手。 我有几个方法,其中有很多重复的代码。 比如说,

 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();
    }

如何重构这个代码以避免复制?

这看起来是一个你想考虑建立一个PayStand用于重用的情况。

它将内联定义提取到成员变量,以便更好地重用。默认情况下,它还会根据需要延迟加载WebElement引用,如果您有一个经常刷新或更改的页面,这会很好

另一个巨大的优势是,它允许您公开定义良好的行为方法,以帮助理解环境模型

从你上面的例子来看,我认为它应该是这样的:

示例页面模型

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();
    }

}
更新测试

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.
         */
    }
}