背景
自动化是为业务测试服务,如果数据与业务放在一起看起来不是专业,把数据与业务分离是完全必要的,这也是市场常见的数据驱动框架,今天咱们就聊一聊怎么搭建自己数据驱动框架。
前置条件
Appium 环境搭建成功,不会参考自动化工具之 Appium 入门文章,实在不会在参考网上怎么搭建。
主要实现
UI 自动化框架元素放文本存储、数据存储、代码直接存储等其实随便那个都行,只有自己用者方便就行,我工作中用文本文件存储,这样修改方便,脚本放 SVN/Git 管理就行,UI 自动元素操作基本常见的就是 ID/NAME/XPATH 等这几种,咱们写一个工具转换类即可。
元素读取工具类
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);
return getkeyVule(locatorType, locatorValue);
} else {
new RuntimeException("元素没有获取到!");
return null;
}
}
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();
}
}
编写元素存放位置: 内容为:
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;
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;
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();
}
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"));
}
}
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();
LogUtil.info(text);
categorylist.get(i).click();
}
}
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);
}
}
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("跳转失败");
}
}
}
基础启动类
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;
public class BestRuner {
static AndroidDriver<AndroidElement> driver;
public static AndroidDriver<AndroidElement> getDriver() {
return driver;
}
@BeforeClass
@Parameters({"udid", "port"})
public void BeforeClass(String udid, String port) {
Reporter.log("步骤1:启动appium与应用", true);
LogUtil.info("---这是设备ID号-->" + udid);
LogUtil.info("--这是运行端口--->" + port);
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;
public class CartTest {
CartProvider cartProvider = new CartProvider();
@Test(description = "点击首页百宝箱", testName = "点击百宝箱", priority = 1)
public void T002() {
System.out.println("调试");
}
}
其他测试类集成,即可
总结
项目虽然小但包含了数据怎么存放,怎么获取,怎么换回成 selenium 识别的类型。
本文源码:
|