[原創]Juint4 + WebDriver 搭建自動化測試框架


本例中用百度的搜索為例,將百度首頁定義成一個待測試類 HomePage

 

public class HomePage {
private WebDriver driver;

@FindBy(how = How.NAME, using = "wd")
public static WebElement serchInputbox;

@FindBy(how = How.ID, using = "su")
public static WebElement serchBtn;

@FindBy(how = How.ID, using = "container")
public static WebElement serchResult;

public HomePage(WebDriver driver) {
    this.driver = driver;
    ElementLocatorFactory finder = new AjaxElementLocatorFactory(driver,
            120);
    PageFactory.initElements(finder, this);

}

public void enterSerchTxt(String serchTxt) {
    serchInputbox.clear();
    serchInputbox.sendKeys(serchTxt);
}

public void clickSerchButton() {
    serchBtn.click();
}

public void checkResult() {
    assertEquals(serchResult.isDisplayed(), true);
}
} 

上面的構造函數中用到了 PageFactory 這個三方類,另外定義了一些待測方法(測試用例中的小步驟)
下面是對應於 HomePage 的測試類 homepageTest ,您可以在HomePage上右擊新建 junit file ,選擇 BeforeClass, Setup ...需要注意的是命名必須是以 Test 結尾。

public class homepageTest {
protected static WebDriver driver;

@BeforeClass
public static void beforeClass() throws Exception {
    driver = new InternetExplorerDriver();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
    driver.quit();
}

@Before
public void setUp() throws Exception {
    driver.get("http://www.baidu.com");
}

@After
public void tearDown() throws Exception {
}

@Test
public void testHomepage() {
    HomePage homepage = new HomePage(driver);
    homepage.enterSerchTxt("selenium");
    homepage.clickSerchButton();
    // maybe the net will delay, so wait for while
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    homepage.checkResult();
}
}

 

@Test 里面便是測試用例,可以有多個 @Test。
現在就可以編譯下,run as --> junit test

本文采用的 iedriver ,機器是64位的,會默認啟動你的64位 ie(ie8分64和32位),如果您需要啟32位 ie,則需要用32位的 jar 啟動 selenium sever。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM