标签:
1. 安装 SeleniumIDE 并使用它进行录制和导出脚本
1) seleniumIDE是火狐的插件,所以要先要安装火狐;
2) 打开火狐,选择【开发者】,再选择【获取更多工具】,点击网页里的【附加组件标题】 或 直接登陆
https://addons.mozilla.org/zh-CN/firefox/collections/mozilla/webdeveloper/
3) 搜索seleniumIDE
添加到Firefox
4) 安装完成后,会在右上角有一个图标,点击打开它
5) 访问测试网站,这里使用 http://www.ncfxy.com,输入用户名和密码
6) 点击 提交
选中新网页中显示的邮箱,右键选择assertText…..
 
7) 导出脚本
 
 
2. 在eclipse中进行模拟测试:
1) 打开eclipse,新建一个普通的java工程,将上面生成的脚本(java文件)拖拽进来,修改包名。
2) 从官网下载selenium所需要的库
http://www.seleniumhq.org/download/
3) 将这个压缩包解压出来的所有jar导入工程
       
4)因为笔者使用的数据由csv文件中获取,所以还需要导入一个csv的解析包,使用的是javacsv.Jar
5)修改脚本进行测试,注意那个csv文件的路径要写自己的文件
package com.example.tests;
 
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.regex.Pattern;
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 static org.hamcrest.CoreMatchers.*;
 
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
 
import com.csvreader.CsvReader;
 
@RunWith(Parameterized.class)
public class SeleniumIDETest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
 
  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.ncfxy.com";
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
  }
 
  @Test
  public void testSeleniumIDE() 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(name.substring(4));
    driver.findElement(By.id("submit")).click();
    assertTrue(email.equals(driver.findElement(By.xpath("//tbody[@id='table-main']/tr/td[2]")).getText()));
    //assertEquals(email, driver.findElement(By.xpath("//tbody[@id='table-main']/tr/td[2]")).getText());
  }
 
  @Parameters
  public static Collection<Object[]> getData() throws IOException{
      //使用参数化测试。每从csv文件中读取出一行创建一个object数组
      Object[][] obj = new Object[109][];
      CsvReader r= new CsvReader("E:\\课件\\大三下\\软件测试课件\\lab\\2\\info.csv",',',Charset.forName("GBK"));
      int count = 0;
     
      while(r.readRecord()){
          obj[count] = new Object[]{r.get(0), r.get(1)};
          count++;
      }
      return Arrays.asList(obj);
  }
 
  String name;
  String email;
  public SeleniumIDETest(String name, String email){
        this.name = name;
        this.email = email;
  }
 
  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }
 
  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }
 
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}6)测试运行
ST实验二------- seleniumIDE 与可视化网页的测试
标签:
原文地址:http://blog.csdn.net/yinglang19941010/article/details/51175079