1.打开IDEA软件
2.在工具栏中点击File,下拉框中找到New-》Project
3.点击Maven -》NEXT
4.显示以下界面,修改名称,点击Finish完成
5.显示以下界面,选择pom.xml文件,在</properties>下面添加以下代码;
添加以下代码:
<dependencies>
<dependency>
<groupId>org.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
6.在工具栏中file-》Settings,搜索框中输入maven,找到Maven文件,把事先处理好的文件settings.xml导入进来,下面的repository可自动显示repository文件,点击应用后关闭该界面。
7.回到以下界面,找到右边的Maven,显示Lifecycle-》clean,install,分别右击,选择Run Maven Build
8.跑通后显示以下界面,说明maven的信息已建立成功
9.新建一个包;
10.新建一个包完成后,在这个包下再新建Java class,即可在上面写代码
案例练习
Test
在输入框中输入信息
public void demo(){
driver.get("http://localhost:8080/demo.html");
driver.findElement(By.id("user")).sendKeys("学习JAVA",Keys.ENTER);
打开超链接
public void demo(){
driver.get("http://localhost:8080/demo.html");
WebElement baidu = driver.findElement(By.className("baidu"));
Actions actions = new Actions(driver);
actions.click(baidu).perform();
识别置灰按钮,并输出“按钮不可用”
public void demo(){
driver.get("http://localhost:8080/demo.html");
WebElement Button =driver.findElement(By.id("button"));
if (Button.isEnabled()) {
System.out.println("按钮不可用!");
}
}
窗口句柄切换 并输出“title信息”
public void demo(){
driver.get("http://localhost:8080/demo.html");
WebElement newwidows= driver.findElement(By.className("open"));
Actions actions = new Actions(driver);
actions.click(newwidows).perform();
String openwidows = driver.getWindowHandle();
Set<String> windonwHandles = driver.getWindowHandles();
for (String windowHandle:windonwHandles) {
if (windowHandle.equals("openwidows")) {
driver.close();
continue;
}else {
driver.switchTo().window(windowHandle);
}
}System.out.println(driver.getTitle());
}