LAB2 软件测试 Selenium上机实验 2017

时间:2023-03-10 07:18:55
LAB2 软件测试 Selenium上机实验 2017

1、安装SeleniumIDE插件

打开Firefox——>菜单栏——>附加组件——>获取附加组件——>查看更多附加组件——>搜索框输入SeleniumIDE并查找——>选择SeleniumIDE并安装即可 。

2、学会使用SeleniumIDE录制脚本和导出脚本

打开Firefox——>工具——>Selenium IDE,此时已经开始录制,在Firefox的操作会被记录下来

在Selenium IDE插件中,文件——>Export Test Case As——>Java/JUnit 4/WebDriver(用WebDriver对于web自动化测试更方便)——>命名然后确定,脚本导出完成。

3.编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系是否正确。

package SeleniumTest;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; 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 java.io.File;
import java.io.FileReader;
import java.nio.charset.Charset;
import java.util.List;
import com.csvreader.CsvReader; public class TestSelenium {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer(); @Before
public void setUp() throws Exception {
System.setProperty("webdriver.firefox.bin","D:/浏览器/firefox.exe");
//driver = new FirefoxDriver();
baseUrl = "http://121.193.130.195:8080";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} @Test
public void test1() throws Exception { CsvReader r = new CsvReader("C:/Users/Administrator/Desktop/软件测试/inputgit.csv", ',',Charset.forName("utf-8"));
r.readHeaders();
while (r.readRecord()) {
String id = r.get("id");
String password = id.substring(4);
String address = r.get("address");
driver.get(baseUrl + "/"); driver.findElement(By.id("name")).clear();
driver.findElement(By.id("name")).sendKeys(id);
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("submit")).sendKeys(Keys.ENTER); WebElement text = driver.findElement(By.cssSelector("#table-main tr:last-child td:last-child"));
String address0 = text.getText();
assertEquals(address,address0);
}
r.close();
} @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;
}
}
}

注意事项:本实验要求 Firefox 版本不能过高且Selenium的Java包要和Firefox的版本兼容。我是用的是 firefox 43 和selenium-java-2.53.1