import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTest {
public static void main(String[] args) throws IOException {
String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
webDriver.get("https://www.baidu.com/");
webDriver.manage().window().maximize();
//点击登陆按钮
webDriver.findElement(By.xpath("//*[@id='u1']/a[7]")).click();
webDriver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys("用户名称");
webDriver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys("用户密码");
webDriver.findElement(By.id("TANGRAM__PSP_10__verifyCode")).sendKeys("验证码");
webDriver.findElement(By.id("TANGRAM__PSP_10__submit")).click();
//在登录后界面查找用户名是否存在,获得用户名
String username = webDriver.findElement(By.id("user-name")).getText();
//判断是否一致,设置断言
if (username.contains("用户名称")) {
System.out.print(true);
} else {
System.out.print(false);
}
webDriver.close();
}
}
Eclipse 安装 testng 在网上有很多内容,这里就不叙述;安装完成 testng 后,在 maven 的 pom.xml 文件引入 testng 包:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
testng 包引入完成后,新建一个 class 文件,如下图 在输入 @Test
后,通过ATL+ /
显示出下图,选择 Testng 的注解;
下面代码就是一个 Testng 框架的测试代码基础:
import org.testng.annotations.Test;
public class SeleniumTest1 {
@Test
public void test() {
}
}
将百度 UI 实现 webDriver 代码放入到 testng 中:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class SeleniumTest1 {
@Test
public void test() throws IOException {
// 通过查找当前路径返回一个规范化路径,这样可以把工程放到不同地方执行了
String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
webDriver.get("https://www.baidu.com/");
webDriver.manage().window().maximize();
webDriver.findElement(By.xpath("//*[@id='u1']/a[7]")).click();
webDriver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys("username");
webDriver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys("password");
webDriver.findElement(By.id("TANGRAM__PSP_10__verifyCode")).sendKeys("验证码");
webDriver.findElement(By.id("TANGRAM__PSP_10__submit")).click();
String username = webDriver.findElement(By.id("user-name")).getText();
if (username.contains("username")) {
System.out.print(true);
} else {
System.out.print(false);
}
webDriver.close();
}
}
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class SeleniumTest1 {
WebDriver webDriver;
@BeforeMethod
public void beforMethod() throws IOException {
// 业务无关,启动配置相关代码
String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
public void test() {
webDriver.get("https://www.baidu.com/");
webDriver.manage().window().maximize();
webDriver.findElement(By.xpath("//*[@id='u1']/a[7]")).click();
webDriver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys("username");
webDriver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys("password");
webDriver.findElement(By.id("TANGRAM__PSP_10__verifyCode")).sendKeys("验证码");
webDriver.findElement(By.id("TANGRAM__PSP_10__submit")).click();
String username = webDriver.findElement(By.id("user-name")).getText();
if (username.contains("username")) {
System.out.print(true);
} else {
System.out.print(false);
}
}
@AfterMethod
public void afterMethod() {
// 业务无关代码
webDriver.quit();
}
}