[软件测试] - No.4 Selenium + Firefox 自动化测试

一、测试环境相关资源:

这个测试需要我们配置Selenium 和Firefox 上的Selenium IDE,由于版本的问题配置方式有以下两种:

1.新版Firefox(52.01) + Selenium 3 + geckodriver

2.旧版Firefox(40) + Selenium2.48(2.4X左右应该都可以)

二、打开Firefox,运行Selenium IDE 录制操作



然后将测试用例导出为Java Junit4


三、创建自动化测试:

新建你的Eclipse项目,这里我是用的是Firefox(40) + Selenium2.48。在文件中导入以下文件:

1.Junit-4.12

2.harmcrest-1.3

3.selenium2.48

项目如图所示:


Lab2.java:

import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
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;

@RunWith(Parameterized.class)
public class Lab2 {
  private String studentId;
  private String github;
  private String passwd;
  
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  
  public Lab2(String id,String passwd,String git) {
	this.studentId = id;
	this.github = git;
	this.passwd = passwd;
  }
  
  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://121.193.130.195:8080";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  
  @Parameters 
  public static Collection<Object[]> getData(){
	  	List<Object[]> array = new ArrayList<>();
		File csv = new File("E:\\inputgit.csv");  // CSV文件路径
	    BufferedReader br = null;
	    try
	    {
	        br = new BufferedReader(new FileReader(csv));
	    } catch (FileNotFoundException e)
	    {
	        e.printStackTrace();
	    }
	    String line = "";
	    String everyLine = "";
	    try {
	        
	    		line = br.readLine();
	            while ((line = br.readLine()) != null)  //读取到的内容给line变量
	            {
	                everyLine = line;
	                String [] temp = everyLine.split(",");
	                //System.out.println(temp[0]);
	                Object [] tempobj = new Object[]{
	                		temp[0],temp[0].substring(4,temp[0].length()),temp[2]};
	                array.add(tempobj);
	            }
	    } catch (IOException e)
	    {
	        e.printStackTrace();
	    }
		return array;
	  }
  
  @Test
  public void testLab2() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys(this.studentId);
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys(this.passwd);
    driver.findElement(By.id("submit")).click();
    Object gitAddress = driver.findElement(By.xpath("//*[@id='table-main']/tr[3]/td[2]")).getText();
    System.out.println(gitAddress.toString());
    assertEquals(this.github, gitAddress);
    
  }

  @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;
    }
  }

}
测试结果:



本文Github源码下载


P.S.文章不妥之处还望指正


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值