准备工作
- 下载chrome驱动
- 下载jdk
- 下载maven
- 下载IntelliJ IDEA,配置下载的maven
- 设置maven的setting.xml
<localRepository>D:\maven\repository</localRepository> #设置本地仓库
....
# 设置阿里云镜像下载
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
开始
- IntelliJ IDEA,创建一个maven项目
- 设置pox.xml中的依赖项
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
- 代码准备
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
public class test {
public WebDriver driver;
@Test
public void main() {
driver.navigate().to("http://www.baidu.com");
String strPageTitle = driver.getTitle();
System.out.println("Page title: - "+strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("百度一下,你就知道"), "Page title doesn't match");
}
@BeforeClass
public void beforeMethod() {
File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.baidu.com");
}
@AfterClass
public void afterMethod() {
driver.quit();
}
}
执行结果