关键词:
背景
自动化是为业务测试服务,如果数据与业务放在一起看起来不是专业,把数据与业务分离是完全必要的,这也是市场常见的数据驱动框架,今天咱们就聊一聊怎么搭建自己数据驱动框架。
前置条件
Appium 环境搭建成功,不会参考自动化工具之 Appium 入门文章,实在不会在参考网上怎么搭建。
主要实现
UI 自动化框架元素放文本存储、数据存储、代码直接存储等其实随便那个都行,只有自己用者方便就行,我工作中用文本文件存储,这样修改方便,脚本放 SVN/Git 管理就行,UI 自动元素操作基本常见的就是 ID/NAME/XPATH 等这几种,咱们写一个工具转换类即可。
元素读取工具类
/**
* 移动端元素定位
*
* @return
* @param: name
*/
public By getLocatorApp(String name)
String locator = properties.getProperty(name.trim());
String locatorType = locator.split(">>")[0].toLowerCase();
String locatorValue = locator.split(">>")[1];
if (locatorType != null && locatorType != null)
logger.info("获取元素key: " + name + "\\t 获取的定位类型:" + locatorType + "\\t 获取的定位表达式: " + locatorValue);
// 根据 locatorType 的变量值内容判断,返回何种定位方式的 By 对象
return getkeyVule(locatorType, locatorValue);
else
new RuntimeException("元素没有获取到!");
return null;
/**
* 读取元素
* @param locatorType
* @param locatorValue
* @return By
*/
public By getkeyVule(String locatorType, String locatorValue)
if ("id".equals(locatorType.toLowerCase()))
return By.id(locatorValue);
else if ("name".equals(locatorType.toLowerCase()))
return By.name(locatorValue);
else if (("classname".equals(locatorType.toLowerCase())) || ("class".equals(locatorType.toLowerCase())))
return By.className(locatorValue);
else if (("tagname".equals(locatorType.toLowerCase())) || ("tag".equals(locatorType.toLowerCase())))
return By.className(locatorValue);
else if (("linktext".equals(locatorType.toLowerCase())) || ("link".equals(locatorType.toLowerCase())))
return By.linkText(locatorValue);
else if ("partiallinktext".equals(locatorType.toLowerCase()))
return By.partialLinkText(locatorValue);
else if (("cssselector".equals(locatorType.toLowerCase())) || ("css".equals(locatorType.toLowerCase())))
return By.cssSelector(locatorValue);
else if ("xpath".equals(locatorType.toLowerCase()))
return By.xpath(locatorValue);
else
return By.tagName(locatorValue);
读取文件操作方法
public GetByLocator(String filename)
properties = new Properties();
try
FileInputStream in = new FileInputStream((sysProperty.getProperty("user.dir") + "/comsevenday/src/main/resources/uiElement/" + filename));
this.properties.load(in);
in.close();
catch (Exception e)
logger.info("找不到文件:" + filename);
e.printStackTrace();
编写元素存放位置:
内容为:
#\\u70B9\\u51FB\\u9996\\u9875
clickCart=xpath>>//android.view.View[@content-desc='\\u9996\\u9875']
编写测试类
@Test
public void ss() throws IOException
InputStream in = this.getClass().getResourceAsStream("/uielement/loginElement.properties");
Properties properties = new Properties();
properties.load(in);
String to_shop_text1 = properties.getProperty("clickCart");
System.out.println(to_shop_text1);
验证结果:
元素等待查找工具类
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
/**
* @author 7DGroup
* @Title: WaitUtil
* @Description: 等待查找元素工具类
* @date 2019/11/23 / 10:58
*/
public class WaitUtil
public static void waitWebElement(AndroidDriver<AndroidElement> driver, By by, String name)
LogUtil.info("等待元素出现-->目前操步骤为:" + name);
Reporter.log("目前操步骤为:" + name);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(by));
元素定位类
package appout.appcase.data;
import appout.utils.GetByLocator;
import appout.utils.LogUtil;
import appout.utils.WaitUtil;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import java.util.List;
/**
* @author 7DGroup
* @Title: CartProvider
* @Description: this is
* @date 2019/11/23 / 10:57
*/
public class CartProvider
private GetByLocator getByLocator;
private WebElement element = null;
/**
* 构造函数
*/
public CartProvider()
this.getByLocator = new GetByLocator("loginElement.properties");
/**
* 点击首页
*/
public void clickHome(AndroidDriver<AndroidElement> driver)
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("clickCart"), "点击首页");
element = driver.findElement(getByLocator.getLocatorApp("clickCart"));
element.click();
/**
* 点击百宝箱
*
* @param driver
*/
public void clickbaibao(AndroidDriver<AndroidElement> driver)
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("clickCart"), "百宝箱");
String[] name = "京东超市", "数码电器", "京东服饰", "京东生鲜";
for (int i = 0; i < name.length; i++)
element = driver.findElement(getByLocator.getLocatorApp("Jingdong_supermarket".replace("京东超市", name[i])));
element.click();
//断言
assertTrue(driver, "assertTitile", name[i] + "跳转成功");
//退回
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("home_back"), "退回");
driver.findElement(getByLocator.getLocatorApp("home_back"));
/**
* 分类写法
*
* @param driver
*/
public void categorylist(AndroidDriver<AndroidElement> driver)
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("categorylist"), "遍历分类");
//获取全部数据挨个遍历
List<AndroidElement> categorylist = driver.findElements(getByLocator.getLocatorApp("categorylist"));
for (int i = 0; i < categorylist.size(); i++)
String text = categorylist.get(i).getText();
//输出遍历name
LogUtil.info(text);
categorylist.get(i).click();
/**
* 收藏
*
* @param driver
*/
public void productdetail(AndroidDriver<AndroidElement> driver)
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("productdetail"), "收藏");
AndroidElement detail = driver.findElement(getByLocator.getLocatorApp("productdetail"));
String attribute = detail.getAttribute("selected");
if (attribute.equals(true))
LogUtil.info("目前商品收藏,目前的属性为:" + attribute);
else
LogUtil.info("商品没有收藏,目前的属性为:" + attribute);
/**
* 统一判断是否通过
*
* @param driver 传driver
* @param elemn 定元素
* @param des 描述
*/
public void assertTrue(AndroidDriver<AndroidElement> driver, String elemn, String des)
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp(elemn), des);
element = driver.findElement(getByLocator.getLocatorApp(elemn));
String text = element.getText();
if (text != null)
Assert.assertTrue(true, des + "验证成功");
else
//如果失败系统会自动截图
Assert.fail("跳转失败");
// Assert.assertTrue("", "验证跳转成功");
// driver.findElement(By.id(""))
基础启动类
package appout.appcase;
import appout.base.DriverBase;
import appout.utils.LogUtil;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* @author 7DGroup
* @Title: LoginTest
* @Description: 用例区域
* @date 2019/11/21 / 10:06
*/
public class BestRuner
static AndroidDriver<AndroidElement> driver;
public static AndroidDriver<AndroidElement> getDriver()
return driver;
/**
* 初始化运行类
*
* @param udid
* @param port
* @throws Exception
*/
@BeforeClass
@Parameters("udid", "port")
public void BeforeClass(String udid, String port)
Reporter.log("步骤1:启动appium与应用", true);
LogUtil.info("---这是设备ID号-->" + udid);
LogUtil.info("--这是运行端口--->" + port);
//通过路径获取包名与APP_ACTIVITY
String apk = "com.jingdong.app.mall_70502.apk";
driver = DriverBase.initDriver(port, udid, apk, true);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
@Test(description = "初始化登录启动", testName = "进入首页", priority = 0)
public void T001()
LogUtil.info("启动");
driver.findElement(By.id("com.jingdong.app.mall:id/mj")).click();
业务组合测试类
package appout.appcase;
import appout.appcase.data.CartProvider;
import org.testng.annotations.Test;
/**
* @author 7DGroup
* @Title: CartTest
* @Description: 测试类 extends BestRuner
* @date 2019/11/23 / 19:57
*/
public class CartTest
CartProvider cartProvider = new CartProvider();
@Test(description = "点击首页百宝箱", testName = "点击百宝箱", priority = 1)
public void T002()
// cartProvider.clickHome(driver);
// cartProvider.clickbaibao(driver);
System.out.println("调试");
其他测试类集成,即可
总结
项目虽然小但包含了数据怎么存放,怎么获取,怎么换回成 selenium 识别的类型。
本文源码:
appium移动端自动化测试一
最近公司决定使用appium为主体框架做一个移动端得自动化测试系统,我会陆续记录项目得进展。1.首先是appium环境得搭建,需要安装appium-server,appium-desktop,android-sdk等一系列工具,编译工具我用的是IDEA 这些安装请自行百度。... 查看详情
移动端自动化测试之appium+pyhton环境准备篇
由于工作的需要进行Andriod手机app自动化测试,公司现有支持的app的手机平台(Andriod和IOS),所以选择了Appium工具。因为Andriod和IOS,Appium都支持。百度和谷歌上的各种Appium教程可谓是琳琅满目,但大抵都是浅尝辄止。因此笔... 查看详情
python爬虫:使用appium在移动端抓取微博数据(代码片段)
...一条信息使用Appium在移动端抓取微博数据Appium是移动端的自动化测试工具,读者可以类比为PC端的selenium。通过它,我们可以驱动App完成自动化的一系列操作,同样也可以爬取需要的内容。这里,我们需要首先在PC... 查看详情
python爬虫:使用appium在移动端抓取微博数据(代码片段)
...一条信息使用Appium在移动端抓取微博数据Appium是移动端的自动化测试工具,读者可以类比为PC端的selenium。通过它,我们可以驱动App完成自动化的一系列操作,同样也可以爬取需要的内容。这里,我们需要首先在PC... 查看详情
appium移动自动化测试之元素定位
实验简介 做过UI自动化(web自动化,移动自动化)的同学都会知道,除去框架的选型和搭建以外,落到实处的对元素进行定位就成了最重要的技能. 做过UI自动化的同学会知道,对页面元素的定位方式有8种:id,name,xpath,class,linkte... 查看详情
移动端自动测试工具appium快速入门(代码片段)
文章目录一、前言二、Appium概述1、架构图2、UI自动化收益三、环境安装1、桌面版本安装2、DOS命令安装3、安装SDK四、常用操作方法五、常见定位方式1、ID定位2、name定位3、xpath定位4、定位h5页面六、简单demo示例七、运行效果八... 查看详情
app自动化测试系列之appium介绍及运行原理
在面试APP自动化时,有的面试官可能会问Appium的运行原理,以下介绍Appium运行原理。一、Appium介绍1、Appium概念Appium是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序测试。它使用WebDriver协议驱动IOS... 查看详情
appium+java实现androidapp自动化测试(代码片段)
一、Appium:是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序测试。它使用WebDriver协议驱动iOS,Android和Windows应用程序;UiAutomator测试框架是AndroidSDK自带的AppUI自动化测试Java库;appium在Android... 查看详情
appium+java实现androidapp自动化测试(代码片段)
一、Appium:是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序测试。它使用WebDriver协议驱动iOS,Android和Windows应用程序;UiAutomator测试框架是AndroidSDK自带的AppUI自动化测试Java库;appium在Android... 查看详情
移动端自动化测试工具appium快速入门(代码片段)
文章目录一、前言二、Appium概述1、架构图2、UI自动化收益三、环境安装1、桌面版本安装2、DOS命令安装3、安装SDK四、常用操作方法五、常见定位方式1、ID定位2、name定位3、xpath定位4、定位h5页面六、简单demo示例七、运行效果八... 查看详情
python+appium自动化测试——appium并发测试之多设备启动(代码片段)
来自APPAndroid端自动化测试初学者的笔记,写的不对的地方大家多多指教哦一、启动appium服务器1.通过命令行窗口启动单个appium服务器appium--直接打开默认的4723端口号appium-p4723--使用-p来启动固定端口号的appium服务器复制代码2.... 查看详情
移动端app自动化测试超全基础汇总
目录一.面试过程1.自动化岗位要求 2.面试流程,面试类型 3.沟通技巧,不同级别要求二.真实面试案例1.一个输入框的面试题(有人拿到高级岗位,有人连初级都没拿到,为什么)三.自我分析1.积累的知识决定了初入社... 查看详情
使用 Appium 进行移动应用程序自动化测试
】使用Appium进行移动应用程序自动化测试【英文标题】:MobileappautomationtestingusingAppium【发布时间】:2021-10-1104:45:37【问题描述】:我在安装Appium以进行移动自动化测试时遇到错误。我在驱动器G而不是C中安装了androidstudio。当我... 查看详情
appium测试apk
...个开源、跨平台的测试框架,可以用来测试原生及混合的移动端应用。Appium支持iOS、Android及FirefoxOS平台测试。Appium使用WebDriver的jsonwire协议,来驱动Apple系统的UIAutomation库、Android系统的UIAutomator框架。特点:跨架构跨设备跨语言... 查看详情
移动端自动化测试appium环境搭建
自动化测试有主要有两个分类,接口自动化和ui自动化,ui自动化呢又分移动端的和web端的,当然还有c/s架构的,这种桌面程序应用的自动化,使用QTP,只不过现在没人做了。 web自动化呢,现在基本上都是用selenium,webdriver这... 查看详情
python+appium自动化测试-appium并发测试之多设备启动(代码片段)
一、启动appium服务器(文末有福利哦)1.通过命令行窗口启动单个appium服务器appium--直接打开默认的4723端口号appium-p4723--使用-p来启动固定端口号的appium服务器2.通过命令行窗口启动多个appium服务器appium-p4723appium-p4726二、启... 查看详情
appium-自动化测试框架-工作原理环境搭建
在这里,我还给大家推荐一套app自动化测试的视频,比文字内容更容易理解!python+Appium自动化测试框架【项目实战合集】,轻松掌握app高级自动化测试_哔哩哔哩_bilibilipython+Appium自动化测试框架【项目实战... 查看详情
appium使用之helloworld(代码片段)
...方面的内容,同时自己又属于比较懒的,所以期望可以做成自动化测试,写个脚本自动运行,在版本迭代后做回归测试会比较方便,另外,我期望测试框架可以根据id等属性来定位控件,而不是如monkeyRunner那样通过坐标来定位,以便于测试... 查看详情