记我的第二次自动化尝试——selenium+pageobject+pagefactory实现自动化下单、退款、撤销回归测试
需求:
系统需要做下单、退款、撤销的回归测试,有下单页面,所以就想到用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实现自动化下单、退款、撤销回归测试的更多相关文章
- 自动化工具selenium
selenium web 自动化工具 selenium 不仅仅可以做web自动化,还可以考虑用于爬虫 java.python..net都可使用,具体使用方法google 构建Python+Seleni ...
- 小程序UI自动化(一):appium小程序自动化尝试
appium 进行 小程序自动化尝试: 由于工作中进行app自动化用的是appium,故首先尝试用appium进行小程序自动化,以美团小程序为例(python脚本实现) 一.配置基础信息 启动微信ap ...
- 自动化测试之Selenium篇(一):环境搭建
当前无论找工作或者是实际项目应用,自动化测试扮演着非常重要的角色,今天我们来学习下Selenium的环境搭建 Selenium简述 Selenium是一个强大的开源Web功能测试工具系列 可进行读入测 ...
- 技术分享 | Web自动化之Selenium安装
Web 应用程序的验收测试常常涉及一些手工任务,例如打开一个浏览器,并执行一个测试用例中所描述的操作.但是手工执行的任务容易出现人为的错误,也比较费时间.因此,将这些任务自动化,就可以消除人为因素.S ...
- Selenium+java - PageFactory设计模式
前言 上一小节我们已经学习了Page Object设计模式,优势很明显,能更好的体现java的面向对象思想和封装特性.但同时也存在一些不足之处,那就是随着这种模式使用,随着元素定位获取,元素定位与页面 ...
- 浅析selenium的PageFactory模式
前面的文章介绍了selenium的PO模式,见文章:http://www.cnblogs.com/qiaoyeye/p/5220827.html.下面介绍一下PageFactory模式. 1.首先介绍 ...
- Selenium的PageFactory在大型项目中的应用
出路出路,走出去了,总是会有路的:困难苦难,困在家里就是难. 因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手 ...
- Selenium的PageFactory & PageObject 在大型项目中的应用
因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手动的操作步骤去转换,近日看到一个文章,又去wiki上查了查,觉 ...
- 浅析selenium的PageFactory模式 PageFactory初始化pageobject
1.首先介绍FindBy类: For example, these two annotations point to the same element: @FindBy(id = "foob ...
随机推荐
- 07 ProgressDialog
<span style="font-size:18px;">package com.fmy.example1; import android.app.Activity; ...
- JSP自定义标签必知必会
自定义标签技术自sun公司发布以来,便一向很受欢迎!下面我就来谈一谈如何实现自定义标签,以及如何使用自定义标签. 如何实现自定义标签 首先我们应该知道原理,不管是标签还是JSP,本身实际上都会被JSP ...
- Android进阶(二十七)Android原生扰人烦的布局
Android原生扰人烦的布局 在开发Android应用时,UI布局是一件令人烦恼的事情.下面主要讲解一下Android中的界面布局. 一.线性布局(LinearLayout) 线性布局分为: (1) ...
- Rational Rose正逆向工程(类图转Java代码,Java代码转类图)
一,正向工程 1.设置默认语言为Java,Tools->Options->Notation->default:选择Java. 2.设置环境变量Class ...
- 使用Mediaplay类写一个播放器
我们知道android本身播放视频的的能力是有限的..先来一个Demo 另附我的一个还未成熟的播放器,下载地址:http://www.eoemarket.com/soft/370334.html,正在 ...
- 《java入门第一季》之泛型引入
泛型的引入: 首先看一段代码体会自动报错. // 看下面这个代码 自动报错 String[] strArray = new String[3]; strArray[0] = "hello&q ...
- 【Android 应用开发】 FastJson 使用详解
博客地址 :http://blog.csdn.net/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https:// ...
- ArrayList与Vector的区别
ArrayList与Vector的区别 相同 这两个类都实现了List接口. 他们都是有序集合. 不同 ArrayList实现不是同步的,Vector实现是同步的. ArrayList与Vector都 ...
- Socket层实现系列 — I/O事件及其处理函数
主要内容:Socket I/O事件的定义.I/O处理函数的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd I/O事件定义 sock中定义了几个I/ ...
- Socket编程实践(8) --Select-I/O复用
五种I/O模型介绍 (1)阻塞I/O[默认] 当上层应用App调用recv系统调用时,如果对等方没有发送数据(Linux内核缓冲区中没有数据),上层应用Application1将阻塞;当对等方发送了数 ...