Automation Testing - Best Practice(书写规范)
Coding Standards
Coding Standards are suggestions that will help us to write automation Scripts code using any language. Naming conventions for creating automation Script, objects, variables and procedures. Commenting standard in the scripting, Text formatting and align guidelines for the script.
Naming Conventions:
Naming Conventions should be followed at all levels of the project for e.g. Variables, Constants, Objects
Standards of Variable Naming
For easy readability purpose use the prefixes like below examples, along with descriptive names for variables in Automation Script code.
Example:
- String – sName
- Object – oObjectname
- Integer,Byte,Long – nNumber
- Array – arrListitems
- Boolean – bYes
- Single,Double – fPrice
- DateTime – dToday
- Error – errResponse
- Currency – cRupees
Standards of Constant Naming
Constant names should be in uppercase with underscores (_) between the words.
Example: USER_LIST_MAX, NEW_LINE
Object Naming Standards
For the objects created in the Automation script, use prefix “o” (small letter o).
Example:
- ADODB.Connection – oConn
- ADODB.Recordset – oRset
- Scripting.xmlobject – oXmlobj
- Scipting.FileSystemObject – oFsobj
- Scipting.TextStream – oTxt
- Scripting.Dictionary – oDic
- Excel.Application – oXls
Reusability:
Abstraction to be followed for Code Reuse at all possible levels – function library, config file, POM
POM (Page Object Model) based approach
- POM approach is to create a separate class file which would find web elements, fill them or verify them. This class can be reused in all the scripts using that element. In future if there is change in the web element, we need to make change in just 1 class file and not 10 different scripts.
- This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements
- A lot more expressive as we look at the code. With well named methods, we create a higher level of abstraction that is easier to read and understand
- If the UI changes (an id of a field), we go to our one method, update the id and we are good to go
For frequent changes we use properties/Excel sheet/XML file
We can use excel sheet for storing our test data. It is easy to maintain the data in excel and also easy to modify the test data as per the business requirements.
Use custom exception which can be easily understood
Custom exception helps to clearly understand the error. Please find below some key points for using custom exceptions:
- Add situation specific data to an exception. Ideally this data would help another developer to track down the source of the error.
- Provides a type safe mechanism for a developer to detect an error condition.
- No existing exception adequately described my problem.
Maintainability and Extensibility:
- Use of Interface based programming to achieve the maintenance of existing scripts and extension of new ones
Portability:
Code often has to run on all the platforms.
No hard coding to be used unless absolutely necessary
Locator Identification Techniques
If you’ve come here looking for the perfect, unbreakable locator, then I’m afraid to tell you that there is no perfect locator. That HTML changes and locators become incompatible are realities of writing automated UI tests. There is no other option than getting used to update them as the development teams experiment with design, streamline HTML and fix bugs as long as the application is evolving.
A good element locator:
- is as small as possible to indicate the GUI element
- will continue working when GUI elements around the GUI element change
- will continue working when properties of the GUI element change
A failing locator is a good thing so don’t be afraid of it. The trusty NoSuchElementException, rather than an assertion failure, is often your first sign that there is a regression in your software.
ID Locator
In a perfect world, each web element has an html 'id'. Finding an element by html id is the most precise way to define an element, since the id is usually the property that’s less likely to change. By W3C standards, it should be unique in the page meaning you will never have a problem with finding more than one element matching the locator.
The ID is also independent of the element type and location in the tree and thus if the developer moves the element or changes its type WebDriver can still locate it.
IDs are often also used in the web page’s JavaScript so a developer will avoid changing an element’s ID to avoid having to change his JavaScript. That’s great for us testers!
If you have flexible developers or even an eye for the app source code you can always try and get extra IDs added into the code. A good use case will definitely convince even the developers to add the ids to the elements. However, sometimes adding IDs everywhere is impractical or not viable so we need to use CSS or Xpath locators.
CSS and Xpath locators
CSS and Xpath locators are conceptually very similar.
These types of locators with combinations of tag name, descendant elements, CSS class or element attribute makes the matching pattern strict or loose, strict meaning that small HTML changes will break the pattern and lose meaning that it might match more than one HTML element.
When writing a CSS or Xpath locator it’s all about finding the balance between strict and loose; durable enough to work with HTML changes and strict enough to fail when the app fails.
Find an anchoring element
A good way to start a CSS or Xpath locator is to start with an element that you know is not likely to change much and use it as an ‘anchor’ in your locator. It may have an ID or stable location but not be the element you need to locate but is a reliable position to search from. Your anchoring element can be above or below the current element in the HTML tree, but most often it’s above.
<div id=”main-section”>
<p>Introduction</p>
<ul>
<li> Option 1</li>
</ul>
</div>
In this example the <li> element that we want to locate does not have an ID or a CSS class making it difficult to locate. There is also the chance there is more than one list in the HTML. The div with the id “main-section” makes a good anchoring element from which to find the <li> element. It narrows down the HTML the locator is searching in.
When to use ‘index’ locators like nth-child() and [x]
nth-child(), first-child, [1] and such index-type locators should only be used if you are using it against a list itself. In this case the test should explicitly know it wants to pick an item at that index from the list, for example validating the first item of search results. Using an index-type locator to locate an element that is not index-placed is likely to cause you problems when the order of the elements is changed and thus should be avoided!
<menu>
<button>Option 1</button>
<button>Option 2</button>
<button>Option 3</button>
</menu>
//menu/button[1] is a suitable locator only when you know you want to interact with the first menu item regardless of how the list of buttons is sorted. The order of the buttons may change and cause your test to fail. Would this be a legitimate failure or one that requires you to re-write the locator?
Depending upon the objective of your test a non-index based locator like //menu/*[text()=’Option 1’] might be more suitable. <menu> is the ideal anchoring element.
CSS class names often tell their purpose
Assuming that the Front end designers will often give CSS classes, the names that represent their purpose, we can take advantage of the situation. We can choose locators that are dependent upon the functionality rather than the styling because styling often changes.
<footer class="form-footer buttons">
<div class="column-1">
<a class="alt button cancel" href="#">Cancel</a>
</div>
<div class="column-2">
<a class="alt button ok" href="#">Accept</a>
</div>
</footer>
In this example ignore the class “column-1” and “column-2”. They refer to the layout and thus might be susceptible to changes if the development team decide to adjust the design. It will be more reliable to target the button directly. Although “button.ok” would be quite a ‘loose’ locator there could be more than one on the page. You can use the footer as your anchoring element, making “footer .ok” a good locator in this example.
Spotting future fragility
By observing the HTML you can spot potential future fragility. In the previous example, 3 more things are intentionally left out of the locator: the <a>’s tag name, the <a>’s content text and any direct descendants ( > ) between footer and a. In the HTML it looks like the dev team have already changed the text label and the tag from “ok” and “button” respectively. The class, text content and tag names are all mismatched!
If the dev team are indecisive or experimenting with UX and performance improvements, these might still change again so we will err on a slightly “looser” locator that will tolerate some changes in the HTML.
Direct descendents
CSS example: div > div > ul > li > span
Xpath example: //div/div/ul/li/span
A direct descendent refers to the parent to child relationship of HTML elements. A good example is the first <li> element inside a <ul>.
A long chain of direct descendants like in the locator example above might help you find an element where there are no classes or IDs but it is sure to be unreliable in the long term. A large block of content without IDs or classes is likely to be very dynamic too and probably move around and change HTML structure often. It only takes one element in the chain to change for your locator to come tumbling down.
If you absolutely must use direct descendants in your locators then try to only use a maximum of one in each locator.
Adjust it for purpose
<section id=”snippet”>
<div>Blurb</div>
</section>
Only use as much of a locator you need. Less is more! If you are only capturing text then using a locator like “#snippet div” is unnecessary. WebDriver will return the same text content for the locator ‘#snippet and ‘#snippet > div’ but the latter locator would break if the div element were changed to a <p> or <span>.
Locating on element attributes
...
Tag name, link text, name locating strategies
...
CSS and Xpath Axes
An axis defines a node-set relative to the current node.
|
Axis Name
|
Result |
|---|---|
|
Axis Name
|
Result |
| ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node |
| ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself |
| attribute |
Selects all attributes of the current node |
| child | Selects all children of the current node |
| descendant | Selects all descendants (children, grandchildren, etc.) of the current node |
| descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself |
| following | Selects everything in the document after the closing tag of the current node |
| following-sibling | Selects all siblings after the current node |
| namespace | Selects all namespace nodes of the current node |
| parent | Selects the parent of the current node |
| preceding | Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes |
| preceding-sibling | Selects all siblings before the current node |
| self | Selects the current node |
Automation Testing - Best Practice(书写规范)的更多相关文章
- CSS书写规范、命名规范、网易CSS框架NEC
网易CSS框架NEC:http://nec.netease.com/ NEC框架的CSS规范: CSS规范 - 分类方法 CSS规范 - 命名规则 CSS规范 - 代码格式 CSS规范 - 优化方案 ...
- html和css书写规范
HTML 规范 分离的标记.样式和脚本 结构.表现.行为分离 在可能情况下验证你的标记 使用编辑器验证你的标记是否正确,一般编辑器都自带有这个功能. 技术不支持的时候使用备胎,如canvas 编码格式 ...
- 数据库DDL语句书写规范
数据库DDL语句书写规范 1.SQL语句编写说明编写SQL语句应遵循统一的规范,包括大小写.空格.换行.缩进等等,只有完全一样的SQL才能在数据库中共享,从而减少硬解析. 字段类型.长度:根据数据情况 ...
- CSS书写规范
一.CSS书写顺序 1.位置属性(position,top,right,z-index,display,float等) 2.大小(width,height,padding,margin) 3.文字系列 ...
- 推荐大家使用的CSS书写规范、顺序
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...
- 分享给大家的CSS书写规范、顺序
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...
- css命名书写规范小结。
单行形式书写风格的排版约束 1. 每一条规则的大括号 { 前后加空格 2. 多个selector共用一个样式集,则多个selector必须写成多行形式 3. 每一条规则结束的大括号 } 前 ...
- CSS书写规范及顺序
CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin)3. ...
- [Java拾遗一] XML的书写规范与解析.
前言今天天气大好, 起了个大早开始总结一些常用的基础知识. XML一直来说都很陌生, 使用大多是用于配置文件, 之前并没有细究过其中的约束规范, 今天刚好没事来学习并总结下. 1,XML基础介绍 XM ...
随机推荐
- Java 面试前的基础准备 - 01
使用这个在线网页编辑真的是不习惯,还是 windows live writer 好. 下面列一个清单用于最近的面试:( 清单是网上down的 ) static,final,transient 等关键字 ...
- node web 应用热更新
在每次更改完 node.js 项目后,我们都需要先将 node.js停止(快捷键: Ctrl+C),然后再通过命令再次运行,这样特别麻烦.这里我推荐使用 supervisor工具, npm 安装命令为 ...
- java finally 与return
finally之外的语句块有return,finally语句块没有return:该语句块的返回值被固定下来,等fianlly执行完后返回给调用者 finally语句块与其他语句块同时有return:返 ...
- Argus UVALive - 3135(优先队列 水题一道)
有一系列的事件,它每Period秒钟就会产生编号为qNum的事件,你的任务是模拟出前k个事件,如果多个事件同时发生,先处理qNum小的事件 今天再看看数据结构.. #include <iostr ...
- Hyperledger Fabric 实战(十二): Fabric 源码本地调试
借助开发网络调试 fabric 源码本地调试 准备工作 IDE Goland Go 1.9.7 fabric-samples 模块 chaincode-docker-devmode fabric 源码 ...
- NOIP2017 考前汇总
时隔一年,相比去年一无所知的自己,学到了不少东西,虽然还是很弱,但也颇有收获[学会了打板QAQ] 现在是2017.11.9 21:10,NOIP2017的前两天晚上,明天就要出发,做最后的总结 N ...
- 解题:APIO 2008 免费道路
题面 我们发现我们可以很容易知道最终完成的生成树中有多少鹅卵石路,但是我们不好得到这棵生成树的结构,所以我们尽量“谨慎”地完成生成树·,最好是一点点加到我们要达到的标准而不是通过删掉一些东西来完成 我 ...
- Caffe框架详细梳理
protobuf是google公司开发的,并在Google内部久经考验的一个东西,在08年google把它贡献给了开源社区,随后便有越来越多的人使用它.protobuf是一个结构化信息传递的工具,主要 ...
- SpringMVC 重定向
在返回视图名字的字符串前面加forword:或redirect:前缀是就会对他们做特殊处理,它们分别是转发和重定向 我们测试一个重定向操作把 Java代码 @RequestMapping(" ...
- 线性判别分析(Linear Discriminant Analysis)
1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...