|
1、通过webdriver做web自动化测试
2、maven 依赖
- <dependency>
- <groupId>org.seleniumhq.selenium</groupId>
- <artifactId>selenium-java</artifactId>
- <version>2.53.1</version> - - 注意,这个版本号要更新,否则操作新版本的浏览器会出错。
- </dependency>
复制代码 3、code
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.openqa.selenium.firefox.FirefoxDriver;
-
- public class ChromeTest1 {
-
- private String baseUrl = "http://www.baidu.com/";
-
- @Test
- public void testChrome() throws InterruptedException{
- //chrome没有自带driver,需要下载一个driver
- System.setProperty("webdriver.chrome.driver", "F:\\软件\\chromedriver_win32-2.14\\chromedriver.exe");
- ChromeOptions options = new ChromeOptions();
- //设置用户数据,否则每次都以新用户打开,浏览器界面不好看
- options.addArguments("user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data");
- WebDriver chromdriver = new ChromeDriver(options);
-
- test(chromdriver);
-
- chromdriver.close();
- chromdriver.quit();
- }
-
- @Test
- public void testFireFox() throws InterruptedException{
- WebDriver firefoxdriver = new FirefoxDriver();
-
- test(firefoxdriver);
-
- firefoxdriver.close();
- firefoxdriver.quit();
- }
-
- private void test(WebDriver driver) throws InterruptedException {
- driver.get(baseUrl);
-
- WebElement element = driver.findElement(By.id("kw"));
- element.clear();
- element.sendKeys("webDriver");
-
- element = driver.findElement(By.id("su"));
- element.click();
-
- Thread.sleep(1*1000);
- }
- }
复制代码
|
|