java - 无法从 native 切换到 WEBVIEW_chrome,显示异常 : io. appium.java_client.NoSuchContextException

标签 java selenium google-chrome adb appium-android

我正在真实设备上测试 Web 应用程序并使用 Chrome 浏览器,我尝试设置集合,并且同时获得 NATIVE_APP 和 WEBVIEW_chrome,但是当我尝试使用 driver.context() 方法切换到 WEBVIEW_chrome 时,它​​会抛出 Exception"io .appium.java_client.NoSuchContextException:io.appium.java_client.NoSuchContextException:处理命令时发生未知的服务器端错误。原始错误:处理命令时发生未知的服务器端错误。原始错误:未知错误:无法将端口转发到设备 18e6bebc:。未知错误:adb 命令失败。额外响应:<>。 请帮助我,自过去两周以来我一直在挣扎。提前致谢。

我已经尝试了 amazon.in 的功能和测试脚本,如下所示:

private static AndroidDriver<WebElement> driver = null;  
    @BeforeTest
    public void initConfig() throws MalformedURLException, InterruptedException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "Galaxy C9 Pro");
        capabilities.setCapability("platformVersion", "8.0.0");
    capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.android.chrome");
        capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");
        capabilities.setCapability("startIWDP", true);
        capabilities.setCapability("adbExecTimeout", 20000);
        capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

}

@Test
public void chromeTest() throws InterruptedException {

        System.out.println("Hello Android");
        System.out.println("Started Mobile Testing......");
        //driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        System.out.println("start");

        System.out.println("after wait 3000----------------------------------------------");
        Set<String> context = driver.getContextHandles();
          System.out.println(context.size());

          for(String contexts : context) { System.out.println(contexts); }

          WebElement searchbar_txt = driver.findElementById("com.android.chrome:id/search_box_text");
            searchbar_txt.click();

            WebElement url_Bar = driver.findElementById("com.android.chrome:id/url_bar");

            url_Bar.sendKeys("amazon.in");

            WebElement urlhistory = driver.findElementByXPath("//*[@text='amazon.in']");
            //  findElementByName("Kotak Securities");
        urlhistory.click();
        Thread.sleep(4000);

             Set<String> context1 = driver.getContextHandles();
              System.out.println(context1.size());

              for(String contexts : context1) { System.out.println(contexts); }
             // driver.context((String) context1.toArray()[1]);

             driver.context("WEBVIEW_chrome");
              Thread.sleep(4000);

              driver.findElementById("nav-search-keywords").sendKeys("Bags");
             driver.findElementByTagName("Submit").click();

Eclipse 控制台输出:

Nov 02, 2019 12:19:03 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: W3C
Hello Android
Started Mobile Testing......
start
after wait 3000----------------------------------------------
1
NATIVE_APP
2
NATIVE_APP
WEBVIEW_chrome
FAILED: chromeTest
io.appium.java_client.NoSuchContextException: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: unknown error: Failed to forward ports to device 18e6bebc: . unknown error: The adb command failed. Extra response: <>.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'TVDLT020', ip: '192.168.1.12', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {adbExecTimeout: 20000, appActivity: com.google.android.apps.chr..., appPackage: com.android.chrome, databaseEnabled: false, desired: {adbExecTimeout: 20000, appActivity: com.google.android.apps.chr..., appPackage: com.android.chrome, deviceName: Galaxy C9 Pro, noReset: true, platformName: android, platformVersion: 8.0.0, startIWDP: true}, deviceApiLevel: 26, deviceManufacturer: samsung, deviceModel: SM-C900F, deviceName: 18e6bebc, deviceScreenDensity: 420, deviceScreenSize: 1080x1920, deviceUDID: 18e6bebc, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, noReset: true, pixelRatio: 2.625, platform: LINUX, platformName: Android, platformVersion: 8.0.0, startIWDP: true, statBarHeight: 63, takesScreenshot: true, viewportRect: {height: 1857, left: 0, top: 63, width: 1080}, warnings: {}, webStorageEnabled: false}
Session ID: 3e90e00b-2eba-40b9-9b85-fc66fde1b491
    at io.appium.java_client.AppiumDriver.context(AppiumDriver.java:229)
    at appiumAndroidApp.appiumAndroid_GoogleWeb.chromeTest(appiumAndroid_GoogleWeb.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: unknown error: Failed to forward ports to device 18e6bebc: . unknown error: The adb command failed. Extra response: <>.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'TVDLT020', ip: '192.168.1.12', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {adbExecTimeout: 20000, appActivity: com.google.android.apps.chr..., appPackage: com.android.chrome, databaseEnabled: false, desired: {adbExecTimeout: 20000, appActivity: com.google.android.apps.chr..., appPackage: com.android.chrome, deviceName: Galaxy C9 Pro, noReset: true, platformName: android, platformVersion: 8.0.0, startIWDP: true}, deviceApiLevel: 26, deviceManufacturer: samsung, deviceModel: SM-C900F, deviceName: 18e6bebc, deviceScreenDensity: 420, deviceScreenSize: 1080x1920, deviceUDID: 18e6bebc, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, noReset: true, pixelRatio: 2.625, platform: LINUX, platformName: Android, platformVersion: 8.0.0, startIWDP: true, statBarHeight: 63, takesScreenshot: true, viewportRect: {height: 1857, left: 0, top: 63, width: 1080}, warnings: {}, webStorageEnabled: false}
Session ID: 3e90e00b-2eba-40b9-9b85-fc66fde1b491
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at io.appium.java_client.AppiumDriver.context(AppiumDriver.java:226)
    ... 26 more


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

我无法切换上下文并在 amazon.in 的搜索栏上发送 key ,请指导我在哪里犯了错误或我做错了什么。

有关 Appium 日志,请参阅以下链接:

https://1drv.ms/t/s!AhlCZJfQWY3ngzWsr7mLaF4CtxO9?e=gFXxDd

最佳答案

无需切换上下文,您可以使用 chrome 功能和 chromeDriver 本身的路径来运行它。 包括行:
features.setCapability("appium:chromeOptions", ImmutableMap.of("w3c", false)); 忽略 w3c 服务,因为 appium 或 chromedriver 似乎不再支持它。

关于java - 无法从 native 切换到 WEBVIEW_chrome,显示异常 : io. appium.java_client.NoSuchContextException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58669174/

相关文章:

c# - 必须为所有区域将启用保护模式设置为相同的值(启用或禁用)

javascript - 默认接收器上的 Chromecast WebVTT 字幕

javascript - 任何应用程序(chrome、flash 等)如何获得比系统时间分辨率更好的时间分辨率?

java - 最后一个数据包成功发送到服务器是在 > 70,400,003 毫秒之前。比服务器配置的长

java - Selenium IDE - 在表格中特定文本的同一行中查找链接

java - Ubuntu 加载 JNI 失败

python - 如何获取python-selenium中元素的 'state'?

google-chrome - 撤消 Google Chrome 检查器的放大镜工具?

java - 通过 "Always on Display"启动应用程序并锁定屏幕

java - 什么应该是最好的设计方法