2018-11-13 day21UI自动化编码
2018-11-13 本文已影响0人
凡星点点5
一、导入项目
1认识界面.
微信图片_20181113193914.png
2.在线导入
![](https://img.haomeiwen.com/i14481160/e7961634cceaf038.png)
![](https://img.haomeiwen.com/i14481160/6c653e24f97400c2.png)
3.本地导入
![](https://img.haomeiwen.com/i14481160/478e5afc7fb17750.png)
![](https://img.haomeiwen.com/i14481160/80555d7a342e3baf.png)
![](https://img.haomeiwen.com/i14481160/7cb788b08347cd06.png)
![](https://img.haomeiwen.com/i14481160/92ca304676e11063.png)
![](https://img.haomeiwen.com/i14481160/e5bc24dad823e11b.png)
二、创建文件夹及标签
![](https://img.haomeiwen.com/i14481160/cd9e36f4deb48546.png)
![](https://img.haomeiwen.com/i14481160/f197deae2ce80632.png)
三、通过命令行操作导入
![](https://img.haomeiwen.com/i14481160/1a85077d3604e533.png)
名词
1.新建业务类
2.继承 extends 父类 BaseUI
3.新建方法
4.新建党阀使用 @test注解
driver:驱动
findelement:查找元素
clear:清除
senkeys:输入
click:点击
alert:弹窗
accept:同意
switchTo:切换
八种定位方式:
id定位
name定位
class name class样式定位
tag name 根据标签名定位
link text 文本链接定位
partial link text 文本链接模糊匹配定位
xpath:路径定位
css selector 样式表达式定位
对象/变量:Webelement,Alert
5.右键执行类
6.右键执行方法
7.新建textNG配置文件
xml控制脚本流程
suite:套件
test:测试集
classes:类集合
class:类
methods:方法
include:方法名
四、login+signup
package com.guoyasoft.autoUI.guoya_1810;
//引入java代码路径
import com.guoyasoft.autoUI.common.BaseUI;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
public class GuoyaLogin extends BaseUI {
//public 公开的方法 void 无返回 login() 方法名
//添加testng注解用来执行测试方法
@Test
public void login(){
//打开网页
driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp");
//网页休眠,等待2s
sleep(2000);
//查找元素,根据name查找,然后执行清除
driver.findElement(By.name("userName")).clear();
//查找元素,根据name查找,执行输入
driver.findElement(By.name("userName")).sendKeys("guoya888");
sleep(1000);
//查找元素,根据id查找,输入密码
driver.findElement(By.id("password")).sendKeys("qweasd");
sleep(1000);
//查找元素,根据name查找,输入验证码
driver.findElement(By.name("checkCode")).sendKeys("12345");
sleep(1000);
//查找元素,根据id查找,点击
driver.findElement(By.xpath("//input[@id='loginBtn']")).click();
sleep(5000);
}
@Test
public void signup(){
//打开网页
driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/signUp.jsp");
sleep(1000);
//查找用户框,清空,两种写法
driver.findElement(By.xpath("//input[@id='userName']")).clear();
driver.findElement(By.xpath("//input[@id='userName']")).sendKeys("zxxc9999");
//第二种写法先对driver.findElement(By.xpath("//input[@id='userName']"));进行变量命名为element,然后用element进行编辑
WebElement element=driver.findElement(By.xpath("//input[@id='userName']"));
element.clear();
element.sendKeys("zxxc9999");
sleep(1000);
driver.findElement(By.xpath("//input[@id='realName']")).sendKeys("周雄");
sleep(1000);
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("qweasd");
sleep(1000);
driver.findElement(By.xpath("//input[@id='password2']")).sendKeys("qweasd");
sleep(1000);
driver.findElement(By.xpath("//input[@id='phone']")).sendKeys("18311118888");
sleep(1000);
driver.findElement(By.xpath("//input[@id='age']")).sendKeys("23");
sleep(1000);
driver.findElement(By.xpath("//input[@id='checkCode']")).sendKeys("1234");
sleep(1000);
driver.findElement(By.xpath("//input[@id='submitBtn']")).click();
sleep(100);
//切换弹窗-选择接收两种写法
driver.switchTo().alert().accept();
//切换弹窗-把driver.switchTo().alert()当成一个固定模板,进行命名当做变量
Alert alert=driver.switchTo().alert();
alert.accept();
alert.dismiss();
}
}
SearchBaidu
package com.guoyasoft.autoUI.guoya_1810;
import com.guoyasoft.autoUI.common.BaseUI;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
public class SearchBaidu extends BaseUI {
@Test
public void search(){
driver.get("https://www.baidu.com/");
driver.findElement(By.xpath("//input[@name='wd']")).sendKeys("京东");
driver.findElement(By.xpath("//input[@id='su']")).click();
sleep(5000);
}
}
控制脚本流程.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--suite 代表套件-->
<suite name="ZX">
<!--test 代表测试用例集-->
<test name="test0" preserve-order="true" enabled="true">
<!--class 代表类集合可以执行多个类-->
<classes>
<!--class 代表单个类-->
<class name="com.guoyasoft.autoUI.guoya_1810.GuoyaLogin">
<!--methods 代表方法集-->
<methods>
<!--include 代表方法名-->
<include name="signup" />
<include name="login" />
</methods>
</class>
<class name="com.guoyasoft.autoUI.guoya_1810.SearchBaidu">
<!--methods 代表方法集-->
<methods>
<!--include 代表方法名-->
<include name="search" />
</methods>
</class>
</classes>
</test>
</suite>
![](https://img.haomeiwen.com/i14481160/4f3d37fee193a4fa.png)
![](https://img.haomeiwen.com/i14481160/0028661f2f6a55ac.png)