ad

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

51 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
  4. Can you please give example of testing Hybrid app using Appium C#.

    ReplyDelete
  5. Test Automation on Android using Appium, C# My sincere thanks for sharing this post Please Continue to share this post
    Software Testing Training in Chennai

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

      Delete
  6. nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    softwaretesting training in chennai

    ReplyDelete
  7. Great content thanks for sharing this informative blog which provided me technical information keep posting. Software Testing Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  8. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.I really like this topic.
    Python Online Training
    Learn Python Online

    ReplyDelete
  9. nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge about this. so keep on sharing such kind of an interesting blogs.
    python training in marathahalli

    ReplyDelete
  10. nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    selenium training in chennai

    ReplyDelete
  11. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.
    Web Designing Training Institute in Chennai | DotNet Training Institute in Chennai | Java Training Institute in Chennai

    ReplyDelete
  12. Thanks a lot for sharing this with all of us, I like it and we can communicate. Do you need buy app ratings and reviews. To boost app ranking and double app downloads now.

    ReplyDelete
  13. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Android Project Center in Chennai | Android Project Center in Velachery

    ReplyDelete
  14. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    java training in bangalore
    java training in bangalore

    ReplyDelete
  15. Thanks for your informative article. Your post helped me to understand the future and career prospects. Keep on updating your blog with such awesome article.
    ME/M.Tech Project Center in Chennai | ME/M.Tech Project Center in Velachery

    ReplyDelete
  16. Thank you for this great post! It has long been incredibly beneficial. I wish which you will carry on posting your information with us. @ Orthopedic Doctors in Visakhapatnam

    ReplyDelete
  17. Google makes it easier for developers to market their apps by having Android Market pre-installed on most Android devices and allows people to choose from apps published by many different developers.Download ShowBox APK

    ReplyDelete
  18. The information you share is very useful. Thank you for the valuable information.
    My Website

    ReplyDelete
  19. GBWhatsapp apk Download Latest Version 2018. Download Latest GB Whatsapp for Use 2 Whatsapp in One Mobile.  GBWhatsApp APK

    ReplyDelete
  20. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    Java Training in Chennai
    Java course in Chennai
    Hadoop Training in Chennai
    Python Training in Chennai
    Java Training in Porur
    Java Training in Adyar
    Java Training in Tnagar

    ReplyDelete
  21. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign
    apk download

    ReplyDelete
  22. The free application always provides the service of navigation, community, weather, music and so on. The service of document and the hardware management should be paid.gerald winata gozali

    ReplyDelete
  23. A befuddling web diary I visit this blog, it's incredibly grand. Strangely, in this present blog's substance made motivation behind fact and sensible. The substance of information is instructive
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  24. GBWhatsApp has group video calling feature and many privacy options, With GB WhatsApp you can use two applications of WhatsApp Plus in same android device. https://whatsopp.net/download-gbwhatsapp-apk-latest-version/

    ReplyDelete
  25. Use dual account with GBwhatsapp Latest Application With free of cost. Gbwhatsapp Apk

    ReplyDelete
  26. Good information. Mathway App is the best App to have for simple to complex math solutions.

    ReplyDelete
  27. Nice thanks for share, download latest VidMate

    ReplyDelete
  28. thank you for this sharing
    Check out the best home office desks nz

    ReplyDelete
  29. GBWhatsapp is one of the best modded version of official Whatsapp with latest features.

    Gbwhatsapp

    ReplyDelete
  30. outsourcingall.com Most Poplar Free Porn Training Center largest The coolest Free Porn Videos & Sex Movies Updated Daily. Update is a tube porn site with millions Online and offline real life porn cam

    ReplyDelete
  31. Nice thanks for share, download latest Snaptube Apk

    ReplyDelete
  32. Nice thanks for share, download latest lucky patcher is one of the most popular android tool must check lucky patcher apk download

    ReplyDelete
  33. We all know about the app GBWhatsApp which is the most popular among all mods of whatsapp. It is unofficial also but there is a lot of features in this app that made people crazy. Today with the new technology people want advance technology in modern era that’s why people attracted more with mods of apps then official. As we know that there are many mods of whatsapp available but GBWhatsApp is the best in all of these.for more Updates Visit Our GBWhatsApp APK

    ReplyDelete
  34. Free Random Live Video Streaming App Video chat: is ideal for one-to-one conversations that you can meet and match fun and interesting single all over the world in seconds.

    ReplyDelete