UI Browser Automation using Appium with Visual Studio Android Emulator


Appium is an open source cross platform automation tool for native/hybrid and mobile web based apps.

Why?

You can use Appium with various languages like c#, java or Objective C with Selenium driver. Thus it is truly a cross platform automation tool!

How?

The machine which is running the web driver scripts communicates with the Appium server through Http/JSON and in turn Appium sends/receives the command to Devices/Emulators of Android or iOS platforms.

 

Here we will discuss about the Integration of Appium with Visual Studio using C#.

Pre Requisites:

Setting up the Visual Studio Android Emulator.

  • Search for the visual studio android emulator and open it.
  • Select the latest API level and download the emulator.
  • Run the Emulator.

Setting up the Appium Server

  • Launch Appium.exe from the downloaded folder.

  • Click on Android Icon and change the settings as highlighted.

  • Click Play button.

Getting the Device Name

Goto Android SDK folder in command prompt. And type adb devices -l. This will list all the running devices

Setting up the Visual Studio Project

Steps:

Create a new Unit Test project in Visual Studio.

Under Reference in Solution Explorer, Click Add Manage Nuget Packages.

Search for ‘Appium’ and install.

Post installation, the references will be updated.

In the .cs file, enter the code below. Initially we instantiate the Appium web driver and set the desired capabilities.


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
namespace Appium
{
    [TestClass]
    public class AppiumTest
    {
        
        AppiumDriver _driver;

        [TestMethod]
        public void TestBrowser()
        {
            //Set the capabilities
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.SetCapability("deviceName", "donatello");
            cap.SetCapability("platformVersion", "6.0.0");
            cap.SetCapability("udid", "169.254.76.233:5555");
            cap.SetCapability("fullReset", "True");
            cap.SetCapability(MobileCapabilityType.App, "Browser");
            cap.SetCapability("platformName", "Android");
            _driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), cap);

            //Navigate to browser
            _driver.Navigate().GotoUrl("http://www.bing.com");
            _driver.FindElementByName("q").SendKeys("Microsoft");
            _driver.FindElementByName("q").SendKeys(Keys.Enter);
        }
    }
}

 

The Test method above performs actions on the Device default browser. These actions are to:

Navigate to the http://www.bing.com website, then

Type the search text: ‘Microsoft’

Running the Test:

 

 

Most common Issues:

1. Unable to connect to remote server – no connection could be made because the target server actively refused it 127.0.0.1:4723

Resolution : Ensure the Appium server is running. And the IP address is entered correctly in the Appium server settings page. Verify the desired capabilities parameters.

2.  A new connection could not be created. An unknown server side error occurred while processing the command. Unknown error. Device <IP address> is not online.

Resolution:

– Verify if the emulator/device is connected through adb devices -l in command prompt.

– Desired capabilities should be properly entered. Ex: devicename/platformversion/udid/platformname.

– Check for Appium settings. Restart the Appium server.

3. Could not find a connected Android device.

Resolution:

Ensure the android device or emulator is running. (goto command prompt, goto Android SDK\Tools\ and run adb devices -l)

Comments (0)

Skip to main content