Juint4 + WebDriver to build an automated testing framework (transfer)

Reprinted from: http://www.cnblogs.com/GGHHLL/archive/2013/06/07/3124097.html

In this example, Baidu's search is used as an example, and the Baidu home page is defined as a class to be tested HomePage

 

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

The third-party class PageFactory is used in the above constructor, and some methods to be tested (small steps in the test case) are defined.
The following is the test class homepageTest corresponding to HomePage. You can right-click on HomePage to create a new junit file and select BeforeClass , Setup ... Note that the name must end with Test.

copy code
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();
}
}
copy code

 

There are test cases in @Test, and there can be multiple @Tests.
Now you can compile, run as --> junit test

The iedriver used in this article, the machine is 64-bit, will start your 64-bit IE by default (ie8 is divided into 64 and 32-bit). If you need to start 32-bit IE, you need to start selenium sever with a 32-bit jar.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326602115&siteId=291194637