【问题标题】:How to run automated test using css Selector Appium java如何使用 css Selector Appium java 运行自动化测试
【发布时间】:2016-07-18 07:12:00
【问题描述】:

我正在尝试使用 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>

非常感谢您的帮助。

【问题讨论】:

    标签: java android css webview appium


    【解决方案1】:

    你能发布 Appium 控制台的输出吗?可能您的应用程序将setWebContentsDebuggingEnabled 设置为false。确保您的应用已启用它,否则您将没有 WebView 上下文。

    【讨论】:

    • 不管怎样,我找到了另一种切换到webview的方法,你知道答案吗? link 非常感谢。如何检查应用程序是否将其设置为 true 或 false?因为我没有成功,我只是在测试它。谢谢。
    • 您可以在 Appium Inspector 中查看。在所选元素的属性下有一个“上下文”菜单(检查屏幕截图 - imgur.com/a/RoFZy )。如果有 WebView ,则您的应用已启用 WebView 调试。否则,您需要要求开发人员将其包含在应用程序中。
    • 我使用的是 Windows 版 Appium,检查员没有上下文。无论如何在 Windows 的 Appium 中看到它还是必须使用 Mac?谢谢。
    • 你的方法的一部分应该存储可用的上下文Set&lt;String&gt; availableContexts = driver.getContextHandles(); System.out.println("Total No of Context Found After we reach to WebView = "+ availableContexts.size());。您可以遍历此集合并检查是否存在“WEBVIEW”上下文或只有本机上下文。出于测试目的,您可以惰性 Thread.sleep(time) 以加载应用程序。
    • 我确实使用了它,它打印了 2 个,由 WEBVIEW 和 NATIVE_APP 组成。但现在我得到一个 WebDriverException:处理命令时发生未知的服务器端错误。你知道如何解决这个问题吗?
    猜你喜欢
    • 2014-08-03
    • 2020-06-30
    • 2023-03-10
    • 2015-09-27
    • 2013-09-10
    • 2021-12-05
    • 2021-10-11
    • 2015-07-10
    • 2019-01-23
    相关资源
    最近更新 更多