The first Test of webdriver Automation Test
Now that you have set up the environment for testing and learned the methods of locate elements, let's create the first simple automated test case.
The test scenario is as follows:
1. Open Baidu home page
two。 Enter keywords in the search box, such as: webdriver automation testing
3. Click on Baidu button
4. Verify that the search results contain the keywords entered
An example of use case automated test code is as follows:
Package com.example.tests
Import org.openqa.selenium.By
Import org.openqa.selenium.WebDriver
Import org.openqa.selenium.firefox.FirefoxDriver
Import org.testng.Assert
Import org.testng.annotations.AfterMethod
Import org.testng.annotations.BeforeMethod
Import org.testng.annotations.Test
Public class BaiDuSearchTest {
Private WebDriver driver
Private String baseUrl
@ BeforeMethod
Public void setUp () throws Exception {
/ / Launch Firefox browser
Driver = new FirefoxDriver ()
BaseUrl = "http://www.baidu.com";
}
@ Test
Public void baiDuSearchTest () throws Exception {
String exResult= "WebDriver automation testing"
/ / Open Baidu home page
Driver.get (baseUrl)
/ / Locate search box and input search keyword
Driver.findElement (By.id ("kw1")) .sendKeys ("WebDriver automation testing")
/ / Click Baidu button
Driver.findElement (By.id (su1)) .click ()
/ / find the first link on the results page and verify that the search keyword is displayed in the link
String actResult=driver.findElement (By.id ("1")) .getText ()
Assert.assertTrue (actResult.contains (exResult))
}
@ AfterMethod
Public void tearDown () throws Exception {
Driver.quit ()
}
}
Then right-click the java file and select run as TestNG test, and then you can view the execution of the automated test case.
This is the end of the simplest test case. Is it very easy?