Java/Selenium 重构

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

如何重构此代码以避免重复?

最佳答案

这看起来像是您要考虑设置 PageFactory 以供重用的情况。

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

它将内联定义提取到成员变量中以便更好地重用。默认情况下,它还会根据需要延迟加载 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.
         */
    }
}

关于Java/Selenium 重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063286/

相关文章:

java - Java 项目中的自动更新属性文件值

java - Jar 中的 @PropertySource 用于类路径上的外部文件

reactjs - 使用 Selenium 识别 ReactJS 网页上的元素,这是更好的方法吗?

java - 我希望 mvn test 在运行单元测试之前运行 sh 文件

c# - 如何在用 C# 编写的 Selenium WebDriver(Nunit 测试用例)中按 "Enter"?

python - Selenium 无法打开指定的 URL 并显示数据 :,

java - Apache Commons EmailValidator 覆盖 isValidDomain

java - Swing 中的外部面板

python - 虽然声明未对 Selenium Webdriver 评估为 false

ruby - 在 selenium webdriver 2 ruby​​ 中鼠标向上