首页 > 代码库 > 软件测试lab2
软件测试lab2
- Install the selenium plugin on Firefox
First,entre the official website http://www.seleniumhq.org/download/
Click this link.
And click the installation button.
Finally, in the add-on, add selenium ide to the menu bar.
2. Learn to use SeleniumIDE to record scripts and export scripts
Click on the little label to open selenium ide.
Base url Fill in with the URL to test. When we use it, we should click the red button to recording firstly.
It can export scripts in various formats . We usually choose the Junit 4/WebDrive。
3. Write Selenium Java WebDriver program
The code in github URL is https://github.com/zjwjerome/lab2
The first method does not store data. Each cycle tests a use case. But the results can be seen at the end, and it is not easy to see which case is wrong.
The operation results shown in Figure:
@Parameters
public static Collection<Object[]> getData(){
Object[][] test = new Object[117][3];
File fi = new File("E:/inputgit.csv");
String csvSplitBy = ",";
int add = 0;
try{
BufferedReader br = new BufferedReader(new FileReader(fi));
String line = br.readLine();
while ((line = br.readLine()) != null) {
String [] temp = line.split(csvSplitBy);
test[add][0] = temp[0];
test[add][1] = temp[0].substring(4,10);
test[add][2] = temp[2];
add++;
}
br.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
return Arrays.asList(test);
}
@Test
public void testSelenium() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("name")).clear();
driver.findElement(By.id("name")).sendKeys(name);
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys(pwd);
driver.findElement(By.id("submit")).click();
String infoAll = driver.findElement(By.xpath("//tbody[@id=‘table-main‘]")).getText();
String infoAddr = infoAll.substring(infoAll.indexOf("http"));
assertEquals(url, infoAddr);
}
We can only see a result in the end.
The second method first stores the data and then uses a parametric test.
Parametric test preparation process is as follows:
A. Specifying a special runtime for the parameterized test class with @RunWith annotation: Parameterized.class;
B. Declare several variables in the test class, which are used to store the expected values and test data and create a constructor for several parameters of the user;
C. Create a static test data feed (feed) method, the return type is Collection, and use @Parameter annotation to modify;
D. Write the test method (with @Test annotation).
The operation results shown in Figure:
It can test each pair of data. The wrong will prompt, and it will not stop running.
软件测试lab2