Java 怎么为Appium设置移动功能

2019年7月29日19:57:30Java 怎么为Appium设置移动功能已关闭评论 566 2108字阅读7分1秒

1.简介

Java 怎么为Appium设置移动功能?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public class Timespoints_Hybrid_Emulator {

    public static AndroidDriver<MobileElement> driver;

    public static void main(String[] args) throws MalformedURLException,
            InterruptedException {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "6.0.1");
        capabilities.setCapability("deviceName", "G5AXB730P3112UM");
        capabilities.setCapability("appPackage", "com.til.timespoints");
        capabilities.setCapability("appActivity",
                "com.til.timespoints.SplashActivity");

        driver = new AndroidDriver<MobileElement>(new URL(
                "http:
        driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
        try {
            driver.findElement(
                    By.id("com.til.timespoints:id/tp_button_sign_in"))
                    .click();

            Set<String> contextNames = driver.getContextHandles();
            for (String context : contextNames) {
                System.out.println(context);
                if (context.contains("WEBVIEW")) {
                    driver.context(context);
                }
            }

            System.out.println(driver.getPageSource());
            Thread.sleep(10000);

            driver.findElement(By.name("emailId")).sendKeys(
                    "times.newswire@gmail.com");
            Thread.sleep(2000);
            driver.findElement(
                    By.xpath("
                    .click();
            Thread.sleep(2000);

            driver.findElement(By.id("password")).sendKeys("Times@123");
            Thread.sleep(2000);

            driver.findElement(
                    By.xpath("
                    .click();
            Thread.sleep(2000);

            TouchAction action = new TouchAction((MobileDriver) driver);
            action.tap(400, 1500).release().perform();

        }

        catch (Exception e) {

            Thread.sleep(5000);
            driver.findElement(
                    By.id("com.til.timespoints:id/tp_image_settings"))
                    .click();
            String str = "Sign Out";
            driver.findElementByAndroidUIAutomator(
                    "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""
                            + str + "\").instance(0))").click();

        }
    }

}

 
继续阅读
编程十万个怎么办