Software Testing -- LAB02-Selenium上机实验
Selenium上机实验
博客地址:http://www.cnblogs.com/--CYH--/p/6612232.html
github:https://github.com/JohnsonGreen/winfile/tree/master/WorkSpace/EclipseJavaWeb/SeleniumTest/src/net/chenyuhong/selenium
1.Selenium 安装与使用
. 1) 完成selenium在firefox上的安装之后打开selenium,点击录制,并在浏览器输入框中输入“天津大学”,
2)录制完毕后,添加测试用例:浏览器输入框右键-Show All Available Commands-选择assertTitle 天津大学_百度搜索,便可在selenium中添加命令
3) 然后调节Play speed,点击Play entire test suite,运行所有命令
4) 全部运行成功
5)导出Java Junit测试代码
2.导出脚本的使用
1)将以下两个jar包加入项目测试目录
2)为了兼容性,选取firefox的40版本,在测试文件中选择Converage as Junit,弹出firefox后,测试成功:
3. 测试inputgit.csv表格中的学号和git地址的对应关系是否正确
1)按行读取文件,并分离其中的学号和github地址,保存在Object二维数组中,以用作多用例测试:
2)将学号和密码自动填入输入框,并且查找学号和github对应的地址
3)测试结果: 一共117个测试用例
4.源代码
1) inputgit.csv文件
学号,姓名,github地址 3014219080,陈煜弘,http://github.com/JohnsonGreen
2)Csv.java:用于读取csv文件
1 package net.chenyuhong.selenium; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 /** 11 * @Title: Csv.java 12 * @Package net.chenyuhong.selenium 13 * @Description: 14 * @author cyh tjuchenheng@163.com 15 * @date 2017-3-24 下午2:17:15 16 * @version V1.0 17 */ 18 public class Csv { 19 20 /** 21 * 导入 22 * 23 * @param file csv文件(路径+文件) 24 * @return 25 */ 26 public static List<Student> importCsv(File file){ 27 List<Student> dataList=new ArrayList<Student>(); 28 29 BufferedReader br=null; 30 try { 31 br = new BufferedReader(new FileReader(file)); 32 String line = ""; 33 br.readLine(); 34 35 while ((line = br.readLine()) != null) { 36 37 String[] result = line.split("\\,"); 38 Student student= new Student(); 39 student.setStunum(result[0]); 40 student.setGithub(result[2]); 41 for (String s : result) { 42 System.out.println(s); 43 } 44 45 dataList.add(student); 46 } 47 }catch (Exception e) { 48 }finally{ 49 if(br!=null){ 50 try { 51 br.close(); 52 br=null; 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 59 return dataList; 60 } 61 62 public static Object[][] getStu(){ 63 String path = "F:\\TESTFILE\\inputgit.csv"; 64 File file = new File(path); 65 66 67 List<Student> stuList = Csv.importCsv(file); 68 Object[][] aObjects = new Object[stuList.size()][2]; 69 int i = 0; 70 for(Student stu : stuList){ 71 aObjects[i][0] = stu.getStunum(); 72 aObjects[i][1] = stu.getGithub(); 73 i++; 74 } 75 76 return aObjects; //读取文件 77 } 78 79 }
3)Student.java:用于保存学生的学号以及github账号信息
package net.chenyuhong.selenium; /** * @Title: Students.java * @Package net.chenyuhong.selenium * @Description: * @author cyh tjuchenheng@163.com * @date 2017-3-24 下午2:29:13 * @version V1.0 */ public class Student { private String stunum; private String github; public String getStunum() { return stunum; } public void setStunum(String stunum) { this.stunum = stunum; } public String getGithub() { return github; } public void setGithub(String github) { this.github = github; } }
4)Selenium测试代码
package net.chenyuhong.selenium; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.TimeUnit; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import static org.junit.Assert.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; @RunWith(Parameterized.class) public class TestSelenium { private WebDriver driver; private String baseUrl; private StringBuffer verificationErrors = new StringBuffer(); private String stunum; private String github; public TestSelenium(String stunum,String github){ this.stunum = stunum; this.github = github; } @Parameters public static Collection<Object[]> getData(){ return Arrays.asList(Csv.getStu()); //获取所有学生的学号和github信息 } @Before public void setUp() throws Exception { System.setProperty ( "webdriver.firefox.bin" , "F:/Surfing/Firefox/firefox.exe" ); //firefox的安装路径 driver = new FirefoxDriver(); baseUrl = "http://121.193.130.195:8080/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testSelenium() throws Exception { driver.get(baseUrl); String str = this.stunum; driver.findElement(By.id("name")).sendKeys(this.stunum); driver.findElement(By.id("pwd")).sendKeys(str.substring(str.length()-6)); driver.findElement(By.id("submit")).click(); String stun = driver.findElement(By.xpath("//tbody/tr[2]/td[2]")).getText(); //利用xpath找到网页中的学号 String github = driver.findElement(By.xpath("//tbody/tr[3]/td[2]")).getText(); System.out.println(stun); System.out.println(github); assertEquals(this.stunum,stun); //判断csv中的学号和网页中的学号是否相等 assertEquals(this.github, github); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } }