0

I have tried a lot of different stuff from the google and you-tube and this is where i land and i cannot get it to work, my connection to the Appium and emulator is fine also i have checked the adb devices everything is fine.

I am getting error for the Line driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(180));

These are the two errors which i get:

"cannot convert from 'System.Uri' to 'OpenQA.Selenium.Appium.Service.AppiumServiceBuilder'"

and

"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");

      }
   }
 }

1 Answer 1

2

You are very close, but I would make a few small tweaks. Your error messages are complaining about two things -- the use of DesiredCapabilities instead of AppiumOptions, and the use of new Uri("http://127.0.0.1:4723/wd/hub") as a parameter for AndroidDriver<IWebElement>.

To solve these issues -- instead of DesiredCapabilities, I initialize my AndroidDriver with AppiumOptions. Additionally, you should try starting Appium through AppiumServiceBuilder() and use that service to start your driver session. You should also specify a parameter for automationName.

Here's what I always use to start a new mobile session on Android with 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);
Sign up to request clarification or add additional context in comments.

6 Comments

Thankyou for the quick response, I went though the documentation and skipped most of it lol i guess i should have spent more time. It works for some reason it error-ed out there is no installed nodes even though i had appium and the nodes, fixed it by doing npm global install. its working much appreciated. I will look at you git hub as well. Thanks
I went through something similar when I first started working on this -- it took several days of research, trial and error, to finally get everything working. The most helpful resource I can direct you to is the official Appium repo, with C# code samples included: github.com/appium/appium/tree/master/sample-code/csharp
I have another question, How can i seprate the test cases while using this, also do i need to pass separate driver for each test case? D I need to follow setup and tear down process?
@Newbie Following SetUp and TearDown process is cleanest way to integrate this into your test cases. If you open another StackOverflow question (so this one does not go off topic), I would be happy to assist.
not sure what happened now it went up, i guess it will take sometime to get used to here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.