Selenium上机实验说明

Selenium上机实验


实验环境:firefox44

SeleniumIDE使用

  1. 在火狐浏览器上安装安装SeleniumIDE
  2. 有时候会在工具栏看不见SeleniumIDE的图标,所以按照一下方法进行定制操作,将seleniumIDE拖到工具栏。
  3. 打开Selennium,点击录制,开始登录指定网站,输入密码账号,点击登录,然后在需要判断的地方勾选右键添加aseert判断。
  4. 运行录制样本。

编写Selenium Java WebDriver

  1. 首先遇到版本问题,WebDriver driver = new Driver()一直报错,在3.3版本使用时,说需要下载geckodriver,然后添加System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe"),但是还是不能使用。又折腾回selenium2.53版本,还是疯狂报错,最后猜想可能是firefox版本太高,所以又卸载firefox52,安装老版本firefox44,用selenium2.53版本就可以通过了。
  2. 接着遇到断言问题,在老师帮助下,下载了fireBug和firepath,右键选定判断的内容,点击inspect in firepath,然后获取xpath路径,在测试环境中使用xpath获取内容
    String id = driver.findElement(By.xpath(".//*[@id='table-main']/tr[2]/td[2]")).getText();
    String github = driver.findElement(By.xpath(".//*[@id='table-main']/tr[3]/td[2]")).getText();
  3. 通过opencsv的api获取csv内容,在csv的list里面递归寻找id与web上相同的信息对应的github,然后用assertEqual判断web上和csv里面的是否相同assertEquals(github, csvGithub);
package webtest;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
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.opencsv.CSVReader;

public class WebTest {
  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://121.193.130.195:8080";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testWeb() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("3014218060");
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys("218060");
    driver.findElement(By.id("submit")).click();
    String id = driver.findElement(By.xpath(".//*[@id='table-main']/tr[2]/td[2]")).getText();
    String github = driver.findElement(By.xpath(".//*[@id='table-main']/tr[3]/td[2]")).getText();
    String csvGithub = getGithub(id);
    assertEquals(github, csvGithub);
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
  
  private String getGithub(String id) throws Exception
  {
	 CSVReader reader = new CSVReader(new FileReader("C:\\Users\\Kirin\\Desktopinputgit.csv"));
	 List<String[]> mylist = reader.readAll();
	 String result = "";
	 for(int i = 0;i<mylist.size();i++)
	 {
		 if(mylist.get(i)[0].equals(id))
			 result = mylist.get(i)[2];
	 }
	 return result;
  }
}
posted @ 2017-03-25 11:11  faith30  阅读(156)  评论(0编辑  收藏  举报