软件测试(五)——使用Selenium IDE进行自动化测试

使用Selenium IDE进行自动化测试

1. 综述

  Selenium IDE是火狐浏览器的一个插件,它会记录你在网页中进行的操作,如登陆、点击等。更为强大的是它还能将记录导出,例如导出成junit测试用例,非常强大,接下里将会看见。

  在火狐的插件管理里,搜索这个插件,安装。

 

2. 使用Selenium IDE

  1. 单击浏览器上Selenium IDE图标,打开之;
  2. 随后即可在网页上进行操作,Selenium IDE会自动记录操作,下图是我打开http://www.ncfxy.com/,输入账号密码并登录的记录;
  3. 导出为Java / JUnit 4 / WebDriver                                                                                                                                                      
  4. 保存导出内容,导出内容为文本,seleniumSuite.java是我自己的命名;
  5. 在文件夹里找到seleniumSuite.java,其内容如下,稍加修改就可作为JUnit测试类。修改后的代码https://github.com/yongheng20/SeleniumJavaWebDriver
     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 SeleniumSuite {
    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 = "http://www.ncfxy.com/";
    22     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    23   }
    24 
    25   @Test
    26   public void testSeleniumSuite() throws Exception {
    27     driver.get(baseUrl + "/");
    28     driver.findElement(By.id("name")).clear();
    29     driver.findElement(By.id("name")).sendKeys("0123456789");
    30     driver.findElement(By.id("pwd")).clear();
    31     driver.findElement(By.id("pwd")).sendKeys("000000");
    32     driver.findElement(By.id("submit")).click();
    33   }
    34 
    35   @After
    36   public void tearDown() throws Exception {
    37     driver.quit();
    38     String verificationErrorString = verificationErrors.toString();
    39     if (!"".equals(verificationErrorString)) {
    40       fail(verificationErrorString);
    41     }
    42   }
    43 
    44   private boolean isElementPresent(By by) {
    45     try {
    46       driver.findElement(by);
    47       return true;
    48     } catch (NoSuchElementException e) {
    49       return false;
    50     }
    51   }
    52 
    53   private boolean isAlertPresent() {
    54     try {
    55       driver.switchTo().alert();
    56       return true;
    57     } catch (NoAlertPresentException e) {
    58       return false;
    59     }
    60   }
    61 
    62   private String closeAlertAndGetItsText() {
    63     try {
    64       Alert alert = driver.switchTo().alert();
    65       String alertText = alert.getText();
    66       if (acceptNextAlert) {
    67         alert.accept();
    68       } else {
    69         alert.dismiss();
    70       }
    71       return alertText;
    72     } finally {
    73       acceptNextAlert = true;
    74     }
    75   }
    76 }
posted @ 2016-04-19 20:20  永哼哼  阅读(701)  评论(0编辑  收藏  举报