【软件测试】Lab#2 Selenium
这次的实验室是使用SeleniumIDE及jar包进行自动化测试。下面是我做的实验的一些结果:
1.安装firefox,安装SeleniumIDE,用seleniumIDE录制脚本,导出脚本。
录制脚本如图:
导出webdriver如下:
1 package com.example.tests; 2 3 import java.util.regex.Pattern; 4 import java.util.concurrent.TimeUnit; 5 import org.junit.*; 6 import static org.junit.Assert.*; 7 import static org.hamcrest.CoreMatchers.*; 8 import org.openqa.selenium.*; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 import org.openqa.selenium.support.ui.Select; 11 12 public class Export { 13 private WebDriver driver; 14 private String baseUrl; 15 private boolean acceptNextAlert = true; 16 private StringBuffer verificationErrors = new StringBuffer(); 17 18 @Before 19 public void setUp() throws Exception { 20 driver = new FirefoxDriver(); 21 baseUrl = "https://www.baidu.com/"; 22 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 23 } 24 25 @Test 26 public void testExport() throws Exception { 27 driver.get(baseUrl + "/"); 28 driver.findElement(By.id("kw")).click(); 29 driver.findElement(By.id("kw")).clear(); 30 driver.findElement(By.id("kw")).sendKeys("天津大学"); 31 driver.findElement(By.id("su")).click(); 32 driver.findElement(By.id("kw")).click(); 33 driver.findElement(By.id("kw")).clear(); 34 driver.findElement(By.id("kw")).sendKeys("南开大学"); 35 driver.findElement(By.id("su")).click(); 36 driver.findElement(By.id("su")).click(); 37 } 38 39 @After 40 public void tearDown() throws Exception { 41 driver.quit(); 42 String verificationErrorString = verificationErrors.toString(); 43 if (!"".equals(verificationErrorString)) { 44 fail(verificationErrorString); 45 } 46 } 47 48 private boolean isElementPresent(By by) { 49 try { 50 driver.findElement(by); 51 return true; 52 } catch (NoSuchElementException e) { 53 return false; 54 } 55 } 56 57 private boolean isAlertPresent() { 58 try { 59 driver.switchTo().alert(); 60 return true; 61 } catch (NoAlertPresentException e) { 62 return false; 63 } 64 } 65 66 private String closeAlertAndGetItsText() { 67 try { 68 Alert alert = driver.switchTo().alert(); 69 String alertText = alert.getText(); 70 if (acceptNextAlert) { 71 alert.accept(); 72 } else { 73 alert.dismiss(); 74 } 75 return alertText; 76 } finally { 77 acceptNextAlert = true; 78 } 79 } 80 }
2.编写webdriver,测试info.csv中的对应关系是否正确。
在这部分,总共写了3个类
Webdriver.java
用于自动向http://www.ncfxy.com中输入学号和密码,并提取该学号对应邮箱。注意用到了对字符串的部分截取。
其实因为要测试109数据,应该在同一个页面中,用selenium来退回页面(driver.navigate().back(); // 后退)和刷新页面(driver.navigate().refresh(); // 刷新),这样效率应该更高,但是因为这次实验,我把webdriver单独写到一个类里面去了,所以判断一次邮箱是否匹配都要调用一次这个类来自动化找到网页中的邮箱,所以只能每次都打开新页面,再关闭新页面。如果直接把webdriver写到测试用例里,应该可以后退,然后再刷新重输了。其中用到的相关代码:
firefox:
1 public static String webDriver(String stuNum) 2 { 3 String email; 4 //get the last 6num as the password 5 String password = stuNum.substring(stuNum.length()-6, stuNum.length()); 6 7 System.setProperty("webdriver.firefox.bin", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe"); 8 WebDriver driver = new FirefoxDriver(); 9 driver.get("http://www.ncfxy.com"); 10 11 //submit student number 12 WebElement number = driver.findElement(By.xpath(".//*[@id='name']")); 13 number.sendKeys(stuNum); 14 //input password 15 WebElement pass = driver.findElement(By.xpath(".//*[@id='pwd']")); 16 pass.sendKeys(password); 17 pass.submit(); 18 19 //get the email address 20 WebElement emailAdd = driver.findElement(By.xpath(".//*[@id='table-main']/tr[1]/td[2]")); 21 //System.out.println(emailAdd.getText()); 22 email = emailAdd.getText(); 23 //must quit the driver, then the browser will quit itself 24 driver.quit(); 25 return email; 26 }
chrome 和 IE只是部分代码不同而已,下面是不同的代码:
chrome:
1 System.setProperty("webdriver.chrome.driver", "C:/Users/lenovo/Desktop/dasanxia/st/webdriver/chromedriver.exe"); 2 ChromeDriver driver= new ChromeDriver();
IE:
1 System.setProperty("webdriver.ie.driver", "C:/Users/lenovo/Desktop/dasanxia/st/webdriver/IEDriverServer.exe"); 2 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); 3 ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 4 InternetExplorerDriver driver = new InternetExplorerDriver(ieCapabilities); 5 driver.get("http://www.ncfxy.com");
其中,IE需要在internet选项中设置下面都解除保护模式
ReadCSV.java
用于读取已知的csv文件,并把学号和邮箱分别转存到两个字符串数组中。其中,用到了已有的jar包 javacsv.jar
这个jar包很方便,基本的对csv的操作都可以实现,这里只需要简单的写几行代码就好了。主代码如下:
1 public static void readCsv(String[] stuNum, String[] emailAddress){ 2 try{ 3 4 ArrayList<String[]> csvList = new ArrayList<String[]>(); 5 String csvFilePath = "C:/Users/lenovo/Desktop/dasanxia/st/2nd_Experiment/info.csv"; 6 CsvReader reader = new CsvReader(csvFilePath, ',', Charset.forName("SJIS")); 7 //reader.readHeaders(); 8 9 while(reader.readRecord()){// read in the data line by line 10 csvList.add(reader.getValues()); 11 12 } 13 reader.close(); 14 15 int i = 0; 16 for(int row=0; row < csvList.size(); row++) 17 { 18 String num = csvList.get(row)[0]; 19 String email = csvList.get(row)[1]; 20 stuNum[i] = num; 21 emailAddress[i] = email; 22 i++; 23 24 } 25 26 27 }catch(Exception ex){ 28 System.out.println(ex); 29 } 30 31 }
TestDriver.java
这个是测试用例,因为有109个邮箱要测试,所以需要用到JUnit的参数化测试,还是通过百度,找到了相关的模板,然后直接应用到这里就好了。其中注意如果不加
@RunWith(Parameterized.class)的话,这个类的构造函数是为默认的无参数构造函数,而参数化测试需要添加带有参数的构造函数。
代码如下:
1 package cn.tju.sc.st.webdriver; 2 3 import static org.junit.Assert.*; 4 5 import java.util.Arrays; 6 import java.util.Collection; 7 8 import org.junit.Before; 9 import org.junit.BeforeClass; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 import org.junit.runners.Parameterized; 13 import org.junit.runners.Parameterized.Parameters; 14 15 @RunWith(Parameterized.class) 16 public class TestDriver { 17 18 private static String[] stuNum; 19 private static String[] emailAdd; 20 private static Webdriver wd; 21 22 private String expected; 23 private String actual; 24 25 //constructor 26 public TestDriver(String expected, String actual) 27 { 28 this.expected = expected; 29 this.actual = actual; 30 } 31 32 /* 33 @BeforeClass 34 public static void SetUp(){ 35 stuNum = new String[200]; 36 emailAdd = new String[200]; 37 ReadCSV.readCsv(stuNum, emailAdd); 38 // System.out.println(stuNum[108]); 39 } 40 */ 41 42 @Parameters 43 public static Collection prepareData() 44 { 45 stuNum = new String[200]; 46 emailAdd = new String[200]; 47 ReadCSV.readCsv(stuNum, emailAdd); 48 49 Object[][] objects = new Object[109][2]; 50 for(int i = 0; i < objects.length; i++) 51 { 52 objects[i][0]= Webdriver.webDriver(stuNum[i]); 53 objects[i][1] = emailAdd[i]; 54 55 } 56 57 return Arrays.asList(objects); 58 } 59 60 61 @Before 62 public void setUp() 63 { 64 wd = new Webdriver(); 65 66 } 67 68 69 @Test 70 public void test() { 71 72 assertEquals(this.expected,this.actual); 73 74 } 75 76 }
运行结果如图: