Appium 无法在 C# 中从 NativeApp 切换到 WebView
Appium Unable to Switch from NativeApp to WebView in C#
我一直被一个问题困扰很久,无法解决,急需帮助。我有一个 android apk 文件,启动该应用程序后,它会显示网站的 Webview。启动App后无法从Native App切换到Webview。
我看到 2 个上下文可用(本机和 Webview),但我无法切换到 WebView,它无法显示“消息”:“ChromeDriver 准备好进行新会话”。以下是我尝试切换时的错误消息。我在 Appium 中验证了 ChromeDriver,它与 Chromeversion 相同并且 Appium 是 v4 latest
Error in switching Context : OpenQA.Selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Did not get a valid response object. Object was: {"value":{"build":{"version":"85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689})"},"message":"ChromeDriver ready for new sessions.","os":{"arch":"x86_64","name":"Windows NT","version":"10.0.17763"},"ready":true}}
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at MobileDriver.SwithContextToWebView(AppiumDriver`1 driver, String context) in C:\Automation\Src\Common\MobileDriver.cs:line 656
以下是我想要的功能
private static AppiumOptions SetCapability(MobileDriverConfig config)
{
AppiumOptions capabilities = new AppiumOptions();
capabilities.AddAdditionalCapability("automationName", config.AutomationName);
capabilities.AddAdditionalCapability("deviceName", "Android");
capabilities.AddAdditionalCapability("platformName", config.PlatformName);
capabilities.AddAdditionalCapability("platformVersion", config.PlatformVersion);
capabilities.AddAdditionalCapability(CapabilityType.Platform, config.SystemOs);
capabilities.AddAdditionalCapability("udid", config.DeviceId);
capabilities.AddAdditionalCapability("appPackage", config.AppPackage);
capabilities.AddAdditionalCapability("appActivity", config.AppActivity);
capabilities.AddAdditionalCapability("chromeBundleId", config.ChromeBundleId);
capabilities.AddAdditionalCapability("ensureWebviewsHavePages", config.EnsureWebviewsHavePages);
capabilities.AddAdditionalCapability("webviewDevtoolsPort", config.WebviewDevtoolsPort);
capabilities.AddAdditionalCapability("chromedriverUseSystemExecutable", config.ChromedriverUseSystemExecutable);
capabilities.AddAdditionalCapability("enableWebviewDetailsCollection", config.EnableWebviewDetailsCollection);
capabilities.AddAdditionalCapability("fullContextList", true);
}
以下是我用于上下文切换的内容
public void SwitchToContext(ViewType type)
{
Thread.Sleep(10000);
var driver = (AppiumDriver<IWebElement>)MDriver; // this is already initialized
System.Collections.ObjectModel.ReadOnlyCollection<string> allContexts = driver.Contexts; // returns 2 Context [Native_APP, WEBVIEW_packageName]
foreach (string context in allContexts)
{
Logger.Debug("Context Name: " + context);
switch (type)
{
case ViewType.Native:
if (context.Contains("NATIVE"))
{
driver.Context = context; // Native Switch works fine
}
break;
case ViewType.Webview:
if (context.Contains("WEBVIEW"))
{
driver.Context = context; // Webview switch errors out
}
break;
}
}
}
下面是我如何初始化我的驱动程序
var driver = new AndroidDriver<IWebElement>(new Uri("http://" + config.Server + ":"
+ config.Port + "/wd/hub/"), SetCapability(config), new TimeSpan(0, 3, 0));
请帮忙解决问题
你检查过所用设备的chrome版本了吗?它应该匹配 Appium 指向的 chrome 驱动程序的版本。检查此 link 以获取有关 chrome 驱动程序的更多信息,下载合适的版本后,您应该添加 chromedriverExecutable Capability
https://raw.githubusercontent.com/appium/appium-chromedriver/master/config/mapping.json
这对我有用:
AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName,
"Android");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion,
"8.1");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.AutomationName,
"UiAutomator2");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName,
"Android Device");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.App,
"D:\XXX.apk");
appiumOptions.AddAdditionalCapability("chromedriverExecutable",
"C:\Users\Downloads\chromedriver.exe");
driver = new AndroidDriver<AndroidElement>(new
Uri("http://127.0.0.1:4723/wd/hub"), appiumOptions);
我可以使用 Appium 本地服务解决这个问题
AppiumLocalService _appiumLocalService = new AppiumServiceBuilder().UsingAnyFreePort().Build();
_appiumLocalService.Start();
var appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName, "Android_Accelerated_x86_Oreo");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, "7.1");
_driver = new AndroidDriver<AppiumWebElement>(_appiumLocalService, appiumOptions);
确保在测试后处理该服务
_appiumLocalService.Dispose();
我一直被一个问题困扰很久,无法解决,急需帮助。我有一个 android apk 文件,启动该应用程序后,它会显示网站的 Webview。启动App后无法从Native App切换到Webview。
我看到 2 个上下文可用(本机和 Webview),但我无法切换到 WebView,它无法显示“消息”:“ChromeDriver 准备好进行新会话”。以下是我尝试切换时的错误消息。我在 Appium 中验证了 ChromeDriver,它与 Chromeversion 相同并且 Appium 是 v4 latest
Error in switching Context : OpenQA.Selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Did not get a valid response object. Object was: {"value":{"build":{"version":"85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689})"},"message":"ChromeDriver ready for new sessions.","os":{"arch":"x86_64","name":"Windows NT","version":"10.0.17763"},"ready":true}}
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at MobileDriver.SwithContextToWebView(AppiumDriver`1 driver, String context) in C:\Automation\Src\Common\MobileDriver.cs:line 656
以下是我想要的功能
private static AppiumOptions SetCapability(MobileDriverConfig config)
{
AppiumOptions capabilities = new AppiumOptions();
capabilities.AddAdditionalCapability("automationName", config.AutomationName);
capabilities.AddAdditionalCapability("deviceName", "Android");
capabilities.AddAdditionalCapability("platformName", config.PlatformName);
capabilities.AddAdditionalCapability("platformVersion", config.PlatformVersion);
capabilities.AddAdditionalCapability(CapabilityType.Platform, config.SystemOs);
capabilities.AddAdditionalCapability("udid", config.DeviceId);
capabilities.AddAdditionalCapability("appPackage", config.AppPackage);
capabilities.AddAdditionalCapability("appActivity", config.AppActivity);
capabilities.AddAdditionalCapability("chromeBundleId", config.ChromeBundleId);
capabilities.AddAdditionalCapability("ensureWebviewsHavePages", config.EnsureWebviewsHavePages);
capabilities.AddAdditionalCapability("webviewDevtoolsPort", config.WebviewDevtoolsPort);
capabilities.AddAdditionalCapability("chromedriverUseSystemExecutable", config.ChromedriverUseSystemExecutable);
capabilities.AddAdditionalCapability("enableWebviewDetailsCollection", config.EnableWebviewDetailsCollection);
capabilities.AddAdditionalCapability("fullContextList", true);
}
以下是我用于上下文切换的内容
public void SwitchToContext(ViewType type)
{
Thread.Sleep(10000);
var driver = (AppiumDriver<IWebElement>)MDriver; // this is already initialized
System.Collections.ObjectModel.ReadOnlyCollection<string> allContexts = driver.Contexts; // returns 2 Context [Native_APP, WEBVIEW_packageName]
foreach (string context in allContexts)
{
Logger.Debug("Context Name: " + context);
switch (type)
{
case ViewType.Native:
if (context.Contains("NATIVE"))
{
driver.Context = context; // Native Switch works fine
}
break;
case ViewType.Webview:
if (context.Contains("WEBVIEW"))
{
driver.Context = context; // Webview switch errors out
}
break;
}
}
}
下面是我如何初始化我的驱动程序
var driver = new AndroidDriver<IWebElement>(new Uri("http://" + config.Server + ":"
+ config.Port + "/wd/hub/"), SetCapability(config), new TimeSpan(0, 3, 0));
请帮忙解决问题
你检查过所用设备的chrome版本了吗?它应该匹配 Appium 指向的 chrome 驱动程序的版本。检查此 link 以获取有关 chrome 驱动程序的更多信息,下载合适的版本后,您应该添加 chromedriverExecutable Capability
https://raw.githubusercontent.com/appium/appium-chromedriver/master/config/mapping.json
这对我有用:
AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName,
"Android");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion,
"8.1");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.AutomationName,
"UiAutomator2");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName,
"Android Device");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.App,
"D:\XXX.apk");
appiumOptions.AddAdditionalCapability("chromedriverExecutable",
"C:\Users\Downloads\chromedriver.exe");
driver = new AndroidDriver<AndroidElement>(new
Uri("http://127.0.0.1:4723/wd/hub"), appiumOptions);
我可以使用 Appium 本地服务解决这个问题
AppiumLocalService _appiumLocalService = new AppiumServiceBuilder().UsingAnyFreePort().Build();
_appiumLocalService.Start();
var appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName, "Android_Accelerated_x86_Oreo");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, "7.1");
_driver = new AndroidDriver<AppiumWebElement>(_appiumLocalService, appiumOptions);
确保在测试后处理该服务
_appiumLocalService.Dispose();