注意selenium3.x.x需下载各浏览器驱动程序 >jdk8
例如:firefox需geckodriver 下载路经:https://github.com/mozilla/geckodriver/releases
代码中指定firefox及其驱动程序路经或配置path
依赖包:
compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.4.0'
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
测试代码:
package com.zxing.seleniumjenkins;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.fail;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SeleniumJenkinsApplicationTests {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
//设置firefox.exe 路径
System.setProperty("webdriver.firefox.bin","E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//设置firefox驱动路经 selenium3.0之后需设置 firefox版本要求大于48
System.setProperty("webdriver.gecko.driver","E:\\java\\javaTool\\driver\\geckodriver.exe");
driver = new FirefoxDriver();
baseUrl = "https://www.katalon.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitledTestCase() throws Exception {
driver.get("https://www.baidu.com/");
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("test");
driver.findElement(By.id("form")).submit();
System.out.println("----over---");
}
@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;
}
}
}
参考:http://www.selenium.org.cn/1680.html
http://www.cnblogs.com/fengpingfan/p/5880139.html