当前位置:网站首页>Data driving of appium framework for mobile terminal automated testing
Data driving of appium framework for mobile terminal automated testing
2022-07-03 16:27:00 【Programming simplicity】
background
Automation is a service for business testing , If data and business are put together, it doesn't look professional , It is absolutely necessary to separate data from business , This is also a common data-driven framework in the market , Today, let's talk about how to build our own data-driven framework .
precondition
Appium The environment was built successfully , No reference to automation tools Appium Introductory articles , I really don't know how to build it on the reference network .
Main facts
UI Automation framework elements are stored in text 、 data storage 、 Direct code storage, etc. in fact, any one can , It's only convenient for your own users , I use text files to store , It's easy to modify , Script placement SVN/Git Just manage ,UI Automatic element operations are basically common ID/NAME/XPATH Wait for these kinds , Let's write a tool conversion class .
Element reading tool class
/**
* Mobile element positioning
*
* @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(" Get elements key: " + name + "\t Get the location type :" + locatorType + "\t Gets the location expression for the : " + locatorValue);
// according to locatorType Variable value content judgment , Return the location method By object
return getkeyVule(locatorType, locatorValue);
} else {
new RuntimeException(" Element did not get !");
return null;
}
}
/**
* Read elements
* @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);
}
}
Copy code
Read file operation method
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(" No files found :" + filename);
e.printStackTrace();
}
}
Copy code
Write where elements are stored :
The content is :
#\u70B9\u51FB\u9996\u9875
clickCart=xpath>>//android.view.View[@content-desc='\u9996\u9875']
Copy code
Writing test classes
@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);
}
Copy code
The verification results :
Element waiting for finder class
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: Wait to find the element tool class
* @date 2019/11/23 / 10:58
*/
public class WaitUtil {
public static void waitWebElement(AndroidDriver<AndroidElement> driver, By by, String name) {
LogUtil.info(" Wait for the element to appear --> The current operation steps are :" + name);
Reporter.log(" The current operation steps are :" + name);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
}
Copy code
Element positioning class
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;
/**
* Constructors
*/
public CartProvider() {
this.getByLocator = new GetByLocator("loginElement.properties");
}
/**
* Click home
*/
public void clickHome(AndroidDriver<AndroidElement> driver) {
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("clickCart"), " Click home ");
element = driver.findElement(getByLocator.getLocatorApp("clickCart"));
element.click();
}
/**
* Click the treasure chest
*
* @param driver
*/
public void clickbaibao(AndroidDriver<AndroidElement> driver) {
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("clickCart"), " Treasure chest ");
String[] name = {" Jingdong supermarket ", " Digital appliances ", " Jingdong clothing ", " Jingdong fresh "};
for (int i = 0; i < name.length; i++) {
element = driver.findElement(getByLocator.getLocatorApp("Jingdong_supermarket".replace(" Jingdong supermarket ", name[i])));
element.click();
// Assertion
assertTrue(driver, "assertTitile", name[i] + " Jump successful ");
// return
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("home_back"), " return ");
driver.findElement(getByLocator.getLocatorApp("home_back"));
}
}
/**
* Classified writing
*
* @param driver
*/
public void categorylist(AndroidDriver<AndroidElement> driver) {
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("categorylist"), " Traversal classification ");
// Get all the data and traverse one by one
List<AndroidElement> categorylist = driver.findElements(getByLocator.getLocatorApp("categorylist"));
for (int i = 0; i < categorylist.size(); i++) {
String text = categorylist.get(i).getText();
// Output traversal name
LogUtil.info(text);
categorylist.get(i).click();
}
}
/**
* Collection
*
* @param driver
*/
public void productdetail(AndroidDriver<AndroidElement> driver) {
WaitUtil.waitWebElement(driver, getByLocator.getLocatorApp("productdetail"), " Collection ");
AndroidElement detail = driver.findElement(getByLocator.getLocatorApp("productdetail"));
String attribute = detail.getAttribute("selected");
if (attribute.equals(true)) {
LogUtil.info(" At present, the commodity collection , The current attribute is :" + attribute);
} else {
LogUtil.info(" There is no collection , The current attribute is :" + attribute);
}
}
/**
* Unified judgment on whether to pass
*
* @param driver Pass on driver
* @param elemn Definite element
* @param des describe
*/
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 + " Verify success ");
} else {
// If it fails, the system will automatically take a screenshot
Assert.fail(" Jump failure ");
}
}
}
// Assert.assertTrue("", " Verify that the jump is successful ");
// driver.findElement(By.id(""))
Copy code
Basic startup class
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: Use case area
* @date 2019/11/21 / 10:06
*/
public class BestRuner {
static AndroidDriver<AndroidElement> driver;
public static AndroidDriver<AndroidElement> getDriver() {
return driver;
}
/**
* Initialize the run class
*
* @param udid
* @param port
* @throws Exception
*/
@BeforeClass
@Parameters({"udid", "port"})
public void BeforeClass(String udid, String port) {
Reporter.log(" step 1: start-up appium And applications ", true);
LogUtil.info("--- This is the device ID Number -->" + udid);
LogUtil.info("-- This is the run port --->" + port);
// Get the package name and... Through the path 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 = " Initialize login startup ", testName = " Enter the home page ", priority = 0)
public void T001() {
LogUtil.info(" start-up ");
driver.findElement(By.id("com.jingdong.app.mall:id/mj")).click();
}
}
Copy code
Business combination test class
package appout.appcase;
import appout.appcase.data.CartProvider;
import org.testng.annotations.Test;
/**
* @author 7DGroup
* @Title: CartTest
* @Description: Test class extends BestRuner
* @date 2019/11/23 / 19:57
*/
public class CartTest {
CartProvider cartProvider = new CartProvider();
@Test(description = " Click the treasure chest on the home page ", testName = " Click the treasure chest ", priority = 1)
public void T002() {
// cartProvider.clickHome(driver);
// cartProvider.clickbaibao(driver);
System.out.println(" debugging ");
}
}
Copy code
Other test class integration , that will do
summary
Although the project is small, it includes how to store data , How to get , How to change back to selenium The type of identification .
In this paper, the source code :
边栏推荐
- Golang decorator mode and its use in NSQ
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- 【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
- 为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
- AcWing 第58 场周赛
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- MongoDB 的安装和基本操作
- Is it safe to open an account with tongdaxin?
- Record a jar package conflict resolution process
- 1287. Elements that appear more than 25% in an ordered array
猜你喜欢
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
uploads-labs靶场(附源码分析)(更新中)
Visual SLAM algorithms: a survey from 2010 to 2016
Explore Cassandra's decentralized distributed architecture
Basis of target detection (IOU)
MongoDB 的安装和基本操作
One article takes you to understand machine learning
From the 18th line to the first line, the new story of the network security industry
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
随机推荐
Stm32f103c8t6 firmware library lighting
Unity project optimization case 1
14 topics for performance interviews between superiors and subordinates (4)
【LeetCode】94. Middle order traversal of binary tree
(补)双指针专题
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
线程池执行定时任务
"Remake Apple product UI with Android" (3) - elegant statistical chart
Explore Netease's large-scale automated testing solutions see here see here
Threejs Part 2: vertex concept, geometry structure
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
One article takes you to understand machine learning
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
六月 致 -.-- -..- -
Basis of target detection (IOU)
Myopia: take off or match glasses? These problems must be understood clearly first
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
PHP中register_globals参数设置
Deep understanding of grouping sets statements in SQL
一台服务器最大并发 tcp 连接数多少?65535?