如何使用css Selector Appium java运行自动化测试

时间:2016-07-18 07:12:00

标签: java android css webview appium

我正在尝试使用Java语言中的Appium进行自动化测试。但是,要访问元素,我在浏览器上使用webview查找使用css Selector。我的问题是如何在Java语言中使用Appium切换到webview?我发现appium支持selenium并且可以使用driver.switchTo().window("WEBVIEW")但事实并非如此。现在我使用我在互联网上找到的代码,但是当我运行脚本时,TestNG没有显示任何失败或成功或跳过的内容,设备上的应用程序只是打开然后关闭。有帮助吗? PS:我将com.xxx.xxx更改为此论坛的myApps。

    package com.android.test;
    import java.net.URL;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;

    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    import java.net.MalformedURLException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import static org.junit.Assert.assertEquals;

    public class Login2 {
        public static AppiumDriver<WebElement> driver;

        @BeforeTest
        public void before() throws MalformedURLException, InterruptedException{
              DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setCapability("deviceName", "192.168.150.101:5555");
              capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
              capabilities.setCapability(CapabilityType.VERSION, "5.1.0");
              capabilities.setCapability("platformName", "Android");
              capabilities.setCapability("appPackage", "myApps");
              capabilities.setCapability("appActivity", "myApps");
              driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
              driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
              Set<String> availableContexts = driver.getContextHandles();
                System.out.println("Total No of Context Found After we reach to WebView = "+ availableContexts.size());
                for(String context : availableContexts) {
                    if(context.contains("WEBVIEW")){
                        System.out.println("Context Name is " + context);
                        // 4.3 Call context() method with the id of the context you want to access and change it to WEBVIEW_1
                        //(This puts Appium session into a mode where all commands are interpreted as being intended for automating the web view)
                        driver.context(context);
                        break;
                            }
                        }
        }

        @Test
        public void login() throws InterruptedException{
            //Thread.sleep(6000);
            WebElement log = driver.findElement(By.cssSelector("div[ng-click^='logReg']"));
            log.click();
            driver.context("NATIVE_APP");
        }

        @AfterTest
        public void after(){
            driver.quit();
        }

    }

这是我试图选择点击的元素

    <div class="background-menu col box-menu loginIcon" ng-click="logReg()">
        <img class="menu_icon3" src="img/MenuIcon/blank.png" alt="login">
    </div>

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你可以从Appium控制台发布输出吗?您的应用可能ID, USERID, MAGIC 1, 1, ACD 2, 1, ACFX 3, 1, U128BH 设置为setWebContentsDebuggingEnabled。确保您的应用已启用它,否则您将无法使用WebView上下文。