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

2022-11-22,,,,

需求:

系统需要做下单退款撤销回归测试,有下单页面,所以就想到用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+pageobject+pagefactory实现自动化下单、退款、撤销回归测试.doc》

下载本文的Word格式文档,以方便收藏与打印。