Android 使用 Appium Selenium C# 进行移动测试
Android Mobile testing with Appium Selenium C#
我已经尝试了很多来自 google 和 you-tube 的不同东西,这是我登陆的地方,我无法让它工作,我与 Appium 和模拟器的连接也很好我有检查 adb 设备一切正常。
我收到第 driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(180));
行的错误
这是我得到的两个错误:
"cannot convert from 'System.Uri' to 'OpenQA.Selenium.Appium.Service.AppiumServiceBuilder'"
和
"cannot convert from 'OpenQA.Selenium.Remote.DesiredCapabilities' to 'OpenQA.Selenium.DriverOptions".
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
namespace UnitTestProject4
{
[TestClass]
public class UnitTest1
{
AppiumDriver<IWebElement> driver;
[TestMethod]
public void TestMethod1()
{
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("deviceName", "Pixel 3a Pie 9.0 - API 28");
cap.SetCapability("platformVersion", "9.0");
cap.SetCapability("udid", "emulator-5554");
cap.SetCapability("appPackage", "org.mozilla.firefox");
cap.SetCapability("appActivity", "org.mozilla.gecko.BrowserApp");
cap.SetCapability("platformName", "Android");
driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(180));
driver.Navigate().GoToUrl("https://www.google.com");
}
}
}
你非常接近,但我会做一些小的调整。您的错误消息抱怨两件事——使用 DesiredCapabilities
而不是 AppiumOptions
,以及使用 new Uri("http://127.0.0.1:4723/wd/hub")
作为 AndroidDriver<IWebElement>
.
的参数
为了解决这些问题——我用 AppiumOptions
初始化 AndroidDriver
而不是 DesiredCapabilities
。此外,您应该尝试通过 AppiumServiceBuilder()
启动 Appium 并使用该服务启动您的驱动程序会话。您还应该为 automationName
指定一个参数。
以下是我在 Android 上使用 C# 启动新移动会话时经常使用的方法:
// start appium service
var builder = new AppiumServiceBuilder();
var appiumLocalService = builder.UsingAnyFreePort().Build();
appiumLocalService.Start();
// create appium driver capabilities
var options = new AppiumOptions { PlatformName = "Android" };
options.AddAdditionalCapability("deviceName", "Pixel 3a Pie 9.0 - API 28");
// add app or appPackage / appActivity depending on preference
options.AddAdditionalCapability("appPackage", "org.mozilla.firefox");
options.AddAdditionalCapability("appActivity", "org.mozilla.gecko.BrowserApp");
options.AddAdditionalCapability("udid", "emulator-5554");
options.AddAdditionalCapability("automationName", "UiAutomator2"); // this one is important
// these are optional, but I find them to be helpful -- see DesiredCapabilities Appium docs to learn more
options.AddAdditionalCapability("autoGrantPermissions", true);
options.AddAdditionalCapability("allowSessionOverride", true);
// start the driver
var driver = new AndroidDriver<IWebElement>(appiumLocalService.ServiceUrl, options);
我已经尝试了很多来自 google 和 you-tube 的不同东西,这是我登陆的地方,我无法让它工作,我与 Appium 和模拟器的连接也很好我有检查 adb 设备一切正常。
我收到第 driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(180));
这是我得到的两个错误:
"cannot convert from 'System.Uri' to 'OpenQA.Selenium.Appium.Service.AppiumServiceBuilder'"
和
"cannot convert from 'OpenQA.Selenium.Remote.DesiredCapabilities' to 'OpenQA.Selenium.DriverOptions".
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
namespace UnitTestProject4
{
[TestClass]
public class UnitTest1
{
AppiumDriver<IWebElement> driver;
[TestMethod]
public void TestMethod1()
{
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("deviceName", "Pixel 3a Pie 9.0 - API 28");
cap.SetCapability("platformVersion", "9.0");
cap.SetCapability("udid", "emulator-5554");
cap.SetCapability("appPackage", "org.mozilla.firefox");
cap.SetCapability("appActivity", "org.mozilla.gecko.BrowserApp");
cap.SetCapability("platformName", "Android");
driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(180));
driver.Navigate().GoToUrl("https://www.google.com");
}
}
}
你非常接近,但我会做一些小的调整。您的错误消息抱怨两件事——使用 DesiredCapabilities
而不是 AppiumOptions
,以及使用 new Uri("http://127.0.0.1:4723/wd/hub")
作为 AndroidDriver<IWebElement>
.
为了解决这些问题——我用 AppiumOptions
初始化 AndroidDriver
而不是 DesiredCapabilities
。此外,您应该尝试通过 AppiumServiceBuilder()
启动 Appium 并使用该服务启动您的驱动程序会话。您还应该为 automationName
指定一个参数。
以下是我在 Android 上使用 C# 启动新移动会话时经常使用的方法:
// start appium service
var builder = new AppiumServiceBuilder();
var appiumLocalService = builder.UsingAnyFreePort().Build();
appiumLocalService.Start();
// create appium driver capabilities
var options = new AppiumOptions { PlatformName = "Android" };
options.AddAdditionalCapability("deviceName", "Pixel 3a Pie 9.0 - API 28");
// add app or appPackage / appActivity depending on preference
options.AddAdditionalCapability("appPackage", "org.mozilla.firefox");
options.AddAdditionalCapability("appActivity", "org.mozilla.gecko.BrowserApp");
options.AddAdditionalCapability("udid", "emulator-5554");
options.AddAdditionalCapability("automationName", "UiAutomator2"); // this one is important
// these are optional, but I find them to be helpful -- see DesiredCapabilities Appium docs to learn more
options.AddAdditionalCapability("autoGrantPermissions", true);
options.AddAdditionalCapability("allowSessionOverride", true);
// start the driver
var driver = new AndroidDriver<IWebElement>(appiumLocalService.ServiceUrl, options);