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

Test Automation on Android using Appium, C# and MSTest

This is a two part series of test automation on android using Appium, C# and MSTest.
for part 2 click here.

Prerequisites for this tutorial:

  1. Visual Studio Ultimate (2010 or above) (Because MSTest is present in that)
  2. Android SDK For Windows (Download Link) (Website link)
  3. Appium For Windows (Download Link) (Website Link)
  4. A Real Android Device running Android 4.2 or Above (I am using an Android Phone running Android 4.3)
  5. A USB Cable to attach your Android Phone to Your PC.
  6. ADB Interface Drivers for your Phone (Link on how to get that ) (very important Step)

Some Configurations for Android SDK and Appium:

  • When you have installed Android SDK, go to My Computer, right click , click Properties, click Advanced System Settings, click Environment Variables.
  • Create a new User Variable with the name "ANDROID_HOME". Give path to your sdk folder in the value. The default path is  C:Program Files (x86)Androidandroid-sdk

  • Edit the PATH variable in "System Variable" Section. Append the path to your tools folder and platform-tools folder. Separated with ";"
The paths are
C:Program Files (x86)Androidandroid-sdktools
C:Program Files (x86)Androidandroid-sdkplatform-tools
See the image below.

  •  Connect your Android Phone with USB Cable. To make sure your android phone is connected with your PC, we have to do following.
Go to C:Program Files (x86)Androidandroid-sdktools. Click on "uiautomatorviewer.bat". A window will open. See Image.



 You can use this window to inspect the elements of your app in android. Open the calculator app in your android device and click on "Device ScreenShot" button on this screen. If you receive this error message "No Android devices were found by adb"

That means adb interface drivers are not installed on your system. You have to read again point number 6 in the Prerequisites section. If android device is successfully connected, you should see a snapshot along with Object Map in this window like this.

  • Unzip AppiumforWindows.zip in a folder. Open Appium.exe, you should see a window like this.


Click on the android Icon on the top left of this window. You will see a window in which you can configure the platform and version of your android on which you want to test. I have filled the following configurations there.



Now click on the Play button on the top right corner of the window. Appium server will start with the configurations you have provided.


  • Developer options should be enabled on your android phone, with these two options.
Usb Debugging should be enabled.
StayAwake should be enabled.
If you reach till here, Congratulations, You have successfully configured all the required prerequisites .
In the second part, I will show how to code and run this script on your real android device.


JMeter Tutorials: All Resources

This post is a collection of JMeter Tutorials from the web.
JMeter is a leading open source load testing tool which is widely used across the globe.
When I first tried to use JMeter, I feel that it involves a learning curve. The resources and tutorials are abundant on the internet but scattered.
That is where I decided to create a JMeter Tutorials resource list for newbies which they can go through. After the success of Selenium in Java resource list, It only feels logical that such resource lists are needed very much.
Below are some very useful links for JMeter Tutorials. Please go through them and bookmark those which you find beneficial.

JMeter Tutorials and Resources

1.  JMeter Expert Blog

This blog contains only 4 to 5 posts related to JMeter. But these posts are so beautifully designed especially the conceptual post about How Apache JMeter simulates multiple users that they have nailed it. They have cleared many concepts with this single post using simple diagrams. I urge everyone to read this post first before going on to detailed JMeter tutorials.

2. JMeter Articles on Guru99

Next up is my favourite Guru :)
Guru99.com was also featured in my Selenium Resource List.  They are again featured on this list. Thier tutorials are simple and easy to understand as always. They have covered extensively from Introduction to Jmeter to JMeter interview questions. These will give you a start.

3. The Ultimate JMeter Resource List on Blazemeter

I am creating a resource list and it turns out people have already created their own resource lists before me :)
BlazeMeter is a commercial, self-service load testing platform-as-a-service (PaaS), which is fully compatible with Apache JMeter. BlazeMeter provides an enterprise grade, ‘out-of-the-box’ load testing solution for the developer community. Although the company is commercial, they have created this free resource list for the community.
They have many great links on that resource list but the highlight is their free 5-Day JMeter Course. You have to register to get these videos delivered to your inbox.

4. JMeter Resources On Github

If a commercial company is giving away free resources on an open source tool, do you think that open source community will be behind in sharing their resource list? You are wrong.
See this list on Github, it is created by Aliaksandr Belik and it is quickly grown into a mighty resource list covering everything from Books to Blogs and Tutorials to Tips and Tricks. A must have bookmark for every JMeter enthusiast out there.

5.  JMeter @ tutorialspoint.com

When you search JMeter Tutorials on Google, this site appears on the very first page. Why is that? Take a look yourself. :)

6. JMeter Official Manual

The most comprehensive Manual for JMeter created by the creators of JMeter. It is comprehensive but I would suggest to only look at this after going through the above 5 links. Then it will make sense to you. Don't forget to check the JMeter plugins page. These plugins make JMeter much more powerful.

7. JMeter Videos  on Executeautomation.com by Karthik KK

Free articles and videos on JMeter. This website has tutorials for many other automation tools as well. Karthik works really hard in setting up this website and constantly post videos on that on new tools. This blog crossed 1.4 Million views as of now.

8. JMeter on Art Of Testing.com

The good thing about these tutorials that they start from basics of performance testing. I found them simple and easy to understand. I hope you conclude the same after visiting this link.

9. JMeter Articles by Shantonu Sarker

This guy has written more than 50 posts on JMeter and still more articles are coming soon. He knows what he writes. This link is the collection of the posts he has written on JMeter. He also writes about Selenium, Design Patterns and other stuff on this blog. Great collection of articles.

10. Learn JMETER from Scratch -(Performance + Load) Testing Tool on Udemy

Last but not the least. A complete JMeter course on Udemy is also present. Which is not free of course. But if you are willing to pay some bucks you will get all things at one place along with help from a teacher.  Currently this course holds a 4.3 rating. Read the reviews and decide for yourself.

This list is obviously incomplete without your contribution. Please send me links to blogs/websites which you found useful while learning about JMeter and I will include them here.
Thanks for reading :) Waiting for your feedback and comments.

Selenium in Java: All Resources.

For all those who want to learn Selenium in Java, I have searched and compiled all those resources which I personally found useful. Please go through these links and bookmark those which you find beneficial and useful.

Selenium in Java Free Resources:


Selenium Training in Java on Udemy by Raman Arora
A picture is worth a thousand words and I think a video is worth ten thousand words. This free course by Raman Arora is especially very useful because he focuses on Java basics first. Then he moves towards Selenium Concepts, the basic difference between Selenium IDE, RC and WebDriver. He also touched concepts about Selenium Grid, SVN and DataDriven Framework. Nice course for those who are unfamiliar with Java and wants  to brush up their skills first in Java.


Selenium with Java Tutorials on ToolSQA.com by LAKSHAY SHARMA
A beautifully designed Website with carefully crafted tutorials. ToolSQA is not limited to Selenium Tutorials only. It has extensive tutorials on other automation tools as well such as Cucumber, SpecFlow and Appium etc. The good thing about this website is that tutorials are arranged in an order so that a newbie always has a clue as to start from where and where this will lead to. A great website for automation enthusiasts.


Selenium Tutorials on Guru99.com
Amazing tutorials with great illustrations. Their vision is Fun & Free Education for ALL. Like ToolSQA, it has tutorials for other automation tools as well. Another good thing about this website it that it has links to live projects where you can brush up your selenium skills by practicing on them.


Selenium Article Series on SoftwareTestingHelp.com
33 articles on this topic and counting. SoftwareTestinghelp.com is a household name for software testers in India and Pakistan. These tutorials are comprehensive and enough for you to get started on the Selenium Journey. Shruti Shrivastava,  Amaresh Dhal, and Pallavi Sharma have created this tutorial for all of you. They are also offering live training which is paid.


Tutorials on SeleniumSimplified.com
A website developed by Alan Richardson. The author of the book Selenium Simplified and a Trainer of Selenium. This website contains great articles on Selenium with Java. The highlight of this website is the "speedruns", which is basically a checklist or a quick through to configure the environment for Selenium.

Selenium in Java Other Notable Mentions:


Software testing tutorials and automation
A bit scattered but contains many useful articles.  This blog covers from java basics to Advanced Selenium Concepts. Design is not great but information is pretty useful.

Selenium By Charan
This blog covers some advanced scenarios for selenium webdriver like integration with SauceLabs and Keyword Driven Framework.

Selenium in Java Paid Resources:

If you are willing to pay some money for your learning, I cannot force you enough to take this course online on udemy.com by Alan Richardson.

Selenium 2 WebDriver Basics With Java

This course has 4.9 rating on Udemy. and it only cost $299. Sometimes udemy.com announces discounts on courses, you can wait for that as well.
The benefit is that it will cover almost all the topics under one roof.

Live Training on ToolSQA.com 

The link contains the batch start date, course description, and fee.  You can see the testimonials to confirm the course quality :)

Live Training Sessions on SoftwareTestingHelp.com

Another live course on SoftwareTestingHelp.com. This link contains their training schedule and fee structure. All video lectures are recorded and sent to you afterwards in case if you miss any class.

Selenium Recipes in Java (Ebook) by Zhimin Zhan. 

The Selenium WebDriver Recipes book is a quick problem-solving guide to automated testing web applications with Selenium WebDriver. It contains hundreds of solutions to real-world problems, with clear explanations and ready-to-run test scripts you can use in your own projects. You can also download free sample to see what it contains.
There are also many books present on Amazon.com,  LeanPub.com and packtpub.com. Since I have not read many of those, I really like some feedback through comments so that I can include some more names in the list.
If you own a blog and you write about this topic "Selenium in Java" and you want your blog to be included in this list, please email me on joinsaad@gmail.com.
Update: (I will constantly update other blogs/websites in this section as I receive Emails or comments from their authors.)
  1.  ITeLearn by Karthik Kosireddi    (Paid Live training with some Free Videos of Selenium. Also contains live projects)
  2.  Selenium Easy  (Contains Articles for Selenium as well as Protractor, Jenkins, Maven etc)
I hope this list is beneficial for all of you. Please send your feedback. Thanks for reading.

Selenium WebDriver: Tools to identify Locators easily



When automating a web application, the most important and time taking task is to come up with unique locators for the elements present on the web page. Once you find out the correct locator, the rest of the automation is relatively easier.
Finding correct and unique locators require human intelligence and below tools can help you in a great deal when you start creating your scripts. Every automation engineer should have these tools in their arsenal.

List of Tools to identify Locators

Firebug  (Firefox addon)
The favourite tool of web developers. Firebug integrates with Firefox to put a wealth of web development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page. Its console window and inspection tool are the best available in the market.

Firefinder for Firebug
This tool is simple, provide any XPath or CSS selector and it will highlight the matching elements on the web. Simple, lightweight and easy to use. Install it on top of Firebug.

FirePath for Firebug
Right Click on any element and select "Inspect in Firepath", this addon will generate the XPath or CSS selector for you. It also highlights the element on the web page and in the DOM. Very useful. Again install it on top of Firebug.

WebDriver Element Locator (Firefox addon)
This addon will generate XPath for any given element. But the best thing is, it will also generate WebDriver statements for you and also in the language of your choice. Currently, it supports C#, Java, Python and Ruby.  Just right-click on any element and generate the code in your favourite language. It is that simple. The only downside is that it only generates XPath.

Selector Detector (Bookmarklet)
Sometimes XPath and CSS Selectors do not work in a given situation. That's where JQuery selector comes in handy. This is a bookmarklet created by Jessie A. Morris and it will generate the jQuery selector for your on the clicked element. Go to this link and drag the provided bookmarklet to your bookmarks bar. The button will appear with the name Selector Detector. Now click on it and Click on any element and it will generate the jQuery selector for you.

SelectorGadget (Bookmarklet and Chrome Extension)
This tool is created by Andrew Cantino and Kyle Maxwell. It can generate CSS Selectors for you. Authors describe the method to generate CSS selector like this.
Click on a page element that you would like your selector to match (it will turn green). SelectorGadget will then generate a minimal CSS selector for that element, and will highlight (yellow) everything that is matched by the selector. Now click on a highlighted element to remove it from the selector (red), or click on an unhighlighted element to add it to the selector. Through this process of selection and rejection, SelectorGadget helps you come up with the Perfect CSS selector for your needs.
See the video on the provided link to better understand this tool.

SWD Page Recorder (.Net Application)
This is a comprehensive tool and it is more than just a generator. It is developed by Dmitry Zhariy. Using this tool, you can generate selectors as well as full classes for your code in Selenium WebDriver. It allows you to generate Page Object Models for your automation code. If you are a C# developer, this tool will save you ample amount of time. The best thing about this tool that you can write C# code in this tool and see the live results on the browser. That is amazing.

Looking Glass (Java Application)
Although not as comprehensive as SWD Page Recorder, This tool is still useful in finding elements. It also allows you to write groovy scripts and see the results live on the browser. Good for Java developers.

These were some of the tools to identify locators easily. I hope the above list helps the automation engineers in saving their time to identify and verify element locators. I am also working on an application inspired by Looking Glass and SWD page recorder. It will allow you to verify your locators and generate C# classes for you. I am calling it "SeleniumVerify". It is present on GitHub. Currently it is under development and I will soon update you when it is ready for download.
Do you happen to know any other tools. Please share in comment and also give your feedback on this article.

Selenium WebDriver in C#: How to use the existing window of Chrome Browser.

Selenium WebDriver with all its advantages has some pain points. One of them is to reuse the existing window of your opened browser so that you can continue your test cases from where they are failed. Unfortunately there is no direct method to use the browser window. You have to be a little creative to come up with a solution.
Luckily, after spending hours of internet search and trial and error, I am finally able to devise a solution for this problem.
I have also posted a solution about re-using the existing window of Firefox here.
This solution is specifically for chrome browser. I have also attached the source code.
The steps are as follows.
  1. Create a class which is derived from RemoteWebDriver. We name this class as CustomRemoteDriver.cs.
  2. Override the constructor and Execute() method in the new class like this .
 public class CustomRemoteWebDriver : RemoteWebDriver
    {
        public static bool newSession = false;
        public static string capPath = @"c:\automation\sessionCap";
        public static string sessiondIdPath = @"c:\automation\sessionid";

        public CustomRemoteWebDriver(Uri remoteAddress, DesiredCapabilities desiredCapabilities)
            : base(remoteAddress, desiredCapabilities)
        {


        }




        /// 
        /// Store for the name property.
        /// 
        /// A  value representing the command to execute.
        /// A  containing the names and values of the parameters of the command.
        /// 
        /// A  containing information about the success or failure of the command and any data returned by the command.
        /// 
        protected override Response Execute(string driverCommandToExecute, Dictionary < string, object > parameters)
        {
            if (driverCommandToExecute == DriverCommand.NewSession)
            {
                if (!newSession)
                {
                   
                    var sidText = File.ReadAllText(sessiondIdPath);

                  
                    return new Response
                    {
                        SessionId = sidText,
                        
                    };
                }
                else
                {
                    var response = base.Execute(driverCommandToExecute, parameters);
                  
                    File.WriteAllText(sessiondIdPath, response.SessionId);
                    return response;
                }
            }
            else
            {
                var response = base.Execute(driverCommandToExecute, parameters);
                return response;
            }
        }
    }
This above code is just basically saving the session id in a file when new session is created and in case this is not a new session, it is reading the session id  from the saved file and returning the response based on this session id.
  1. In your [TestInitialize] method, initialize your CustomRemoteWebDriver. Start the chromedriver with Process.Start(). For every test case, check if process is already started. If yes, then Attach the custom remote webdriver to this process, otherwise start the new process. Like this.
  [TestInitialize]
        public void MyTestInitialize()
        {
            if (ConfigurationManager.AppSettings["UseChromeDebugging"] == "true")
            {

                var pr = Process.GetProcessesByName("chromedriver");
                if (pr.Length > 0)
                {
                    CustomRemoteWebDriver.newSession = false;
                    var driver = new CustomRemoteWebDriver(new Uri("http://localhost:9515"), DesiredCapabilities.Chrome());
                    WebDriver = driver;
                }

                else
                {
                    Process.Start(ConfigurationManager.AppSettings["ChromeDriverPath"]);
                    CustomRemoteWebDriver.newSession = true;
                    var driver = new CustomRemoteWebDriver(new Uri("http://localhost:9515"), DesiredCapabilities.Chrome());
                    WebDriver = driver;

                }
            }

            else
            {
                //normal execution
                WebDriver = new ChromeDriver(DRIVER_PATH);                
            }
            
            
        }
  • You are good to go. Now create two test methods. In the first method navigate to google.com and in the second method, search something in google search box.
  • Build the solution, in the test view window, you will be able to see two test methods
  • Execute the first method, chrome driver command prompt window will open,  a chrome window will open and it will navigate to google.com. Test case will be passed.
  • Now the real magic will happen, Execute the second method, It will use the existing window of chrome, No new window will open and google will search the given text.

Full source code will be attached soon. Please do give your feedback. I hope I can make lives of some test automation engineers easier with this approach :)

Selenium WebDriver : Cross Browser Testing

One of the great features of Selenium WebDriver is Cross browser testing. It allows you to create single test case which can execute across different browsers. That means you write one test case to test one functionality and you can run this test case on all of your supported browsers.
Selenium supports Firefox, Chrome, IE, Opera and Safari. It also supports headless browsers like HTML Unit Driver and PhantomJS.
This article will tell the approach to write scripts in such a way that is supports execution on IE, Chrome and FireFox. You can of course further enhance it to fit your needs.
See the code below.
class MainCall
{

public IWebDriver WebDriver = null;
   
   public static void InitializeDriver(string BrowserName)
        {
          if (WebDriver == null)
                {
                                      
                    string DRIVER_PATH = @"C:automationdriversFolder";

                    switch (BrowserName)
                    {
                        case "IE":
                          
                            WebDriver = new InternetExplorerDriver(DRIVER_PATH);                         
                           
                            break;

                        case "FF": 

                            WebDriver = new FirefoxDriver();
                            break;


                        case "CR":
                                                      
                            WebDriver = new ChromeDriver(DRIVER_PATH);
                            break;

                        default:
                            WebDriver = new FirefoxDriver();                           
                            break;

                    }

                }
            
        }
}
The code is self explanatory. You will pass the browser name and it will give you the instance of that particular browser.
To use the above method, you can call it like this in your [TestInitialize] method
[TestInitialize()]
        public void MyTestInitialize()
        {     
    //get the browsername from your app.config file
    string browserName = ConfigurationManager.AppSettings["BrowserName"];     
              
            if(MainCall.WebDriver == null)
                MainCall.InitializeDriver(browserName);
          
            
        }
That's it. Before executing any script, you just have to change the browser name in you app.config file. Your tests will will start executing on that particular browser.
See this sample test method.
 [TestMethod]
        public void Login()
        {           
            MainCall.WebDriver.Manage().Window.Maximize();
            MainCall.WebDriver.Navigate().GoToUrl(URL);           
            MainCall.LoginPage.FillUserName(id);
            MainCall.LoginPage.FillPassword(password);
            MainCall.LoginPage.ClickSignInButton();
        }
This test case can run all three browsers without changing anything in the code.
Thanks for reading this article. Feel free to share your feedback.

Selenium WebDriver in C#: Switch To new window or tab

There are many instances when you click on a link and it opens a new window or tab. Selenim WebDriver provides a way to switch to that new window using SwitchTo() method.
See the code below
 
IWebDriver driver = new new FirefoxDriver();

//perform some action to open a new window. Like clicking a link.
driver.FindElement(By.Id("btnId")).Click();

//switch to new window.
driver.SwitchTo().Window(driver.WindowHandles.Last());

//if you want to switch back to your first window
driver.SwitchTo().Window(driver.WindowHandles.First());
I hope this little code snippet will help you in your future automation :)

Highlight an Element using Selenium Web Driver

Sometimes we want to make sure that we have used a right locator to identify an element. For this purpose, we can highlight that element before applying any operation on that. In this way, we will be sure that we are targeting a right element.
There is no direct way in selenium webdriver to highlight any element. But, we do have Javascript Executor class which we can use to highlight any element through javascript.

Highlight an Element using Selenium Web Driver

See the code below. Language used is C#
IWebDriver driver = new FirefoxDriver();

var element = driver.FindElementById('txtuser');

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

js.ExecuteScript("arguments[0].style.border='3px solid red'", element);
we can go one step further and make it a function.
 public static void HighlighElement(this IWebDriver driver, IWebElement element)
        {
           IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

           js.ExecuteScript("arguments[0].style.border='3px solid red'", element); 
            

        }
Please note that above is an extension method. So this function will be available across every instance of webdriver.
To use this function in our code, we can use it like this with any webdriver instance.
IWebDriver driver = new FirefoxDriver();

var element = driver.FindElementById('txtuser');

driver.HighlighElement(element);
I hope this post helps you in automating your web application.
Please do leave your feedback in comments section.

Css Selectors for Selenium WebDriver

CSS selectors are a great and fast way to locate elements in Selenium WebDriver. I have listed down some basic CSS Selectors for Selenium WebDriver which are commonly used. The goal of this tutorial is to explain as simply as possible about various types of usages of CSS Selectors using simple examples.

Css Selector Using ID Selector

Consider the below HTML

<input id="userid" type="text" value="userid" />
<button id="btnid">Submit button</button>

This HTML contains a text box with id = "userid" and a button with id = "btnid". If we want to select the text box using id css selector the syntax is
syntax = HTMLtagname # ID
Css Selector will be
input#userid      (it will select the text box)
button#btnid   (it will select the button)
The code in WebDriver will be
var TxtBoxElement = driver.FindElement(By.CssSelector("input#userid"));
var BtnElement = driver.FindElement(By.CssSelector("button#btnid"));

Css Selector Using Class Selector

Consider the below HTML

<input class="txtuser" id ="userid" type="text" value="userid" />
<button class=btnclass" id="btnid">Submit button</button>

This HTML contains a text box with class = "txtuser" and a button with class = "btnclass". If we want to select the text box using class css selector the syntax is
syntax = HTMLtagname . ClassName
Css Selector will be
input.txtuser      (it will select text box)
button.btnclass   (it will select the button)
The code in WebDriver will be
var TxtBoxElement = driver.FindElement(By.CssSelector("input.txtuser"));
var BtnElement = driver.FindElement(By.CssSelector("button.btnclass"));

Css Selector Using Attributes Selector

Consider the below HTML

<input class="txtuser" id ="userid" type="text" value="userid" />
<button class=btnclass" id="btnid">Submit button</button>
<a href="http://yahoo.com" title="myLink" > My Link </a>

This HTML contains a text box , button and an anchor with various attributes.
You can select an element by using any attribute.
syntax = HTMLtagname [attribute=value]
Css Selector will be
input[id=userid]      (it will select the text box)
button[class=btnclass]   (it will select the button)
a[title="myLink"]  (it will select the anchor)
The code in WebDriver will be
var TxtBoxElement = driver.FindElement(By.CssSelector("input[id=userid]"));
var BtnElement = driver.FindElement(By.CssSelector("button[class=btnclass]"));
var AnchorElement = driver.FindElement(By.CssSelector("a[title=myLink]"));


Css Selector Using Pattern Matching

Consider the below HTML
<input class="txtuser" id ="userid" type="text" value="userid" />
<button class=classbtn" id="btnid">Submit button</button>
<a href="http://yahoo.com" title="myLink" > My Link </a>

There are three ways you can match the pattern in any attribute.

1. Starts With (prefix)

In CSS Selector, Starts with is denoted by ^ symbol.
Example CSS Selector.
input[id^='user']   (this will select the text box in which id starts with 'user')
Code in WebDriver:
var TxtBoxElement = driver.FindElement(By.CssSelector("input[id^='user']"));


2. Ends With (suffix)

In CSS Selector, Ends with is denoted by $ symbol.
Example CSS Selector.
button[class$='btn']   (this will select the button in which class ends with 'btn')
Code in WebDriver:
var BtnElement = driver.FindElement(By.CssSelector("button[class$='btn']"));

3. Contains

In CSS Selector, Contains is denoted by * symbol.
Example CSS Selector.
a[href*='yahoo']   (this will select the anchor in which href attribute contains 'yahoo')
Code in WebDriver:
var AnchorElement = driver.FindElement(By.CssSelector("a[href*='yahoo'] "));

CSS Selector using Parent (indirect child)

Consider the following HTML
<div id="ParentDiv">
    <input class="txtclass" value="Text" type="text"/>   
    <button class="btnclass">Button</button>
    <a class="anchorclass" id="anchor" href="#"> My Link </a>

</div>

The above HTML contains our beloved text box, button and anchor again. But this time inside a parent element.
The syntax to select element using its parent is
syntax = AnyParentSelector space Anychildselector
example.
div#parentdiv input.txtclass
or
div#parentdiv a[id=anchor]
Code in WebDriver:
var ElementA = driver.FindElement(By.CssSelector("div#parentdiv input.txtclass]"));
var ElementB = driver.FindElement(By.CssSelector("div#parentdiv id=anchor]"));

CSS Selector using Parent (direct child)

Consider the following HTML
<div id="ParentDiv">
    <input class="txtclass" value="Text" type="text"/>   
    <button class="btnclass">Button</button>
    <a class="anchorclass" id="anchor" href="#"> My Link </a>

<div id="ChildDiv">
    <input class="txtclass" value="Text" type="text"/>   
    <button class="btnclass">Button</button>
    <a class="anchorclass" id="anchor" href="#"> My Link </a>
</div>
</div>


The above HTML now contains two divs. If we want to select a child element using indirect child method, it will return us more than one occurrence of child.
example
div#parentdiv input.txtclass   (This will return two text boxes with class "txtclass". As both these text boxes are the children of "parentDiv". One is the direct child and one is indirect child.
Now if we want to access the direct child, we should use ">" symbol instead of space.
example
div#parentdiv > input.txtclass  (this will return the first textbox which is the direct child of parentdiv
Code in WebDriver:
var ElementA = driver.FindElement(By.CssSelector("div#parentdiv > input.txtclass]"));
var ElementB = driver.FindElement(By.CssSelector("div#parentdiv  > id=anchor]")

Conclusion

By using above CSS Selectors, we can identify almost every type of complex web elements. I hope this tutorial helps many automation testers.
Please give your feedback in the comments section

Selenium Web Driver in C#: How to continue script on the already opened browser instance.

This was the very first challenge I encounter when I begin to do scripting in Selenium. Sometimes my scripts failed in between due to any unexpected dialog. To continue my scripts from the point where my scripts failed seems to be impossible in Selenium. So every time, my scripts failed, I have to start execution from the very beginning. This was taking a lot of time in debugging my scripts. So I desperately needed a solution.
After a couple of searches and different trial and errors, I devised a strategy.
I noticed that when I initialize the Firefox WebDriver, It opens the new browser instance. If I don’t close that browser instance and I initialize the Remote WebDriver instance, it uses the already opened FireFox browser. When I noticed that, its easy to write code to handle this and use it according to my way. So here is the code.
IWebDriver WebDriver = null;

try
{
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
Console.WriteLine("Executed on remote driver");

}

catch (Exception)
{

WebDriver = new FirefoxDriver(firefoxProfile);
Console.WriteLine("Executed on New FireFox driver");

}

The above code is easy to understand. If my Firefox browser is closed, the try block will fail and catch block will work, the script will open the new Firefox browser using FirefoxDriver.
If Firefox browser is already opened (due to previous script execution) , the try block will work and will initialize the Remote WebDriver. This Remote WebDriver will use the already opened FireFox browser and scripts will start where you want them to start from.
This strategy helps me a lot and saves an ample amount of time in my automation debugging. I hope this post will help you out too.

Coded UI Test : Testing all assertions in a single test case, even any assertion is failed.

Many of us encountered this situation in which we have put more than one assertion in our single test case. The failure of any one assertion cause the script to break. What if we want to test all the assertions and see at the end that which assertion fails and which has passed.
The above can be explained with a simple example. If we want to test the login of google docs, we will test basic functionality of that login box in a single test case. We assume we want to test four simple assertions.




  1. Email text box should have a title of "Email"
  2. Password text box should have a title of "Password"
  3. Sign in button text should be "Sign in"
  4. Stay signed in check box should be checked

In the above scenario, If we put above four assertions in our test case in a simple old-fashioned manner, it would look like below.

[TestMethod]
public void Checking_LoginBox_of_Google()
{
Assert.AreEqual(TitleEmail.Text, "Email");

Assert.AreEqual(TitlePassword.Text, "Password");
Assert.AreEqual(BtnLogin.Text, "Log in");
Assert.AreEqual(CbStaySigned.Checked, true);
}

In the above code, if first assertion fails, then test case will break and it will not check the remaining three assertions.
If we want to check all the assertions and then see the status, we can enclose each assertion in a try-catch block. This will catch every failed assertion and script will not stop. At the end of script, we can check the failed assertion count. If this count is  greater than 0, we can force script to break. See the above code now transformed into something special.

[TestMethod]
public void Checking_LoginBox_of_Google()
{
int failedAssertionsCount = 0;
try
{
Assert.AreEqual(TitleEmail.Text, "Email");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("First Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(TitlePassword.Text, "Password");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Second Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(BtnLogin.Text, "Log in");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Third Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(CbStaySigned.Checked, true);
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Fourth Assertion failed");
failedAssertionsCount++;
}
//in the end check failed assertion count
if (failedAssertionsCount > 0)
{
Assert.Fail();  //to force script to fail because there is some failed assertion present.
}

}
When script will execute, it will show you at which point your script is failed.



I hope this post will help many test automation engineers. Coded UI is a great tool and has many hidden benefits. Share your experience in the comment section, I would love to learn.
Thanks for reading.




Coded UI Test: How to continue execution after test case is failed.

When we are executing a batch of test cases using ordered test, in some situations we want to continue  the execution even if one of the test cases failed. After execution we want to see the complete report stating passed and failed test cases. This strategy is good for testers who want to execute test cases when they are not on seat.

1. For easy scenarios.

  1. open ordered test case by double clicking on it.
  2. there is a check box "Continue after failure" on the bottom left. Make sure it is checked.

  1. Execute this ordered test. you will notice that test cases are not stop executing even if any test case is  failed.

2. For Difficult Scenarios

Sometimes test cases fail due to an expected message box. In that kind of scenarios, subsequent test      cases will begin to fail one by one.
For that kind of scenario, correct strategy is to check if the test case status is failed, if yes then move the application under test to a base state and subsequent test cases should start from that base state.
We can achieve this in coded ui test by the use of coded ui test. Following are the steps.
  1. Open your testcase.cs file
  • Un-comment the testCleanup method.
  • Write below code in your test cleanup method.

[TestCleanup()]
public void MyTestCleanup()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed) //check if the test case is failed
{
// write some code here to move your application to a base state , for e.g. restart your application
// Process.Kill("yourapplication.exe");
// Playback.Wait(3000);
//Process.Start("yourapplication.exe");
}
}

  1. Note that testcleanup method executes after each test case.
I hope the above scenarios will help test engineers to execute test cases in a much better way.
Cheers.

Measuring Performance of Testers: James Bach Opinion

In my previous post, I shared a link of the presentation by Cem Kaner. Today I came across this wonderful article from James Bach on the same topic i.e. measuring performance of testers. Without going into further details, I think I should just share the link here so my readers can read what this man has to say about the topic
Article Link
Regards,
Saad

Coded UI Tests: Tutorials


When I started working on Automation, my first tool was Microsoft Visual Studio 2010 in which Microsoft has provided an excellent platform for functional testing which is called Coded UI Test.

Before working on this tool, I learned about it from my good teacher THE INTERNET and my teacher has provided me valuable information about how to use it. So I thought to share the most useful links here.

Here is the list of links:

Getting Started with Coded UI

Coded UI Basic Walkthrough: very easy and step by step tutorial. Covering only the basics.

Understanding the Generated Code by Coded UI Test Part1

Understanding the Generated Code by Coded UI Test Part2 (This article in two parts helps you to understand the code which is generated by Recording and Playback)

Modifying Generated code in a Coded UI Test: When you generate the code after recording and playback , this article shows you how to modify the generated code.

Test Automation Guidance using VS 2010 Coded UI   (.pdf format) (excellent PDF covering step by step guidance of creating your first coded UI test project.Very detailed and covering 80% of the concepts. My personal favorite. )

Coded UI sample Framework (Visual Studio Solution)  ( this is the sample project to get you started with the real life application testing)

Advanced Concepts:

How does “Coded UI test” finds a control ??  (This article gives concepts about how the playback engine finds the control in your application)

Walkthrough: Using multiple Coded UI maps in test automation  (It is extremely necessary to use separate UI Maps for each module in testing real life applications. This article explains how to do it)

Best Practices for Coded UI Tests: Best Practices while making Coded UI Tests from Microsoft.

Other than above you can visit other posts of this blog in which I have listed some issues which I faced while making Coded UI Tests and came across a solution.

Keep visiting.

Cheers.
Update: Here are some more links which I found very useful
Content Index for Coded UI maintained by MS Employee    (A great resource list which is maintained by Microsoft employee )
Coded UI Test Framework  (This guy has created a Coded UI framework and shared that on GitHub so that you can quickly start your scripting. He has also created a video to teach about how to use this framework)
Coded UI Adda  ( A blog dedicated to Coded UI created by . I found it very useful and comprehensive)
A video series by Karthik  ( Amazingly crafted video series on Coded UI by my friend Karthik on his wonderful website executeautomation.com. Visit the link to get the feel of what I am talking about)
Image Credits
http://www.evoketechnologies.com/blog/

Friday, March 25, 2016

Horrible Experience of using Test Explorer in VS 2012

Today I install VS2012 on my virtual machine to see if there any good feature related to coded UI tests.
The first thing I encounter that there is no test view window available which was present in VS 2010.
Instead, there is a new window called Test Explorer. I build my project and all my tests become visible in the Test Explorer. Now the real horrible experience begins. I was trying to group my test according to the class name like I used to do it in Test View. To my surprise, there are only two options for the group by . Either by duration time or by test outcome.
This was the not the first issue. I begin to explore Test Explorer and amazed how many features it is missing. some of them are:
  • we cannot “copy” exception messages from the unit test results.
  • we cannot control the columns as we like to see.
  • There is no refresh button to refresh test cases.
  • we cannot see that which test is currently running.
  • The summary page keeps displaying information about the previous run which is very confusing. It should display information about the current run.
  • With VS 2012 I  have to wait at least 18-20 seconds to see the results in the Test Explorer after my test is executed.
Overall it was a big disappointment. I searched on the internet and found an update of VS 2012 link. They are saying that this update will provide some Group By features in the Test Explorer.
There is also a suggestion posted that Microsoft should drop Test Explorer and bring back Test View in 2012.
This was my experience. How about yours?

Coded UI Tests: Executing a test case while desktop is not active.

One of the biggest time consuming and productivity killer task for automation engineers is the time when they are executing their scripts. Not only scripts take long time to run but also while they are running, we cannot do any thing else. This is a desire of every automation engineer that he could do some other work (like developing other test cases) while the previous test case executes.
Same case happens with me and I found a solution for that. Since coded UI tests require an active desktop screen to execute it, there are three ways to achieve this.
1. Either you install visual studio on your development machine and install Test Agent on any other machine where you want to execute tests. When you will execute a test case on your development machine, it will be executed on the test machine PC and development machine will remain free for doing other tasks.  (This approach requires another PC. That means it is an expensive approach)
2. You can set up a VM machine of Windows 7 (or any other) on your development machine. On the VM machine install Test Agent. When you will execute a test case on your development machine, it will be executed on the VM machine and development machine will remain free for doing other tasks. (This approach is less expensive)
3. You can set up a VM machine of Windows 7 (or any other) on your development machine. On the VM machine install Visual Studio 2010 and get the latest version of your source code there with the help of TFS. In this approach you will have two benefits.
  1. You will be able to execute test cases right from VM machine
  2. You will be able to develop or modify test cases in the VM machine.
In approach number 3, you can run the test cases from visual studio inside VM machine and minimize that window. The code will be executed while this VM window is minimized. You will be free to do other tasks as well. I personally follow approach number 3. It allows me to change my code in both environments and by using TFS I am also able to check in / check out my code at both locations and code remains always updated.
What approach do you follow? and what are the benefits. I will love to hear it out.
Cheers.

Coded UI Tests: Error Resolution “COM object that has been separated from its underlying RCW cannot be used”.

This error may come across a coded UI developer while executing test cases. This error is occurred if you are using a same static class across two test cases.
For example in my scenario, I have created a static class called "MainCall". In this class, I have put all the UIMAP class references. So that I can call any test case of any UIMAP from the "MainCall" class.
The class is as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaadAutomation.Maps.UIMap1;
using SaadAutomation.Maps.UIMap2;
using SaadAutomation.Maps.UIMap3;
namespace SaadAutomation
{

class
MainCall
{

private static UIMap1 _spMap;
private static UIMap2 _cMap;
private static UIMap3 _crMap;

public static UIMap1 SPMap
{
get {
if (_spMap == null)
_spMap = new UIMap1();
return _spMap;
}
}

public static UIMap2 CMap
{
get{
if (_cMap == null)
_cMap = new UIMap2();
return _cMap;
}
}

public static UIMap3 CRMap
{
get{
if (_crMap == null)
_crMap = new UIMap3();
return _crMap;
}
}
}
}


I was calling functions like this in a test method.
[TestMethod]

public void ExampleTestCase1 ()
{
MainCall.SPMap.Method1();
MainCall.SPMap.Method2();
MainCall.CMap.Method1();
MainCall.CRMap.CloseAllWindows();
}
[TestMethod]

public
void ExampleTestCase2 ()
{
MainCall.CRMap.Method1();
MainCall.CMap.Method2();
MainCall.SPMap.Method1();
}
When I run both of the above test methods together, I got this error message.
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
So I searched on the internet and found the solution. It is just a setting change in the local.testsettings file.
Resolution:
To resolve this error, open your .testsettings file in any XML editor. This file is located under solution items.
Add this line (marked bold)
<Description>These are test settings for Trace and Test Impact.</Description>
<Execution>


Don't forget to restart visual studio after that. Now you will be able to run your test cases and this error will not come inshallah.
There is one side effect of adding this line. You will not be able to debug your test. See my another post about the mentioned issue.
What other errors you encounter? Post here as comments and we will try to find the solution together.

Testing Challenge

Can you find more than 20 defects in the below image? Write your defects in the comments section.