Sunday, March 27, 2016

Test Automation on Android using Appium, C# and MSTest Part 2

This is the second part of the series "Test automation on Android using Appium, C# and MSTest". If you missed the first part, Click here.

Coding Time

Now it is time to code.
Open Visual Studio Ultimate (2010 or above) (I am using VS2012)
Create a unit Test project.
Install "Appium Web Driver" and "Selenium WebDriver"  using NuGet package manager. (The easiest way)
If you don't want to use NuGet package manager, you can manually download appium dot net driver and selenium web driver c# libraries and add them to your solution
Your reference section should look like this.

Now open your UnitTest1.cs file and add following namespaces in your using section
using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
In your [TestInitialize] section, write this code.
 [TestInitialize]
    public void BeforeAll()
    {     

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.SetCapability("device", "Android");
        capabilities.SetCapability(CapabilityType.Platform, "Windows");     
        capabilities.SetCapability("deviceName", "H30-U10");
        capabilities.SetCapability("platformName", "Android");
        capabilities.SetCapability("platformVersion", "4.3");
        capabilities.SetCapability("appPackage", "com.android.calculator2");
        capabilities.SetCapability("appActivity", "com.android.calculator2.Calculator");

        driver = new  AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180));
        
       

    }
in your [TestMethod], write this code
 [TestMethod]
    public void TestCalculator()
    {

        var two = driver.FindElement(By.Name("2"));
        two.Click();
        var plus = driver.FindElement(By.Name("+"));
        plus.Click();
        var four = driver.FindElement(By.Name("4"));
        four.Click();
        var equalTo = driver.FindElement(By.Name("="));
        equalTo.Click();

      var results = driver.FindElement(By.ClassName("android.widget.EditText"));

        Assert.AreEqual("6", results.Text);
    }
The complete code should look like this.
using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;


namespace AppiumSample
{
    [TestClass]
    public class UnitTest1
    {
        public AndroidDriver driver;

    [TestInitialize]
    public void BeforeAll()
    {


        

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.SetCapability("device", "Android");
        capabilities.SetCapability(CapabilityType.Platform, "Windows");        
        capabilities.SetCapability("deviceName", "H30-U10");
        capabilities.SetCapability("platformName", "Android");
        capabilities.SetCapability("platformVersion", "4.3");
        capabilities.SetCapability("appPackage", "com.android.calculator2");
        capabilities.SetCapability("appActivity", "com.android.calculator2.Calculator");
        
        driver = new  AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180));
        
       

    }

    [TestCleanup]
    public void AfterAll()
    {
      driver.Quit(); 
    }
 

    [TestMethod]
    public void TestCalculator()
    {

        var two = driver.FindElement(By.Name("2"));
        two.Click();
        var plus = driver.FindElement(By.Name("+"));
        plus.Click();
        var four = driver.FindElement(By.Name("4"));
        four.Click();
        var equalTo = driver.FindElement(By.Name("="));
        equalTo.Click();

        var results = driver.FindElement(By.ClassName("android.widget.EditText"));

        Assert.AreEqual("6", results.Text);
    }
To run this test case, click on Test Menu > Windows > Test Explorer. A window will open on your left and it will list down the test cases. Select your desired test case and click run.


Now watch your android phone, A calculator window will open on your android phone, button 2 will be tapped, then button + , then button 4 and then button =.
After that result will show 6 and your test case should ideally be passed. Make sure your Appium server is running. Appium window will show you all the logs.
Congratulations, you have just written your first automated test case in Appium and it is running on a real android device.

Some Explanation

Most of the code is self explanatory. But I want your attention on these two lines.
  capabilities.SetCapability("appPackage", "com.android.calculator2");
        capabilities.SetCapability("appActivity", "com.android.calculator2.Calculator");

For every application you want to test, you must know its package name and app activity name. So to know about these attributes, you need to download a little android app on your phone. It is called apkInfo. It will show you the package name and activityname of any android app installed on your phone. Just pass these parameters here and that app will launch on your phone by automation code.

Conclusion.

If you are familiar with Selenium WebDriver, coding in Appium is not so different. Only problem was the configuration. If you have done this right, you should be able start writing scripts for your android. I tried to explain this as simply as possible. If you still find problems in setting up the environment, you can ask questions in comments

4 comments:

  1. Hello Saad,

    I tried following all the steps you mentioned in both articles but i'm facing one issue.

    Issue: > error: Logcat capture failed: spawn "C:\Android\adb.exe" ENOENT
    I tried verifying my path but no luck all looks good , can you please help me?

    > info: [debug] The following desired capabilities were provided, but not recognized by appium. They will be passed on to any other services running on this server. : device, platform
    > info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device
    > info: [debug] Creating new appium session b3b8bed8-28de-4403-9da5-33b1c3b3f35d
    > info: Starting android appium
    > info: [debug] Getting Java version
    > info: Java version is: 1.8.0_45
    > info: [debug] Checking whether adb is present
    > warn: The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.
    > info: [debug] executing cmd: where adb
    > info: [debug] Using adb from C:\Android\adb.exe
    >
    > warn: No app capability, can't parse package/activity
    > info: [debug] Using fast reset? true
    > info: [debug] Preparing device for session
    > info: [debug] Not checking whether app is present since we are assuming it's already on the device
    > info: Retrieving device
    > info: [debug] Trying to find a connected android device
    > info: [debug] Getting connected devices...
    > info: [debug] executing cmd: "C:\Android\adb.exe" devices
    > info: [debug] 1 device(s) connected
    > info: Found device ba1fb281
    > info: [debug] Setting device id to ba1fb281
    > info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
    > info: [debug] executing cmd: "C:\Android\adb.exe" -s ba1fb281 wait-for-device
    > info: [debug] executing cmd: "C:\Android\adb.exe" -s ba1fb281 shell "echo 'ready'"
    > info: [debug] Starting logcat capture
    > error: Logcat capture failed: spawn "C:\Android\adb.exe" ENOENT
    > info: [debug] Stopping logcat capture
    > info: <-- POST /wd/hub/session - - ms - -
    > undefined

    ReplyDelete
  2. Your app success depends on its uniqueness, intuitive design, and rich set of features and so on.
    http://www.showboxdownloadsapp.com/

    ReplyDelete

  3. When it comes to gaming consoles, there is always a big fight between Sony's Playstation and Microsoft's XBox. Now this rivalry is going to increase even more because Microsoft is getting ready to launch it's next console, the Xbox 2. You can check out all the details from my site here Xbox 2

    ReplyDelete