简体   繁体   English

Selenium WebDriver和Junit类设计

[英]Selenium WebDriver and Junit Class design

I am fairly new to Selenium WebDriver and JUnit, I am testing a web app and was wondering is my class design correct in Junit for testing the UI? 我是Selenium WebDriver和JUnit的新手,我正在测试一个Web应用程序,并且想知道我在Junit中的类设计是否正确用于测试UI?
As I have seen instances of where people have used composition. 正如我所见,人们使用构图的地方。

Any advice would be greatly appreciated 任何建议将不胜感激

Java Class 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);
}

} }

Junit Junit的

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

} }

Your Java class OverviewPage suggests to me that you are wanting to use the PageObject model. 您的Java类OverviewPage向我建议您要使用PageObject模型。

If you want to follow Google's example ( https://code.google.com/p/selenium/wiki/PageObjects ), you could put all the fields and methods pertaining to a particular page in the PageObject rather than the TestClass. 如果您想关注Google的示例( https://code.google.com/p/selenium/wiki/PageObjects ),您可以将所有与特定页面相关的字段和方法放在PageObject而不是TestClass中。

For example, in your TestClass, instantiate the PageObject: 例如,在TestClass中,实例化PageObject:

OverviewPage page = new OverViewPage(driver);

and throughout your TestCalss, replace things like driver.get("http://www.google.com"); 在整个TestCalss中,替换诸如driver.get("http://www.google.com"); with driver.get(page.URL); with driver.get(page.URL);

Basically what it boils down to is - you shouldn't have anything in quotes in your TestClass. 基本上它归结为 - 你的TestClass中不应该有任何引号。 The benefit of this pattern is when you have multiple tests referring to the same field in the PageObject, then when you need to update that field - you can do so easily in one place, rather than refactoring multiple lines of duplicate code throughout your tests. 这种模式的好处是当您有多个测试引用PageObject中的相同字段时,然后当您需要更新该字段时 - 您可以在一个地方轻松完成,而不是在整个测试中重构多行重复代码。

Also, any given test needn't have more than two lines - a method call and an assertion. 此外,任何给定的测试都不需要超过两行 - 方法调用和断言。

So using your test searchResults() as an example, you could move the following lines into a method within the page object: 因此,使用测试searchResults()作为示例,您可以将以下行移动到页面对象中的方法中:

driver.findElement(searchBox).sendKeys(searchQuery);
driver.findElement(submitSearch).click();
String search = driver.getPageSource();
boolean searchResults = search.contains(searchQuery);
return searchResults; // added this one...

And your test becomes: 你的测试成了:

@Test
public void searchResults(){
    boolean searchResults = page.searchResults();
    Assert.assertTrue(searchResults);
}

That's my interpretation. 这是我的解释。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM