|
| 1 | +# 介绍 |
| 2 | +本项目PhoenixAutotest是一个基于Selenium的WebUI自动测试框架,通过该框架可以简化测试人员的学习难度,只要编写少量的Java代码即可,大多数的工作都是编写页面元素的描述文件以及对应的数据源。 |
| 3 | +框架PhoenixAutotest已经集成了三大浏览器(Chrome、IE、Firefox)在Windows、Linux以及Mac操作系统下的各种WebDriver驱动版本。用户无需花费很多时间来进行开发环境的搭建。 |
| 4 | +每一个做过WebUI自动化测试工作的朋友一定也体会到了,元素定位是一个比较耗时、困难的过程,针对这个痛点,框架也给出了三种元素定位的策略。 |
| 5 | +而数据源、参数化更是本框架的一大亮点。您除了可以直接使用框架提供的丰富的数据源和参数化技术外,也可以很轻松地给出自己的实现。 |
| 6 | +测试套件,也是无需用户编码,通过配置文件描述即可。这点,类似于TestNG的配置文件。 |
| 7 | + |
| 8 | +# 准备 |
| 9 | +如果要使用PhoenixAutotest框架来做WebUI自动化测试的话,需要先了解一些必要的技能或者知识。 |
| 10 | +* XML |
| 11 | +框架的元素定位信息、数据源等文件默认是XML格式的,所以,您应该了解XML的语法。 |
| 12 | +* Java基础 |
| 13 | +虽然,大多数的工作都交给XML配置了,但是还是应该了解Java的基础,例如:语法、集合、异常信息、调试技巧等。 |
| 14 | +* 开发工具 |
| 15 | +Eclipse或者其他IDE都是可以的,但您至少应该了解一种。 |
| 16 | +* 元素定位方法 |
| 17 | +WebUI测试,必然是离不开元素定位的。能够熟练地使用浏览器调试工具来获取元素定位信息是做自动化测试的基础。 |
| 18 | + |
| 19 | +如果您对自动化测试感兴趣,但是现在还不了解Selenium的话,这里给您提供一篇[学习建议](http://surenpi.com/2016/04/28/selenium_learning_advise/)。 |
| 20 | +# 入门 |
| 21 | +步骤如下: |
| 22 | +1. 创建工程 |
| 23 | +2. 配置Spring配置文件 |
| 24 | +3. 编写元素定位XML配置 |
| 25 | +4. 编写Page类 |
| 26 | +5. 编写测试类 |
| 27 | + |
| 28 | +## 创建工程 |
| 29 | +可以根据您的习惯来选择Java工程或者Maven工程。如果是Maven工程的话,请添加如下依赖: |
| 30 | +````xml |
| 31 | +<dependency> |
| 32 | + <groupId>com.surenpi.autotest</groupId> |
| 33 | + <artifactId>autotest.web.framework</artifactId> |
| 34 | + <version>1.0.2-20170422</version> |
| 35 | +</dependency> |
| 36 | +```` |
| 37 | +如果只是要用简单的Java工程的话,可以从下面的QQ群里下载编译好的autotest.web.framework.dist-*.jar文件,加入到classpath中即可。 |
| 38 | + |
| 39 | +## Spring配置 |
| 40 | +````xml |
| 41 | +<?xml version="1.0" encoding="UTF-8"?> |
| 42 | +<p:beans xmlns:p="http://www.springframework.org/schema/beans" |
| 43 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 44 | + xmlns:context="http://www.springframework.org/schema/context" |
| 45 | + xsi:schemaLocation="http://www.springframework.org/schema/beans |
| 46 | + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
| 47 | + http://www.springframework.org/schema/context |
| 48 | + http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
| 49 | + <!-- 这里配置Page类所在的包,不然框架讲无法找到对应的类 --> |
| 50 | + <context:component-scan base-package="org.suren.autotest.test.page" /> |
| 51 | +</p:beans> |
| 52 | +```` |
| 53 | +以上的配置文件applicationContext.xml,要放置在src根目录中。注意:该文件名固定,不允许自定义。 |
| 54 | + |
| 55 | +## 元素定位 |
| 56 | +```xml |
| 57 | +<?xml version="1.0" encoding="UTF-8"?> |
| 58 | +<suren:autotest xmlns:suren="http://surenpi.com" |
| 59 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 60 | + xsi:schemaLocation="http://surenpi.com |
| 61 | + http://surenpi.com/schema/autotest/autotest.web.framework.xsd "> |
| 62 | + <suren:engine /> |
| 63 | + <suren:pages> |
| 64 | + <suren:page class="org.suren.autotest.test.page.BaiduHomePage" |
| 65 | + url="https://www.baidu.com/"> |
| 66 | + <suren:field name="keyword" byId="kw" /> |
| 67 | + <suren:field name="searchBut" byId="su" /> |
| 68 | + </suren:page> |
| 69 | + </suren:pages> |
| 70 | +</suren:autotest> |
| 71 | +``` |
| 72 | +以上的配置文件baidu.xml,要放置在src根目录中。 |
| 73 | +## Page类 |
| 74 | +```java |
| 75 | +package org.suren.autotest.test.page; |
| 76 | + |
| 77 | +import org.springframework.beans.factory.annotation.Autowired; |
| 78 | +import org.springframework.stereotype.Component; |
| 79 | +import org.suren.autotest.web.framework.core.ui.Button; |
| 80 | +import org.suren.autotest.web.framework.core.ui.Text; |
| 81 | +import org.suren.autotest.web.framework.page.Page; |
| 82 | + |
| 83 | +/** |
| 84 | + * 逻辑页面,包含页面中需要定位的元素信息</br> |
| 85 | + * Text是文本框、文本域</br> |
| 86 | + * Button是按钮,可以是a标签或者input标签类型为button的以及其他所有可以电机的元素</br> |
| 87 | + * Selector是下拉列表</br> |
| 88 | + * 类上必须加Component,属性上必须加Autowired注解</br> |
| 89 | + * 所有的属性必须添加对应的getter和setter方法</br> |
| 90 | + * 另外,框架提供了工具类用于根据baidu.xml来生成当前Page类的方法,具体请查看code工程 |
| 91 | + * @author suren |
| 92 | + * @date 2016年12月13日 下午7:57:44 |
| 93 | + */ |
| 94 | +@Component |
| 95 | +public class BaiduHomePage extends Page { |
| 96 | + @Autowired |
| 97 | + private Text keyword; |
| 98 | + @Autowired |
| 99 | + private Button searchBut; |
| 100 | + public Text getKeyword() { |
| 101 | + return keyword; |
| 102 | + } |
| 103 | + public void setKeyword(Text keyword) { |
| 104 | + this.keyword = keyword; |
| 105 | + } |
| 106 | + public Button getSearchBut() { |
| 107 | + return searchBut; |
| 108 | + } |
| 109 | + public void setSearchBut(Button searchBut) { |
| 110 | + this.searchBut = searchBut; |
| 111 | + } |
| 112 | +} |
| 113 | +```` |
| 114 | +## 测试类 |
| 115 | +```java |
| 116 | +package org.suren.autotest.test; |
| 117 | + |
| 118 | +import java.io.IOException; |
| 119 | + |
| 120 | +import org.dom4j.DocumentException; |
| 121 | +import org.suren.autotest.test.page.BaiduHomePage; |
| 122 | +import org.suren.autotest.web.framework.settings.SettingUtil; |
| 123 | +import org.xml.sax.SAXException; |
| 124 | + |
| 125 | +/** |
| 126 | + * AutoTest框架的一个简单示例</br> |
| 127 | + * baidu.xml主要包含元素定位信息的描述</br> |
| 128 | + * applicationContext.xml配置了Page类所在的包(package)</br> |
| 129 | + * Page类BaiduHomePage是一个逻辑的页面对象,包括页面中需要定位的元素 |
| 130 | + * @author suren |
| 131 | + * @date 2016年12月13日 下午7:52:06 |
| 132 | + */ |
| 133 | +public class Test { |
| 134 | + |
| 135 | + /** |
| 136 | + * 入口函数 |
| 137 | + * @param args |
| 138 | + * @throws IOException |
| 139 | + * @throws DocumentException |
| 140 | + * @throws SAXException |
| 141 | + * @throws InterruptedException |
| 142 | + */ |
| 143 | + public static void main(String[] args) throws IOException, DocumentException, |
| 144 | + SAXException, InterruptedException { |
| 145 | + SettingUtil util = new SettingUtil(); |
| 146 | + util.readFromClassPath("baidu.xml"); //加载元素定位配置文件 |
| 147 | + |
| 148 | + //获取Page类,然后获取对应的元素,再进行操作 |
| 149 | + BaiduHomePage baiduHomePage = util.getPage(BaiduHomePage.class); |
| 150 | + baiduHomePage.open(); //打开baidu.xml配置文件中配置的页面地址 |
| 151 | + baiduHomePage.getKeyword().setValue("selenium"); //告诉框架文本框要填充的值 |
| 152 | + baiduHomePage.getKeyword().fillValue(); //向文本框中填充值 |
| 153 | + baiduHomePage.getSearchBut().click(); //点击搜索按钮 |
| 154 | + |
| 155 | + Thread.sleep(3000); |
| 156 | + |
| 157 | + util.close(); //关闭框架 |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | +以上示例的[源代码](https://github.com/LinuxSuRen/phoenix.autotest.demo/tree/master/simple) |
| 162 | +# 参考 |
| 163 | +[更多示例](https://github.com/LinuxSuRen/phoenix.autotest.demo) |
0 commit comments