selenium-网站demo学习-test Design-优化自动化代码
看selenium的网站的文档,里面的自动化用例设计有一些小点很靠谱。学了很多,可以用作优化自己的代码。
1.测试类型:
Testing Static Content
Testing Links
Function Tests
Testing Dynamic Elements
Ajax Tests
Assert vs. Verify
assert 和verify的区别:assert如果wrong,就会停止执行后面的内容;verify如果wrong,会记录下来,然后继续执行后面的内容。
Choosing a Location Strategy
1.id和name是最高效速度最快的;
2.xpath是万能的
Wrapping Selenium Calls
包装一下selenium的方法,减少代码冗余
---click方法
原来的代码:
selenium.click(elementLocator);
selenium.waitForPageToLoad(waitPeriod);
优化后的代码:
/**
* Clicks and Waits for page to load.
*
* param elementLocator
* param waitPeriod
*/
public void clickAndWait(String elementLocator, String waitPeriod) {
selenium.click(elementLocator);
selenium.waitForPageToLoad(waitPeriod);
}
----操作元素,这个其实有用过,但是没有全面包装:
/**
* Selenium-WebDriver -- Clicks on an element only if it is available on a page.
*
* param elementLocator
*/
public void safeClick(String elementLocator) {
WebElement webElement = getDriver().findElement(By.XXXX(elementLocator));
if(webElement != null) {
selenium.click(webElement);
} else {
// Using the TestNG API for logging
Reporter.log("Element: " + elementLocator + ", is not available on a page - "
+ getDriver().getUrl());
}
}
‘Safe Operations’ for Element Presence
/**
* Selenium-WebDriver -- Clicks on an element only if it is available on a page.
*
* param elementLocator
*/
public void safeClick(String elementLocator) {
WebElement webElement = getDriver().findElement(By.XXXX(elementLocator));
if(webElement != null) {
selenium.click(webElement);
} else {
// Using the TestNG API for logging
Reporter.log("Element: " + elementLocator + ", is not available on a page - "
+ getDriver().getUrl());
}
}
User Interface Mapping
To summarize, a UI Map has two significant advantages
- Using a centralized location for UI objects instead of having them scattered throughout the script. This makes script maintenance more efficient.
 - Cryptic HTML Identifiers and names can be given more human-readable names improving the readability of test scripts.
 public void testNew() throws Exception {
selenium.open("http://www.test.com");
selenium.type("loginForm:tbUsername", "xxxxxxxx");
selenium.click("loginForm:btnLogin");
selenium.click("adminHomeForm:_activitynew");
selenium.waitForPageToLoad("30000");
selenium.click("addEditEventForm:_IDcancel");
selenium.waitForPageToLoad("30000");
selenium.click("adminHomeForm:_activityold");
selenium.waitForPageToLoad("30000");
}优化为
public void testNew() throws Exception {
selenium.open("http://www.test.com");
selenium.type(admin.username, "xxxxxxxx");
selenium.click(admin.loginbutton);
selenium.click(admin.events.createnewevent);
selenium.waitForPageToLoad("30000");
selenium.click(admin.events.cancel);
selenium.waitForPageToLoad("30000");
selenium.click(admin.events.viewoldevents);
selenium.waitForPageToLoad("30000");
}和properties
admin.username = loginForm:tbUsername
admin.loginbutton = loginForm:btnLogin
admin.events.createnewevent = adminHomeForm:_activitynew
admin.events.cancel = addEditEventForm:_IDcancel
admin.events.viewoldevents = adminHomeForm:_activityold↑ 这种方法,一般在java中的实现,我看到的,都是用class,不是用prop.properties
Page Object Design Pattern
The Page Object Design Pattern provides the following advantages
1. 分离测试代码和页面代码,比如定位和布局;
2. 把页面的服务或者操作存在一个简单的仓库里面,而不是散乱的在测试用例中;
We encourage the reader who wishes to know more to search the internet for blogs on this subject.
之前其实在一些讨论里面看到过page object设计模式,不过具体实现的话,以前曾经做过登录页面的一点点,没有全部页面应用。感觉可以优化一下现有代码。
优化前的实现:
/***
* Tests login feature
*/
public class Login { public void testLogin() {
selenium.type("inputBox", "testUser");
selenium.type("password", "my supersecret password");
selenium.click("sign-in");
selenium.waitForPageToLoad("PageWaitPeriod");
Assert.assertTrue(selenium.isElementPresent("compose button"),
"Login was unsuccessful");
}
}
There are two problems with this approach.
- There is no separation between the test method and the AUT’s locators (IDs in this example); both are intertwined in a single method. If the AUT’s UI changes its identifiers, layout, or how a login is input and processed, the test itself must change.
 - The ID-locators would be spread in multiple tests, in all tests that had to use this login page.
 
e.g
优化后的代码:将page变成对象,操作是属于该page的
/**
* Page Object encapsulates the Sign-in page.
*/
public class SignInPage { private Selenium selenium; public SignInPage(Selenium selenium) {
this.selenium = selenium;
if(!selenium.getTitle().equals("Sign in page")) {
throw new IllegalStateException("This is not sign in page, current page is: "
+selenium.getLocation());
}
} /**
* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/
public HomePage loginValidUser(String userName, String password) {
selenium.type("usernamefield", userName);
selenium.type("passwordfield", password);
selenium.click("sign-in");
selenium.waitForPageToLoad("waitPeriod"); return new HomePage(selenium);
}
}
/**
* Page Object encapsulates the Home Page
*/
public class HomePage { private Selenium selenium; public HomePage(Selenium selenium) {
if (!selenium.getTitle().equals("Home Page of logged in user")) {
throw new IllegalStateException("This is not Home Page of logged in user, current page" +
"is: " +selenium.getLocation());
}
} public HomePage manageProfile() {
// Page encapsulation to manage profile functionality
return new HomePage(selenium);
} /*More methods offering the services represented by Home Page
of Logged User. These methods in turn might return more Page Objects
for example click on Compose mail button could return ComposeMail class object*/ }
用例:
/***
* Tests login feature
*/
public class TestLogin { public void testLogin() {
SignInPage signInPage = new SignInPage(selenium);
HomePage homePage = signInPage.loginValidUser("userName", "password");
Assert.assertTrue(selenium.isElementPresent("compose button"),
"Login was unsuccessful");
}
}
上面的代码应该是seleniumRC?不是最新的selenium3.0的实现。
a few basic rules:
1.Page objects themselves should never make verifications or assertions.
页面对象不做校验和断言。校验和断言属于测试用例,必须在测试代码中实现,而不是在页面对象中。页面对象只包含页面表达, 和服务于页面的方法,和要测试的代码无关。
2.There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly.
有唯一的能被正确定位的页面元素,在页面对象中,可以用来验证这个页面。
这里介绍的方法其实不是很懂,需要多看下其他的介绍。
Data Driven Testing
数据驱动,用相同的用例,不同的数据来跑多条数据;
e.g 原来的代码
@Test( priority=2)
public void testGameCard(){
Verify.verifyCard("我的游戏","更多游戏","积分游戏");
} @Test( priority=3)
public void testLoginCard(){
Verify.verifyCard("登录轨迹","查看更多","用户登录轨迹");
}
@Test( priority=4)
public void testLotteryCard(){
Verify.verifyCard("我的彩票","积分换彩票","玩游戏赢好礼");
}
优化为(代码细节有变更,所以和上面的test方法体有差别)
@DataProvider(name="serviceLogin")
public Object[][] service2Detail(){
return new Object[][]{
{"我的游戏","积分游戏"},
{"登录轨迹","用户登录轨迹"},
{"我的彩票","玩游戏赢好礼"},
{"万里通积分","积分团购"},
{"预约挂号", "平安好医生"},
{"生活缴费","生活缴费"},
{"我的资产","一账通账户资产查询"},
{"问律师","闪电律师"},
{"手机维修","极客修"},
{"家电服务","十分到家"},
};
}
@Test(dataProvider="serviceLogin")
public void testService2(String serviceName,String clickName,String resultTitle){
if(!isLogin){
list.login("test1998","qwr1234");
isLogin=list.isLogin();
}
check(serviceName,resultTitle);
}
selenium-网站demo学习-test Design-优化自动化代码的更多相关文章
- CUDA上深度学习模型量化的自动化优化
		
CUDA上深度学习模型量化的自动化优化 深度学习已成功应用于各种任务.在诸如自动驾驶汽车推理之类的实时场景中,模型的推理速度至关重要.网络量化是加速深度学习模型的有效方法.在量化模型中,数据和模型参数 ...
 - 《IT蓝豹》吹雪花demo,学习android传感器
		
吹雪花demo,学习android传感器 吹雪花demo,学习android传感器,嘴巴对着手机底部吹一下就会出现飘着雪花效果. 算是学习android传感器效果.本例子主要是通过android.me ...
 - [Unity3D]做个小Demo学习Input.touches
		
[Unity3D]做个小Demo学习Input.touches 学不如做,下面用一个简单的Demo展示的Input.touches各项字段,有图有真相. 本项目已发布到Github,地址在(https ...
 - 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
		
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
 - 软件测试自动化…python学习到什么程度?代码好不好学!
		
软件测试自动化…python学习到什么程度?代码好不好学! 如下:
 - 针对特定网站scrapy爬虫的性能优化
		
在使用scrapy爬虫做性能优化时,一定要根据不同网站的特点来进行优化,不要使用一种固定的模式去爬取一个网站,这个是真理,以下是对58同城的爬取优化策略: 一.先来分析一下影响scrapy性能的set ...
 - uiautomatorviewer 优化定位符生成,支持生成Java,Python自动化代码
		
项目介绍 二次开发 uiautomatorviewer 优化定位符生成,支持生成Java,Python自动化代码,修复自带工具画面有动态加载时截图失败问题,优化自带工具截图速度 ,实现类似录制脚本功能 ...
 - 如何学习Linux性能优化?
		
如何学习Linux性能优化? 你是否也曾跟我一样,看了很多书.学了很多 Linux 性能工具,但在面对 Linux 性能问题时,还是束手无策?实际上,性能分析和优化始终是大多数软件工程师的一个痛点.但 ...
 - 深度学习笔记:优化方法总结(BGD,SGD,Momentum,AdaGrad,RMSProp,Adam)
		
深度学习笔记:优化方法总结(BGD,SGD,Momentum,AdaGrad,RMSProp,Adam) 深度学习笔记(一):logistic分类 深度学习笔记(二):简单神经网络,后向传播算法及实现 ...
 
随机推荐
- 老男孩python学习自修第五天【集合】
			
特点: (1)无序 (2)不重复 使用场景: (1)关系测试 (2)去重 x & y 求交集 x | y 求并集 x - y 求差集 x ^ y 求对称差集 x.intersection(y) ...
 - Javaweb小结之——JavaBean+持久层
			
数据持久层学习了JDBC.连接池以及DBUtil 思考一下,在学会使用SSM框架之前,还是先多使用DBUtil吧 数据库持久层的JDBC.连接池和DBUtil,这个链接给我了一些参考https://b ...
 - js笔记2
			
原型:prototype 和 __proto__ prototype 给他即将生成的对象继承下去的属性 prototype: 显式原型,每个function下都有prototype属性,该属性是一个对 ...
 - 删除本地git的远程分支和远程删除git服务器的分支【转】
			
转- 删除本地git的远程分支和远程删除git服务器的分支 在项目中使用git管理代码后,有些时候会创建很多不同名称的分支,以此区分各个分支代码功能. 而随着代码的合并,以前的分支就可能不再需要保存了 ...
 - linux拷贝多个目录下的文件到同一个目录
			
拷贝a目录下的a.txt和b目录下的b.txt到c目录 cp -a \ /root/a/a.txt \ /root/b/b.txt \ /root/c/
 - kubernetes   ceph-rbd挂载步骤 类型PersistentVolume
			
k8s集群每一台上面都要安装客户端: ceph-deploy install k8s的ip地址 创建一个k8s操作用户: ceph auth add client.k8s mon 'allow r ...
 - Eclipse  安装Activiti插件(BPMN打开工具)
			
在Eclipse的菜单中打开help -> install new software: 单击add: Name: Activiti BPMN 2.0 designer Location: htt ...
 - P1008 三连击
			
题目背景 本题为提交答案题,您可以写程序或手算在本机上算出答案后,直接提交答案文本,也可提交答案生成程序. 题目描述 将1,2,⋯,9共9个数分成3组,分别组成3个三位数,且使这33个三位数构成1:2 ...
 - Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
			
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
 - Install Nagios (Agent) nrpe client and plugins in Ubuntu/Debian
			
安装apt-get install nagios-nrpe-server nagios-plugins 修改nrpe.cfgvi /etc/nagios/nrpe.cfg修改Allow Host,添加 ...