Search code examples
c#androidseleniumtestingappium

How to initialze an appium android driver on version 4.0.0 with selenium 3.141.0


I can't find any documentation outlining how to initialize an appium android driver.

I had this test somewhat working with appium 3.0.0.2 and selenium 3.11 but then received the following error when trying to find an element by class name:

OpenQA.Selenium.InvalidSelectorException: 'Locator Strategy 'css selector' is not supported for this session

Ideally I don't want to go back to an older version of selenium as I already have a set of working tests using 3.14. This is my test at the moment, and I cant find clear documentation telling me how to implement an appium driver in C#.

  public void androidTest(){
      AndroidDriver<AndroidElement> driver;
      DesiredCapabilities cap = new DesiredCapabilities();
      cap.SetCapability("deviceName", "myName");
      cap.SetCapability("platformName", "Android");
      cap.SetCapability("automationName", "UiAutomator2");
      cap.SetCapability("appPackage", "myPackage");
      cap.SetCapability("appActivity", "myActivity");

      Uri url = new Uri("http://127.0.0.1:4723/wd/hub");

      driver = new AndroidDriver<IWebElement>(url, cap);

      driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

      driver.FindElementByName("android.widget.EditText").SendKeys("testString");
   }

I get the following two error messages

Argument 1:

cannot convert from 'System.Uri' to 'OpenQA.Selenium.Remote.ICommandExecutor'

Argument 2:

cannot convert from 'OpenQA.Selenium.Remote.DesiredCapabilities' to 'OpenQA.Selenium.DriverOptions' ArenaTests

What arguments do I need to pass into this constructor to get this setup working?


Solution

  • I worked this out. If anyone runs into this, the following initialisation works for me.

    public void androidTest(){
        AndroidDriver<AndroidElement> driver;
        AppiumOptions options = new AppiumOptions();
        options.PlatformName = "Android";
        options.AddAdditionalCapability("deviceName", "MyDevice");
        options.AddAdditionalCapability("platformVersion", "PlatformV");
        options.AddAdditionalCapability("automationName", "UiAutomator2");
        options.AddAdditionalCapability("appPackage", "MyPackage");
        options.AddAdditionalCapability("appActivity", "MyActivity");
    
        Uri url = new Uri("http://127.0.0.1:4723/wd/hub");
    
        driver = new AndroidDriver<AndroidElement>(url, options);
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    
        // Some example selectors
        driver.FindElementByClassName("android.widget.EditText").SendKeys("test");
        driver.FindElement(MobileBy.AndroidUIAutomator("new UiSelector().className(\"android.widget.EditText\").instance(1)")).SendKeys(Username);
    }