需求:

系统需要做下单、退款、撤销的回归测试,有下单页面,所以就想到用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. Cocos2D中Action的进阶使用技巧(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上回说到解决办法是使用CCTargetedAction类. C ...

  2. Android Paint类介绍以及浮雕和阴影效果的设置

    Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制信息. 1.图形绘制 setARGB(int a,int r,int g,int b); 设置绘制的颜色,a代表透明 ...

  3. ubuntu常用文件搜索命令

    1.find find [搜索路径] [搜索关键字] 比如查找/test中文件名为t5.tmp的文件: 查找根目录下大于100M的文件 注意,这里的204800单位是块,1块=512字节 在根目录下查 ...

  4. iOS动画进阶 - 教你写 Slack 的 Loading 动画

    (转载自:http://blog.csdn.net/wang631106979/article/details/52473985) 如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博 ...

  5. Android初级教程Activity小案例(计算器乘法运算)

    首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算:另一个布局文件进行显示最终结果.Activity1启动Activity2,并传递计算结果值给Activity2. main.xml: &l ...

  6. A*寻路算法入门(五)

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

  7. Socket编程实践(10) --select的限制与poll的使用

    select的限制 用select实现的并发服务器,能达到的并发数一般受两方面限制: 1)一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n(number)来调整或 ...

  8. LINUX0.11 内核阅读笔记

    一.源码目录 图1 二.系统总体流程: 系统从boot开始动作,把内核从启动盘装到正确的位置,进行一些基本的初始化,如检测内存,保护模式相关,建立页目录和内存页表,GDT表,IDT表.然后进入main ...

  9. OJ题:将一个数倒置输出

    题目描述: 输入一个数,假如num = 12345 , 输出 54321,以此类推. 代码实现: ) ; }

  10. Linux0.11中对文本文件进行修改的策略

    现在,假设 hello.txt 是硬盘上已有的一个文件,而且内容为 "hello, world" ,在文件的当前指针设置完毕后,我们来介绍 sys_read , sys_write ...