DesiredCapabilities for Selenium native android ap

2019-07-01 17:58发布

问题:

I am trying to set up a selenium framework to test a application on an android emulator. However I keep getting a warning that DesiredCapabilities is obsolete. I cant find any information on this apart from that relation to browsers. But in my framework I am trying to test a native application.

Code:

    [TestMethod]
    public void Test_Open_Mobile_App()
    {
        DesiredCapabilities cap = new DesiredCapabilities();

        //set the emulator
        cap.SetCapability("platformVersion", "8.1");
        cap.SetCapability("platformName", "Android");
        cap.SetCapability("deviceName", "Device 01 Oreo_1440x2560");
        cap.SetCapability("udid", "emulator-5554");
        cap.SetCapability("deviceType", "phone");
        cap.SetCapability("device", "Android");
        cap.SetCapability("app", "C:/Users/User/Desktop/Mobile Tests/App APK/com.hyperspheric.go4schools.apk");
        cap.SetCapability("appPackage", "appPackage");
        cap.SetCapability("appActivity", "appActivity");


        _driver = new AndroidDriver<AndroidElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap);
    }

回答1:

The .NET bindings are moving toward a pattern where DesiredCapabilites should not be used directly. To facilitate that, the ChromeOptions class has a ToCapabilities() method.

EDIT:

This should work fine:

ChromeOptions options = new ChromeOptions()
DesiredCaps = options.ToCapabilities() as DesiredCapabilities;
DesiredCaps.SetCapability("platformName","Android");
DesiredCaps.SetCapability("platformVersion", "8.1");
DesiredCaps.SetCapability("deviceName", "Device 01 Oreo_1440x2560");
DesiredCaps.SetCapability("device", "Android");

AppDriver = new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), DesiredCaps, TimeSpan.FromSeconds(300));


回答2:

As of now, the developpers of Appium ( who provide the AnroidDriver class ) haven't implemented a solution bypassing the use of the DesiredCapabilities class. ( I couldn't find any related issue or pull request )

As refered in this selenium issue