Appium簡單測試用例

工程目錄以下:java

封裝初始化androiddriver 方法android

import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;

public class DeviceSession {
    static
    {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
    static DesiredCapabilities capabilities=null;
    public static DesiredCapabilities getDesiredCapabilities()
    {
        //由於全部在java.io中的類都是將相對路徑名解釋爲起始於用戶的當前工做目錄,
        // 因此能夠經過調用System.getProperty("user.dir") 來得到
                File classpathRoot = new File(System.getProperty("user.dir"));//默認文件位置
        File appDir = new File(classpathRoot, "apk");
        File apk = new File(appDir, "android-debug.apk");
        if(capabilities==null)
        {
            capabilities = new DesiredCapabilities();
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("deviceName", "UGUWUGTWYP49KVNJ");
            capabilities.setCapability("platformVersion", "4.2");
            capabilities.setCapability("app", apk.getAbsolutePath());//"E:\\apk\\android-debug.apk"(絕對路徑,存放在電腦的某個位置)
            capabilities.setCapability("appPackage", "com.eshare.Purse");
            capabilities.setCapability("automationName", "Appium");
            capabilities.setCapability("appActivity", ".MainActivity");
            
        }
        return capabilities;
    }
   }            

 測試用例以下:json

import com.eshare.qa.common.*;
import com.eshare.qa.wireless.DeviceSession;
import io.appium.java_client.android.AndroidDriver;
import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import java.net.URL;

public class TestPurse {
    public AndroidDriver<WebElement> driver=null;
    Assertion assertion;
    @BeforeClass
    public void setUp() throws  Exception{
        assertion = new Assertion();
        DesiredCapabilities capabilities = DeviceSession.getDesiredCapabilities();
        try {
            driver = new AndroidDriver<WebElement>(
                    new URL(Configuration.Url), capabilities);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        //System.out.println("new!");
        try {
            Thread.sleep(15 * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void test() throws  Exception{
        boolean ret = ViewUtils.waitForWebViewInit(driver,"WEBVIEW_com.eshare.Purse");
        assertion.assertTrue(ret);
        driver.context("WEBVIEW");
        String loginButtonXpath = "//*[text()='手機登入']";
        boolean isExsit = isElementExist(loginButtonXpath);
        if(isExsit){
            login();
            afterLogin();
        }else{
            afterLogin();
        }
    }
    @AfterClass
    public void teardown(){
        driver.quit();
    }
相關文章
相關標籤/搜索