我是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/