最近在考虑怎么做一个项目的自动化测试,咨询下来,用selenium的比较多,于是也想写个demo测试下。
首先,我们需要添加依赖
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
这里我们选择用chrome作为测试浏览器,需要去下载chormedriver,下载地址:https://sites.google.com/a/chromium.org/chromedriver/
将下载的文件放到 /Users/bane/develop/webdriver/
下面来写段测试代码
package com.ms.selenium;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class App {
static {
System.setProperty("webdriver.chrome.driver","/Users/bane/develop/webdriver/chromedriver");
}
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10).getSeconds());
try {
driver.get("[http://www.baidu.com/](http://www.baidu.com/)");
driver.findElement(By.id("kw")).sendKeys("美女" + Keys.ENTER);
} finally {
// driver.quit();
}
}
}
来跑一下试试