作者:雪莲2007019 | 来源:互联网 | 2023-09-16 09:59
一、安装Selenium在Eclipse中导入geckodriver,然后内置插件中安装即可。二、安装Firefox和SeleniumIDE插件先安装Firefox,然后搜索Fir
一、安装Selenium
在Eclipse中导入geckodriver,然后内置插件中安装即可。
二、安装Firefox和Selenium IDE插件
先安装Firefox,然后搜索Firefox add-on,从插件中搜索Selenium即可。
三、录制导出代码
打开Selenium,开始录制,对任意学号完成一次操作。然后将录制完成的视频导出为Java(WebDriver+Junit)格式。
注:这里可以用katalon替代。
四、完成自动化测试
可利用Junit的自动化测试对单一操作代码进行改写,使之实现自动化测试。在导入数据的时候有两个需要处理的细节:由于原数据第一行为表头,因此在读取数据的时候需要额外进行一次readLine();由于最后可能会有空行,而split函数在执行时不会对csv的空值进行操作,因此会产生数组越界错误。最简便的解决方法是手动删除空行。关键代码如下:
TestLab2.java:
package testWeb;
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.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 TestLab2 {
private String id;
private String password;
private String github;
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public TestLab2(String id,String password,String github) {
this.id = id;
this.password=password;
this.github=github;
}
@Parameters
public static Collection<String[]>getData() throws IOException{
Collection<String[]> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\dell\\Desktop\\ST\\info.csv"));
br.readLine();
String temp = br.readLine();
temp = br.readLine();
while(temp != null) {
String [] split = new String[3];
String[] result = temp.split(",");
split[0] = result[1];
split[1] = result[1].substring(4);
split[2] = result.length == 4 ? result[3] : "";
list.add(split);
temp = br.readLine();
}
return list;
}
@Before
public void setUp() throws Exception {
String driverPath = System.getProperty("user.dir") + "/src/resource/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
baseUrl = "https://www.katalon.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testBaidu() throws Exception {
driver.get("http://121.193.130.195:8800/login");
driver.findElement(By.name("id")).click();
driver.findElement(By.name("id")).clear();
driver.findElement(By.name("id")).sendKeys(id);
driver.findElement(By.name("password")).click();
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("btn_login")).click();
String studentGit = driver.findElement(By.id("student-git")).getText();
driver.findElement(By.id("btn_logout")).click();
assertEquals(github,studentGit);
}
@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;
}
}
}