selenium java环境配置
1.将selenium 自动化测试环境搭建 博客中的测试case 导出成java Junit代码,如图:
2.配置项目环境将Junit4 和xerces添加到classpath中.如图
3.添加maven配置
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
4.将导出代码粘如建好的java项目中
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
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:/soft/Mozilla Firefox/firefox.exe");
//添加webdriver路径
System.setProperty("webdriver.gecko.driver", "D:/360安全浏览器下载/geckodriver-v0.23.0-win64/geckodriver.exe");
driver = new FirefoxDriver();
baseUrl = "https://start.firefoxchina.cn/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@org.junit.Test
public void test() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("search-key")).clear();
driver.findElement(By.id("search-key")).sendKeys("sinat_33540867的博客");
driver.findElement(By.id("search-submit")).click();
driver.findElement(By.cssSelector("em")).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;
}
}
}
5.运行测试方法,测试测试成功
chrome 浏览器测试配置
替换这两行代码
System.setProperty("webdriver.chrome.bin","C:/Users/FITECH/AppData/Local/Google/Chrome/Application/chrome.exe");
System.setProperty("webdriver.chrome.driver", "D:/360安全浏览器下载/chromedriver_win32/chromedriver.exe");
chrome 版本为68.0(这个浏览器我装的太早了所以没有资源,望见谅)
提供的ebdriver.chrome.driver 虽然是32位的,但是我的chrome 是64位的也能运行
相关软件链接:链接:https://pan.baidu.com/s/1y4kUd_pEjk2EeTWJHRClWg
提取码:v836
想使用其他版本的浏览器 下边是相关链接,希望对读者有些帮助
火狐历史版本下载地址:https://ftp.mozilla.org/pub/firefox/releases/
chromedriver下载地址http://chromedriver.storage.googleapis.com/index.html
chromedriver与chrome版本映射表:https://blog.csdn.net/huilan_same/article/details/51896672