Lab 2 Selenium:
一 、安装SeleniumIDE插件
1.下载火狐V42.0(下载地址:http://ftp.mozilla.org/pub/firefox/releases/42.0/win64/zh-CN/ )。
2.打开火狐浏览器,百度打开 https:
//addons.mozilla.org/zh-CN/firefox/addon/selenium-ide/versions/ ,选择Selenium IDE v2.9.1 ,在弹出窗口中选择安装-->立即重启即可。
![](https://img-blog.csdnimg.cn/img_convert/ef1fc032fb9aae56bc405fea3276b27f.png)
二 、学会使用SeleniumIDE录制脚本和导出脚本
1.打开Firefox,按Ctl+ Alt + S打开SeleniumIDE,或者直接点击firework菜单栏--->selenium图标。
![](https://img-blog.csdnimg.cn/img_convert/c8cb0ce7a108a7ddaad49745f60a3c5b.png)
2.确保SeleniumIDE处于录制状态
![](https://img-blog.csdnimg.cn/img_convert/e35d4aaf3faa0dda1af66676cf0753d8.png)
3.用firework访问https://psych.liebes.top/st,使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到你的git地址,再次点击红色按钮完成录制,并保存测试用例(文件—>Save Test Case) 。
![](https://img-blog.csdnimg.cn/img_convert/f4c14898324ceb2b64f5f84c94d1bdae.png)
4.导出为Java代码。
文件—>Export—>Java/ JUnit 4 /WebDriver
![](https://img-blog.csdnimg.cn/img_convert/05c340797a4ea478238c6ed1812d221c.png)
代码如下:
accessWebsite.java
package com.example.tests;
import java.util.regex.Pattern;
public class AccessWebsite {
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 = "https://psych.liebes.top/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testAccessWebsite() throws Exception {
driver.get(baseUrl + "/st");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("3015218070");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("218070");
driver.findElement(By.id("submitButton")).click();
driver.findElement(By.id("submitButton")).click();
driver.findElement(By.cssSelector("p.login-box-msg")).click();
}
@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;
}
}
}
三、编写Selenium Java WebDriver程序,测试input.xlsx表格中的学号和git地址的对应关系是否正确。
1.下载Selenium Java 2.53.1(下载地址:http://selenium-release.storage.googleapis.com/index.html?path=2.53/ )。
![](https://img-blog.csdnimg.cn/img_convert/3b3463dd38a81f32d15a37f567186a27.png)
2. 打开eclipse,新建项目lab2,导入selenium-java-2.53.1.jar,selenium-java-2.53.1-srcs.jar 和 libs文件夹里的所有jar包。
项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add External JARs-->OK。
3. 下载jxl-2.6.10.jar(下载地址:http://maven.ibiblio.org/maven2/net/sourceforge/jexcelapi/jxl/ ),用于读取input.xlsx(excel文件),并导入项目lab2,同时导入hamcrest-all-1.3.jar(下载地址见上一篇博客),导入方法与步骤2一样。
![](https://img-blog.csdnimg.cn/img_convert/46cdbd465865e6c0b6c545e313a665bd.png)
4. 编写代码。
注意点:(1)excel表格中有的github地址后面带有"/",建议都去掉"/"再比较
if(info.endsWith("/")) {
info = info.substring(0, info.length()-1); } if(address.endsWith("/")) { address = address.substring(0, address.length()-1);
}
(2)从excel表格中获取的github地址以及从页面获得的地址,都用trim()去掉首尾空格后再比较是否一致。
代码如下:
Selenium.java
package selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Selenium {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
String info=new String();
String address=new String();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","E:/大三下/软件测试/chromedriver.exe"); // 此处PATH替换为你的chromedriver所在路径
driver = new ChromeDriver();
baseUrl = "https://psych.liebes.top/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test1() throws Exception {
//读取excel文件
File file = new File("E:/大三下/软件测试/input.xls");
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// 为页签sheet 1创建一个Sheet对象
Sheet sheet = wb.getSheet(0);
for (int i = 0; i < sheet.getRows(); i++) {
//按列名读取这条记录的值
String number = sheet.getCell(0, i).getContents(); //读取学号
String pwd = number.substring(number.length()-6,number.length()); //读取密码
address = sheet.getCell(1, i).getContents().trim(); //读取github地址,记得用trim去掉首尾空格
//访问给定网址
driver.get(baseUrl + "/st");
//输入用户名
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(number);
//输入密码
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(pwd);
//点击登录按钮
driver.findElement(By.id("submitButton")).click();
//登录成功之后,获得当前页面的用户信息
info = driver.findElement(By.tagName("p")).getText().trim();
//excel表格中有的github地址后面带有"/",建议都去掉"/"再比较
if(info.endsWith("/")) {
info = info.substring(0, info.length()-1);
}
if(address.endsWith("/")) {
address = address.substring(0, address.length()-1);
}
//比较查询信息
if(info.equals(address)) {
assertEquals(info,address);
System.out.println(number+"的信息一致.");
}
else {
System.out.println(number+"的信息不一致.");
}
}
driver.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@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;
}
}
}
5. 运行结果截图
![](https://img-blog.csdnimg.cn/img_convert/3be7ca9b0418f87004687cef8dfb0eda.png)
![](https://img-blog.csdnimg.cn/img_convert/ad7df19831bf57915e28743213af403b.png)