Appium自动化测试框架搭建

 

 

1.在utils包中创建一个AppiumUtil类,这个类是对appium api进行封装的。

代码如下:

package utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.ITestResult;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.NoSuchContextException;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;

/**
 * @author young
 * @description appium api封装
 * */

public class AppiumUtil {

    public  AppiumDriver<WebElement> driver;
    public     ITestResult it;
    /**定义日志输出对象*/
    public static Logger logger = Logger.getLogger(AppiumUtil.class);

    /**获取driver
     * @throws  */
    public AppiumDriver<WebElement> getDriver(String url,DesiredCapabilities capabilities,String platform){

            if(platform.equalsIgnoreCase("android")){
            try {
                driver = new AndroidDriver<WebElement>(new URL(url), capabilities);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            }else if(platform.equalsIgnoreCase("ios")){
                try {
                    driver = new IOSDriver<WebElement> (new URL(url),capabilities);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }else{

            }
            return driver;

    }

    /**退出app*/
    public void closeApp(String appName){
        driver.closeApp();
        logger.info(appName+"已经关闭");
    }

    /**退出移动浏览器*/
    public void quit(){
        driver.quit();
        logger.info("driver已被清理");
    }
    /**通过By对象 去查找某个元素*/
    public WebElement findElement(By by){
        return driver.findElement(by);
    }

    /**
     * 通过By对象 去查找一组元素
     * */
    public List<WebElement> findElements(By by) {
        return driver.findElements(by);
    }

    /**清空元素内容*/
    public void clear(By byElement){
        WebElement element = findElement(byElement);
        element.clear();
        logger.info("清空元素:"+getLocatorByElement(element, ">")+"上的内容");
    }

    /**输入内容*/
    public void typeContent(By byElement,String str){
        WebElement element = findElement(byElement);
        element.sendKeys(str);
        logger.info("在元素:"+getLocatorByElement(element, ">")+"输入内容:"+str);
    }
    /**点击*/
    public void click(By byElement){
        WebElement element = findElement(byElement);
        try{
        element.click();
        logger.info("点击元素:"+getLocatorByElement(element, ">"));
        }catch(Exception e){
            logger.error("点击元素:"+getLocatorByElement(element, ">")+"失败", e);
            Assert.fail("点击元素:"+getLocatorByElement(element, ">")+"失败", e);
        }

    }

    /**查找一个元素 - appium新增的查找元素方法*/
    public WebElement findElement(String locateWay,String locateValue){
        WebElement element = null;
    switch(locateWay){

    case "AccessibilityId":
        element = driver.findElementByAccessibilityId(locateValue);
        break;
//    case "AndroidUIAutomator":
//        element = driver.findElementByAndroidUIAutomator(locateValue);
//            break;
    case "ClassName":
        element = driver.findElementByClassName(locateValue);
        break;
    case "CSS":
        element = driver.findElementByCssSelector(locateValue);
        break;
    case "ID":
        element = driver.findElementById(locateValue);
        break;
    case "LinkText":
        element = driver.findElementByLinkText(locateValue);
        break;
    case "Name":
        element = driver.findElementByName(locateValue);
        break;
    case "PartialLinkText":
        element = driver.findElementByPartialLinkText(locateValue);
        break;
    case "TagName":
        element = driver.findElementByTagName(locateValue);
        break;
    case "Xpath":
        element = driver.findElementByXPath(locateValue);
        break;
    default:
        logger.error("定位方式:"+locateWay+"不被支持");
        Assert.fail("定位方式:"+locateWay+"不被支持");

        }
    return element;

    }

    /**查找一组元素 - appium新增的查找元素方法*/
    public List<?> findElements(String locateWay,Strin
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值