需求:

系统需要做下单、退款、撤销的回归测试,有下单页面,所以就想到用selenium做WEB UI 自动化

项目目录结构:

common包上放通用的工具类方法和浏览器操作方法

pageobject包放封装好的页面对象,里面包含页面所有可操作的元素和方法

testcase包放测试用例脚本

data.properties放需要传入的测试数据

result.properties放测试执行后的结果

pom.xml为maven项目的配置文件,解决项目包的依赖问题

testng.xml为testNG框架的配置文件,控制用例的执行

下面开始介绍项目实施过程

1.第一步,新建maven项目,pom.xml文件内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>elcas</groupId>
<artifactId>elcas_selenium_pay</artifactId>
<version>1.0-SNAPSHOT</version> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<!--<scope>compile</scope>-->
</dependency>
<!--<dependency>-->
<!--<groupId>org.seleniumhq.selenium</groupId>-->
<!--<artifactId>selenium-server</artifactId>-->
<!--<version>2.53.1</version>-->
<!--</dependency>-->
</dependencies> </project>

2.第二步,编写操作浏览器的方法,和可能用到的工具类方法

 package common;

 import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver; import java.io.File;
import java.io.IOException; public class OperateBrower {
static WebDriver driver;
public static WebDriver OpenFireFox() throws IOException {
File firefoxFile=new File("D:\\Program Files (x86)\\Mozilla Firefox\\24.0\\firefox.exe");
FirefoxBinary binary=new FirefoxBinary(firefoxFile);
driver =new FirefoxDriver(binary,null);
return driver;
}
public static void OpenURL(String url) throws InterruptedException {
driver.get(url);
driver.manage().window().maximize();
// Thread.sleep(2000);
}
public static void CloseBrower(){
driver.close();
} public static void main(String[] args) throws Exception {
// File directory = new File("");// 参数为空
// String courseFile = directory.getCanonicalPath();
// System.out.println(courseFile);
OperateBrower.OpenFireFox();
OperateBrower.OpenURL("https://www.baidu.com");
OperateBrower.CloseBrower(); }
}
 package common;

 import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties; public class UtilMethod {
public static String getData(String key){
Properties pro=new Properties();
try {
InputStream inputfile= new FileInputStream("data.properties");
pro.load(new InputStreamReader(inputfile,"utf-8")); } catch (IOException e) {
e.printStackTrace();
}
return pro.getProperty(key);
}
public static void setData(String key,String value,String comments){
Properties pro=new Properties();
try {
FileOutputStream outputfile= new FileOutputStream("result.properties",true);
pro.setProperty(key,value);
pro.store(new OutputStreamWriter(outputfile,"utf-8"),comments); } catch (IOException e) {
e.printStackTrace();
}
}
public static String getCurrentDate(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
// System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
return df.format(new Date());
}
public static void main(String[] args){
// System.out.println(getData("URL"));
setData("中文","22","test");
setData("121","孩子","test");
// getCurrentDate();
}
}

读取和写入properties文件时,一开始中文乱码,需要加上相关的把编码类型变成utf-8的语句

3.第三步,创建pageobject对象,通过pagefactory中的@FindBy注解和PageFactory.initElements(driver, this);初始化页面控件元素对象,举个例子

package pageobject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class Pay_page {
WebDriver driver;
WebDriverWait wait;
@FindBy(xpath = "/html/body/div/ul/li[2]/a")
WebElement tab;
@FindBy(id="goods")
WebElement goodsName;
@FindBy(id="sysmerchantno")
WebElement merchantNo;
@FindBy(id="amount")
WebElement amount;
@FindBy(id="cardNO")
WebElement cardNo;
@FindBy(id="pwd")
WebElement password;
@FindBy(id="pay")
WebElement payButton;
@FindBy(xpath = "/html/body/div/div[2]/div[7]/div[1]")
WebElement result;
@FindBy(xpath = "/html/body/div/div[2]/div[7]/div[2]")
WebElement orderNo; public Pay_page(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void clickTab(){
tab.click();
}
public void inputGoods(String goodsname){
goodsName.clear();
goodsName.sendKeys(goodsname);
}
public void inputMerchantNo(String merchantno){
merchantNo.clear();
merchantNo.sendKeys(merchantno);
}
public void inputAmount(String amountStr){
amount.clear();
amount.sendKeys(amountStr);
}
public void inputCardNo(String cardno){
cardNo.clear();
cardNo.sendKeys(cardno);
}
public void inputPassword(String pwd){
password.clear();
password.sendKeys(pwd);
}
public void clickPay(){
payButton.click();
}
public String getResult(){
wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(result));
return result.getText();
}
public String getOrderNo(){
wait.until(ExpectedConditions.visibilityOf(orderNo));
String str=orderNo.getText();
String [] a=str.split(":") ;
return a[1];
}
public void pay(String goods,String merchant,String amount,String cardno,String pwd) throws Exception{
clickTab();
inputGoods(goods);
inputMerchantNo(merchant);
inputAmount(amount);
inputCardNo(cardno);
inputPassword(pwd);
clickPay();
Thread.sleep(2000);
System.out.println("支付结果是:"+getResult()+"\r\n订单号是:"+getOrderNo()); }
}

第四步,编写测试用例脚本

 package testcase;

 import common.OperateBrower;
import common.UtilMethod;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pageobject.Pay_page; public class Pay_testcase {
WebDriver driver;
Pay_page payPage;
@BeforeClass
public void setUp() throws Exception {
driver=OperateBrower.OpenFireFox();
OperateBrower.OpenURL(UtilMethod.getData("URL"));
}
@AfterClass
public void tearDown(){
OperateBrower.CloseBrower();
}
@Test
public void testPay()throws Exception{
payPage=new Pay_page(driver);
// payPage.clickTab();
// Thread.sleep(2000);
System.out.println("支付用例执行开始");
payPage.pay(UtilMethod.getData("GOODS"),UtilMethod.getData("MERCHANT"),UtilMethod.getData("AMOUNT"),UtilMethod.getData("CARD"),UtilMethod.getData("PASSWORD"));
UtilMethod.setData("支付结果",payPage.getResult()+" "+payPage.getOrderNo(),"result");
// UtilMethod.setData(+timestamp,payPage.getOrderNo());
// Thread.sleep(2000);
Assert.assertEquals(payPage.getResult(),"支付成功");
System.out.println("支付用例执行结束");
System.out.println();
}
}

第五步,准备好测试数据,建好存储结果的文件,使用testng.xml运行测试

testng.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="regression">
<test name="all_regression">
<packages><package name="testcase"></package></packages>
</test>
<test name="part_regression">
<classes>
<!--<class name="testcase.Pay_testcase"></class>-->
<!--<class name="testcase.Refund_testcase">-->
<!--<methods>-->
<!--<include name="testRefund"></include>-->
<!--</methods>-->
<!--</class>-->
<!--<class name="testcase.Quash_testcase"></class>-->
</classes>
</test>
</suite>

data.properties文件

 URL=https\://手动打码/
AMOUNT=1
GOODS=test
MERCHANT=123
CARD=6255555555555555
PASSWORD=123456

result.properties文件

#Thu May 03 15:45:08 CST 2018
支付结果=支付成功 02000435490503154454

希望同行们更够给出改进建议,欢迎交流讨论

记我的第二次自动化尝试——selenium+pageobject+pagefactory实现自动化下单、退款、撤销回归测试的更多相关文章

  1. 自动化工具selenium

    selenium web 自动化工具 selenium 不仅仅可以做web自动化,还可以考虑用于爬虫 java.python..net都可使用,具体使用方法google 构建Python+Seleni ...

  2. 小程序UI自动化(一):appium小程序自动化尝试

    appium 进行 小程序自动化尝试: 由于工作中进行app自动化用的是appium,故首先尝试用appium进行小程序自动化,以美团小程序为例(python脚本实现) 一.配置基础信息 启动微信ap ...

  3. 自动化测试之Selenium篇(一):环境搭建

    当前无论找工作或者是实际项目应用,自动化测试扮演着非常重要的角色,今天我们来学习下Selenium的环境搭建 Selenium简述 Selenium是一个强大的开源Web功能测试工具系列 可进行读入测 ...

  4. 技术分享 | Web自动化之Selenium安装

    Web 应用程序的验收测试常常涉及一些手工任务,例如打开一个浏览器,并执行一个测试用例中所描述的操作.但是手工执行的任务容易出现人为的错误,也比较费时间.因此,将这些任务自动化,就可以消除人为因素.S ...

  5. Selenium+java - PageFactory设计模式

    前言 上一小节我们已经学习了Page Object设计模式,优势很明显,能更好的体现java的面向对象思想和封装特性.但同时也存在一些不足之处,那就是随着这种模式使用,随着元素定位获取,元素定位与页面 ...

  6. 浅析selenium的PageFactory模式

    前面的文章介绍了selenium的PO模式,见文章:http://www.cnblogs.com/qiaoyeye/p/5220827.html.下面介绍一下PageFactory模式. 1.首先介绍 ...

  7. Selenium的PageFactory在大型项目中的应用

    出路出路,走出去了,总是会有路的:困难苦难,困在家里就是难. 因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手 ...

  8. Selenium的PageFactory & PageObject 在大型项目中的应用

    因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手动的操作步骤去转换,近日看到一个文章,又去wiki上查了查,觉 ...

  9. 浅析selenium的PageFactory模式 PageFactory初始化pageobject

    1.首先介绍FindBy类: For example, these two annotations point to the same element: @FindBy(id = "foob ...

随机推荐

  1. Linux2.6--虚拟文件系统

          虚拟文件系统(有时也称作虚拟文件交换,更常见的是简称做VFS)作为内核子系统,为用户空间程序提供了文件和文件系统相关的接口.系统中的所有文件系统不但依赖VFS共存,而且也依赖VFS系统协同 ...

  2. AndroidStudio如何快速制作.so

    之前写过一篇Eclipse制作.so的文章,http://blog.csdn.net/baiyuliang2013/article/details/44306921使用的是GNUstep模拟Linux ...

  3. 定义范围中的备选方案生成、横向思维、创建WBS、工作包定义、WBS、确认范围过程和实施质量过程的关系、联合应用设计和质量功能展开QFD

  4. A*寻路算法入门(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  5. cocos2d-js(一)引擎的工作原理和文件的调用顺序

    Cocos2d-js可以实现在网页上运行高性能的2D游戏,实现原理是通过HTML5的canvas标签,该引擎采用Javascript编写,并且有自己的一些语法,因为没有成熟的IDE,一般建立工程是通过 ...

  6. pig代码格式上小注意

    1,%default file test.txt 中不要用引号,'' 和""都不行.'file'不会被识别 2,pig判断相等,用==,不是一个=.. 3,pig中只用单引号,不用 ...

  7. (NO.00003)iOS游戏简单的机器人投射游戏成形记(二十一)

    回到Xcode中,在MainScene.h接口中添加碰撞协议: @interface MainScene : CCNode <CCPhysicsCollisionDelegate> //. ...

  8. 我眼中的Linux设备树(二 节点)

    二 节点(node)的表示首先说节点的表示方法,除了根节点只用一个斜杠"/"表示外,其他节点的表示形式如"node-name@unit-address".@前边 ...

  9. memcached /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

    启动memcached的时候发现找不到libevent的库,这是memcache的默认查找路径不包含libevent的安装路径,所以要告诉memcached去哪里查找libevent. 操作命令如下: ...

  10. JQuery实战总结二 横向纵向菜单下拉效果图

    记得以前在浏览了大多数网站的上面发现很多下拉的导航栏,觉得特别好玩,毕竟咱们是学习编程的嘛,对这下拉的效果还是挺感兴趣的,这种淡入淡出,随着鼠标移动的位置不同.有无等而出现不同的效果,给用户以神美感. ...