我对 Selenium WebDriver 和 JUnit 相当陌生,我正在测试一个 Web 应用程序,并且想知道我在 Junit 中的类设计是否正确以测试 UI?
正如我所看到的人们使用合成的例子。
任何建议将不胜感激
Java 类
public class OverviewPage {
private WebDriver driver;
private String URL = "http://www.google.com";
public String searchQuery = "BBC";
OverviewPage(WebDriver driver){
this.driver = driver;
driver.get(URL);
if(!"Login".equals(driver.getTitle())){
throw new IllegalStateException("Wrong site");
}
}
By searchBox = By.id("siteSearchField");
By submitSearch = By.cssSelector("button.btn.searchIco");
public OverviewPage searchBox(String findADealer){
driver.findElement(searchBox).sendKeys(findADealer);
return this;
}
public OverviewPage searchBoxDisplayed(){
driver.findElement(searchBox);
return this;
}
public FindADealerPage searchResults(){
driver.findElement(searchBox).sendKeys(searchQuery);
driver.findElement(submitSearch).click();
String search = driver.getPageSource();
boolean searchResults = search.contains(searchQuery);
return new FindADealerPage(driver);
}
}
朱尼特
public class OverviewPageTest {
private WebDriver driver;
public String searchQuery = "find a dealer";
By searchBox = By.id("siteSearchField");
By submitSearch = By.cssSelector("button.btn.searchIco");
@Before
public void setUp(){
driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
}
@After
public void tearDown(){
driver.quit();
}
@Test
public void checkTitle(){
Assert.assertEquals("product edit", driver.getTitle());
}
@Test
public void checkSearchBoxExists(){
boolean searchBoxes = driver.findElement(searchBox).isDisplayed();
Assert.assertTrue(searchBoxes);
}
@Test
public void searchResults(){
driver.findElement(searchBox).sendKeys(searchQuery);
driver.findElement(submitSearch).click();
String search = driver.getPageSource();
boolean searchResults = search.contains(searchQuery);
Assert.assertTrue(searchResults);
}
}