软件测试(五)——上机使用Selenium进行Web测试

1. 安装Selenium插件

首先下载最新版火狐浏览器。在工具栏“附加组件”中,搜索“Selenium IDE”,然后安装

 

2.使用 Selenium IDE录制并导出脚本

安装好插件后,点击工具栏中的。然后通过文件--》new test case创建新的测试用例。

a.录制脚本:

接下来进行如下流程的操作:

1)点击右侧的红色按钮开始录制脚本。

2)在火狐浏览器中输入网址http://www.ncfxy.com/,访问该网址

3)在网页的用户名和密码中分别填写正确的信息,并点击提交。

4)在邮箱的位置点击右键,如图所示找到对应的assertText

5)点击IDE中的红色按钮,完成录制

图示流程:

1)

2)3)

4)

5)

完成录制之后,可以通过点击绿色的开始按钮进行测试。

b.导出脚本


首先,点击Optinons-->Options-->General,勾选Enable experimental features

然后,通过Options-->Format选择要导出成的相应格式。

这里为了后面的Java使用,选择Java / JUnit 4/ WebDriver

然后会得到生成的Java文件

 

3.编写Selenium Java WebDriver程序

创建工程,并且引入如下的几种包

 

编写java文件
  1 package com.Selenium.Lab2;
  2 
  3 import java.io.FileNotFoundException;
  4 import java.io.IOException;
  5 import java.nio.charset.Charset;
  6 import java.util.Arrays;
  7 import java.util.Collection;
  8 import java.util.concurrent.TimeUnit;
  9 
 10 import org.junit.*;
 11 import org.junit.runner.RunWith;
 12 import org.junit.runners.Parameterized;
 13 import org.junit.runners.Parameterized.Parameters;
 14 
 15 import static org.junit.Assert.*;
 16 
 17 import org.openqa.selenium.*;
 18 import org.openqa.selenium.firefox.FirefoxDriver;
 19 
 20 import com.csvreader.CsvReader;
 21 
 22 
 23 @RunWith(Parameterized.class)
 24 public class WebDriverTest {
 25   private WebDriver driver;
 26   private boolean acceptNextAlert = true;
 27   private StringBuffer verificationErrors = new StringBuffer();
 28   private String id = null;
 29   private String email = null;
 30 
 31   public WebDriverTest(String id, String email){
 32       this.id= id;
 33       this.email = email;
 34   }
 35   
 36   @Before
 37   public void setUp() throws Exception {
 38     System.setProperty("webdriver.firefox.bin", "D:/installed_softwares/FireFox/firefox.exe");// 如果firefox没有装在c盘,这里需要指明安装位置
 39     driver = new FirefoxDriver();
 40     driver.get("http://www.ncfxy.com/");
 41     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 42   }
 43 
 44   @Parameters
 45   public static Collection<Object[]> getData() throws IOException{
 46       //使用参数化测试。每从csv文件中读取出一行创建一个object数组
 47       Object[][] obj = new Object[109][];
 48       CsvReader r= new CsvReader("E://本科课程学习//软件测试//Lab2//info.csv",',',Charset.forName("GBK"));
 49       int count = 0;
 50       while(r.readRecord()){
 51           obj[count] = new Object[]{r.get(0), r.get(1)};
 52           count++;
 53       }
 54       return Arrays.asList(obj);
 55   }
 56   
 57   @Test
 58   public void testCase() throws Exception {
 59     driver.findElement(By.id("name")).clear();
 60     driver.findElement(By.id("name")).sendKeys(this.id);
 61     driver.findElement(By.id("pwd")).clear();
 62     driver.findElement(By.id("pwd")).sendKeys(this.id.substring(4));
 63     driver.findElement(By.id("submit")).click();
 64     assertEquals(this.email, driver.findElement(By.xpath("//tbody[@id='table-main']/tr/td[2]")).getText());
 65   }
 66 
 67   @After
 68   public void tearDown() throws Exception {
 69     driver.close();
 70     String verificationErrorString = verificationErrors.toString();
 71     if (!"".equals(verificationErrorString)) {
 72       fail(verificationErrorString);
 73     }
 74   }
 75 
 76 //  private boolean isElementPresent(By by) {
 77 //    try {
 78 //      driver.findElement(by);
 79 //      return true;
 80 //    } catch (NoSuchElementException e) {
 81 //      return false;
 82 //    }
 83 //  }
 84 //
 85 //  private boolean isAlertPresent() {
 86 //    try {
 87 //      driver.switchTo().alert();
 88 //      return true;
 89 //    } catch (NoAlertPresentException e) {
 90 //      return false;
 91 //    }
 92 //  }
 93 //
 94 //  private String closeAlertAndGetItsText() {
 95 //    try {
 96 //      Alert alert = driver.switchTo().alert();
 97 //      String alertText = alert.getText();
 98 //      if (acceptNextAlert) {
 99 //        alert.accept();
100 //      } else {
101 //        alert.dismiss();
102 //      }
103 //      return alertText;
104 //    } finally {
105 //      acceptNextAlert = true;
106 //    }
107 //  }
108 }
WebDriverTest

进行测试,得到结果(run109,pass109)

posted on 2016-04-14 18:26  DennisWong  阅读(614)  评论(0编辑  收藏  举报