一、BaseUI
-
工具类的基本内容
点击
输入
启动浏览器
关闭浏览器
@Beforeclass:在所有类型之前执行
@Afterclass:在所有类之后执行
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BaseUI {
//这个变量的类型是webDriver 网站驱动
public WebDriver driver;
public String pageTitle;
public String pageUrl;
public BaseUI(){
}
public BaseUI(WebDriver driver){
this.driver=driver;
}
//在所有类之前执行
@BeforeClass
public void before(){
//打开浏览器
// 设置环境变量,指定chromedriver的路径
System.setProperty("webdriver.chrome.driver",
"src/main/resources/selenium/driver_v236_63_65/chromedriver.exe");
// 设置浏览器的参数
ChromeOptions options = new ChromeOptions();
// 最大化浏览器
options.addArguments("--test-type", "--start-maximized");
// options.setBinary("C:/XXXXXXX/chrome.exe");
// 打开浏览器
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
sleep(1000);
}
@Test
@Parameters({"url"})
public void openUrl(String url){
//打开URL
driver.get(url);
sleep(1000);
}
//在所有类执行之后执行
@AfterClass
public void after(){
//关闭浏览器
sleep(2000);
//浏览器退出
driver.quit();
}
//封装的线程等待方法
public static void sleep(int millis) {
try {
Thread.sleep(millis*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装的截图的方法 传一个driver ,传一个图片的名字
public static void snapshot(TakesScreenshot drivername, String filename) {
// this method will take screen shot ,require two parameters ,one is
// driver name, another is file name
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy
// somewhere
try {
System.out.println("save snapshot path is:c:/" + filename);
FileUtils.copyFile(scrFile, new File("c:\\" + filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
} finally {
System.out.println("screen shot finished");
}
}
/*
* 在文本框内输入字符
*/
protected void text(WebElement element, String text) {
try {
if (element.isEnabled()) {
element.clear();
System.out.println("Clean the value if any in "
+ element.toString() + ".");
element.sendKeys(text);
System.out.println("Type value is: " + text + ".");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 点击元素,这里指点击鼠标左键
*/
protected void click(WebElement element) {
try {
if (element.isEnabled()) {
element.click();
System.out.println("Element: " + element.toString()
+ " was clicked.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 在文本输入框执行清除操作
*/
protected void clean(WebElement element) {
try {
if (element.isEnabled()) {
element.clear();
System.out.println("Element " + element.toString()
+ " was cleaned.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 判断一个页面元素是否显示在当前页面
*/
protected void verifyElementIsPresent(WebElement element) {
try {
if (element.isDisplayed()) {
System.out.println("This Element " + element.toString().trim()
+ " is present.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 获取页面的标题
*/
protected String getCurrentPageTitle() {
pageTitle = driver.getTitle();
System.out.println("Current page title is " + pageTitle);
return pageTitle;
}
/*
* 获取页面的url
*/
protected String getCurrentPageUrl() {
pageUrl = driver.getCurrentUrl();
System.out.println("Current page title is " + pageUrl);
return pageUrl;
}
public void switchToNextWindow() {
String currentWindow = driver.getWindowHandle();// 获取当前窗口句柄
Set<String> handles = driver.getWindowHandles();// 获取所有窗口句柄
System.out.println("当前窗口数量: " + handles.size());
for (String s : handles) {
if (currentWindow.endsWith(s)) {
continue;
} else {
try {
WebDriver window = driver.switchTo().window(s);// 切换到新窗口
System.out
.println("new page title is " + window.getTitle());
break;
} catch (Exception e) {
System.out.println("法切换到新打开窗口" + e.getMessage());
}
}
}
}
//根据页面标题切换窗口
public void switchToTitleWindow(String windowTitle) {
// 将页面上所有的windowshandle放在入set集合当中
String currentHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String s : handles) {
driver.switchTo().window(s);
// 判断title是否和handles当前的窗口相同
if (driver.getTitle().contains(windowTitle)) {
break;// 如果找到当前窗口就停止查找
}
}
}
}
-
方便调用
不带参数的直接使用
带参数需要在括号内传参
二、maven使用
-
百度 添加依赖包
-
找到 dependecy
-
把依赖包的jar包信息放在pom文件内:pom.xml,放在<dependencies>内部</dependencies>
-
maven使用
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<!-- dependency描述具体依赖的第三方开发jar-->
<dependency>
<!-- groupid 描述域名 公司名 org一般是开源 -->
<groupId>org.apache.httpcomponents</groupId>
<!-- 工程名 http客户端-->
<artifactId>httpclient</artifactId>
<!--version版本-->
<version>4.5.5</version>
</dependency>
<!-- soap接口 -->
<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.4</version>
</dependency>
<!-- selenium界面自动化 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.50.0</version>
</dependency>
<!--testNG测试框架 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
<!-- 读取csv文件 -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
-
maven优点自动化添加依赖jar包所有依赖的jar包,高效解决层层依赖的关系
-
管理开发依赖jar包
-
依赖第三方代码位置本地仓库-官方仓库
-
官方仓库地址:http://repo1.maven.org/maven2/
-
本地仓库(按配置找selnium和testng)
-
位置C:\software\apache-maven-3.5.0\repository\org\seleniumhq\selenium\selenium-iphone-driver\2.25.0
https://www.tapd.cn/tfl/captures/2018-11/tapd_63882484_base64_1542358952_90.png
-
在线位置:https://repo1.maven.org/maven2/org/seleniumhq/selenium/
maven配置依赖
工程中使用pom文件配置依赖
maven依赖配置 pom.xml 依赖配置
<dependency> 依赖
<groupId>org.jvnet.localizer </groupId> 域名 公司名
<artifactId>localizer </artifactId> 项目名称
<version>1.8 </version> 版本
</dependency>
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
-
添加依赖包录像
https://www.tapd.cn/tfl/pictures/201811/tapd_63882484_1542360088_78.gif
三、java三大容器
-
固定长度大小的容器
public void demo1(){
String users [][]={
{"hj12304","qweasd1"},
{"hj12305","qweasd2"},
{"hj12306","qweasd3"},
{"hj12307","qweasd4"},
{"hj12308","qweasd5"}
};
for (int i = 0; i <5 ; i++) {
System.out.println(users[i][0]);
System.out.println(users[i][1]);
}
}
-
变化长度的容器
public void map(){
//Map 用来存储键值对数据 取数据通过键来取值
Map<String,Integer> m= new HashMap<String, Integer>();
m.put("李中洋",21);
m.put("杨草原",20);
m.put("周雄",18);
//System.out.printf("李中洋的年龄是"+m.get("李中洋"));
System.out.println("李中洋的年龄是"+m.get("李中洋"));
System.out.println("周雄的年龄是"+m.get("周雄"));
}
-
存键值对的容器
public void mp(){
//Map用来存储键值对数据 取数据通过键来取值
Map<String,Integer> m= new HashMap<String, Integer>();
m.put("李中洋",21);
m.put("杨草原",20);
m.put("周雄",18);
//不换行打印
System.out.printf("李中洋的年龄是"+m.get("李中洋"));
//换行打印
System.out.println("周雄的年龄是"+m.get("周雄"));
System.out.println("周雄的年龄是"+m.get("周雄"));
}
四、pom配置信息(pom.xml)
项目依赖
插件
目标
建立档案
项目版本
开发商
邮件列表
- 添加下拉框,学历,
import com.google.common.annotations.VisibleForTesting;
import com.guoyasoft.autoUI.common.BaseUI;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @program: guoya-test.git
* @description:
* @author: Administrator
* @create: 2018-11-13 11:34
**/
public class GuoyaLogin extends BaseUI {
//实例变量
private String username = "guoya718";
private String password = "qweasd";
private String age = "32";
private String realName = "asdfg";
private String users[] = {"yee08", "yee07", "yee06", "yee05", "yee04", "yee03", "yee02",
"yee01", "yee00", "yee009"};
// private System
//public 公开的方法 void 无返回 login()方法名
//添加teseng注解用来执行测试方法
@Test
public void login() {
//设置循环 起始值,最大值/最小值,增量,减量
for (int i = 0; i <users.length; i++) {
System.out.println("当前循环此时"+i);
String url = "http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp";
driver.get(url);
//查询元素根据name查找 然后执行清除
driver.findElement(By.name("userName")).clear();
//查找元素根据name查找 执行输入
// driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(users[i]);
send("//input[@name='userName']",users[i]);
sleep(1000);
//driver.findElement(By.id("password")).clear();
clear("password");
// driver.findElement(By.id("password")).sendKeys(password);
pass("password",password);
sleep(1000);
driver.findElement(By.xpath("//input[@name='checkCode']")).sendKeys("12345");
sleep(1000);
//driver.findElement(By.xpath("//input[@id='loginBtn']")).click();
click("//input[@id='loginBtn']");
//String source=driver.getPageSource();
//boolean 布尔类型 变量类型 true 真 else 假
//boolean guoya = driver.getPageSource().contains("学生查询");
//assert断言 判断预期结果与实际结果是否相等
//Assert.assertEquals(guoya ,true);
queryeducation("大专");
sleep(1000);
//切换iframe窗口至结果展示窗口
driver.switchTo().frame("result");
//判断切换结果展示页面是否包含查询用户
//Assert.assertEquals(driver.getPageSource().contains(users[0]),true);
//打印新的页面源代码
System.out.println(driver.getPageSource());
//assert断言 判断预期结果与实际结果是否相等
//Assert.assertEquals(guoya,true,"用户登录页面失败");
//切换回默认窗口
//driver.switchTo().defaultContent();
//if (guoya==true){
// System.out.println("登录成功");
//}else{
// System.out.println("登录失败");
//}
//queryalluser();
//queryrealname("jiyiang","20");
//queryrealname("","");
//queryrealname("","");
}
}
@Test
public void signup() {
for (int i = 0; i < users.length; i++) {
//条件成立测一直执行循环,条件不满足条件结束
System.out.println("当前循环此时" + users.length);
driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/signUp.jsp");
//清除用户名
//WebElement element = driver.findElement(By.xpath("//input[@id='userName']"));
//element.click();
//element.sendKeys("wzj1234");
//element.sendKeys("");
driver.findElement(By.xpath("//input[@id='userName']")).clear();
driver.findElement(By.xpath("//input[@id='userName']")).sendKeys(users[i]);
System.out.println("当前用户名为" + users[i]);
driver.findElement(By.xpath("//input[@id='realName']")).sendKeys(realName);
System.out.println("当前真实姓名是" + realName);
driver.findElement(By.xpath("//input[@id='password']")).sendKeys(password);
driver.findElement(By.xpath("//input[@id='password2']")).sendKeys(password);
driver.findElement(By.xpath("//input[@id='phone']")).sendKeys("13460235689");
driver.findElement(By.xpath("//input[@id='age']")).sendKeys("20");
driver.findElement(By.xpath("//input[@id='checkCode']")).sendKeys("1234");
driver.findElement(By.xpath("//input[@id='submitBtn']")).click();
//弹出窗口 是否确定
driver.switchTo().alert().accept();
sleep(2000);
boolean result = driver.getPageSource().contains("登录页面");
//boolean result=driver.getPageSource().contains("登录界面");
//如果条件为真 打印注册成功
//if(result==true){
//System.out.println("用户注册成功");
//否侧就是注册失败
//}else{
// System.out.println("用户注册失败");
}
//Alert alert = driver.switchTo().alert();
//alert.accept();
//alert.dismiss();
//点击确认
}
public void queryalluser() {
driver.findElement(By.xpath("//input[@type='submit']")).clear();
driver.findElement(By.xpath("//input[@type='submit']")).click();
// sleep(5000);
}
public void queryuser() {
driver.findElement(By.xpath("//input[@name='userName']")).clear();
driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(users[0]);
driver.findElement(By.xpath("//input[@type='submit']")).click();
//sleep(5000);
}
public void queryage() {
driver.findElement(By.xpath("//input[@name='userName']")).clear();
driver.findElement(By.xpath("//input[@type='number'][1]")).clear();
driver.findElement(By.xpath("//input[@type='number'][1]")).sendKeys(age);
driver.findElement(By.xpath("//input[@type='submit']")).click();
// sleep(5000);
}
public void queryrealname(String name,String age) {
driver.findElement(By.xpath("//input[@name='userName']")).clear();
driver.findElement(By.xpath("//input[@type='number'][1]")).clear();
driver.findElement(By.xpath("//input[@type='number'][1]")).sendKeys(age);
driver.findElement(By.xpath("//input[@type='submit']")).click();
}
public void click(String xpath){
driver.findElement(By.xpath(xpath)).click();
}
public void send(String xpat,String sendkeys){
driver.findElement(By.xpath(xpat)).clear();
driver.findElement(By.xpath(xpat)).sendKeys(sendkeys);
}
public void clear(String id){
driver.findElement(By.id(id)).clear();
}
public void pass(String id,String sendKeys) {
driver.findElement(By.id(id)).sendKeys(sendKeys);
}
public void queryage(String age){
driver.findElement(By.xpath("//input[@type='number'][1]")).clear();
driver.findElement(By.xpath("//input[@type='number'][1]")).sendKeys(age);
driver.findElement(By.xpath("//input[@type='submit']")).click();
}
public void queryeducation(String education){
//定位select下拉框使用select变量进行储存
WebElement element= driver.findElement(By.xpath("//select[@name='education']"));
Select select = new Select(element);
//使用select选项 根据文本内容选择下拉框
select.selectByVisibleText(education);
driver.findElement(By.xpath("//input[@type='submit']")).click();
sleep(2000);
}
@Test
public void queryclassType(String classname){
WebElement ele=driver.findElement(By.xpath(""));
Select select = new Select(ele);
select.deselectByVisibleText(classname);
click("//input[type='submit']");
}
}
-
思维导图: