Wednesday, December 23, 2015

Some WebDriver Utils for your Visual Studio project

Introduction

In this article we will write about some utils methods that are useful in your selenium webdriver framework
- Visual Studio 2013
- Selenium WebDriver with C#
- Nunit to run the tests
- An existing page object framework


Utils class

In our framework we created Utils.class that contains different methods used in  nunit tests in order to work with mouse events, selection control, string manipulation, etc.

Mouse Events

If you cannot perform wevdriver mouse events by using the usual way, with action class, replace this with 
javascript.




... and with Action class


Work with Alerts, Windows, Frames

Below, you can find some methods used to switch to alert, window, frame











String helpers







Selection controls







Happy testing and... make it green, becomes a dream :).

For every post I'll recommend something to read. The link for today is:  The C# Player's Guide (2nd Edition).

Wednesday, November 18, 2015

Your First Applitool Eyes Test Integrated in a .NET WebDriver Framework

Introduction

In this article we will talk about Applitool Eyes and how we can use this tool to integrate it in a WebDriver framework.
The following tools were used:
- Visual Studio 2013
- Selenium WebDriver with C#
- Nunit to run the tests
- An existing page object framework
- CI --> Jenkins

What is Applitools Eyes?

It is an automation testing tool that will check all visual aspects of the application from a given screen, meaning that we don't need to write separate tests for each UI element on the screen. The tool provides easy integration with different test tools and frameworks like Selenium, CodedUI, HP QC, etc. It allows testing the application on different browsers and resolutions. The logs that are generated after running the tests are very smart, include playback with step by step actions.
What you need: create an account. Once you are logged in, an API key will be generated for you. This will be used in your framework. Find the key by accessing menu My API key.



The tutorial from applitool site that you can read is this: https://eyes.applitools.com/app/tutorial.html.
The tool has a free limited version that will allow running 25 tests/week. You can find more about pricing here: https://applitools.com/pricing/.

Create your First Test

As described in the tutorial, in order to write your first test you need to add Eyes.Selenium package to your project. Go to your project --> References --> Right click on the project --> Manage NuGet Packages... -->Online -->search for Eyes.Selenium and Install it. The reference will be added to your project.



Then you can create your first test like this:



After you run your test for the first time it will be saved as a baseline. You need to run it again and open Applitools Eyes --> Test Results page to analyze the changes. If your test fails it means that there are wanted or unwanted changes in your application page. Depending on your analysis you can accept (if the change is not a bug) or keep the baseline = reject changes (if we have a bug in the application). For a failed test in the nunit test run window we will see a link to the test report providing the exception thrown by Eyes.Close() method. See image below that is taken from applitool site.



Include Applitool in an Existing Framework

We've seen above a brief description about how we can create our first webdriver + applitool test. But what if we want to have a suite of applitool tests and to include them in an existing framework?
We mentioned in previous posts about page object and a way of separating NUnits/MSTests from page object identification. Have a look on http://ralucasuditu-softwaretesting.blogspot.ro/2015/06/write-your-first-test-with-specflow-and.html because the idea is the same. 
- Create a project where 
  • you keep some Utils to deal with the driver, with the controls, to extend web driver if this is necessary, etc.
  • you keep the page object identification for every page, meaning that this is the place where you find your objects with selenium
- Create another project where
  • you define a Start test for SetUp/TearDown actions
  • you are writing your NUnit tests, without having any reference to webdriver elements
  • create an app.config in which you define what is the application url, login credentials, configuration on which you run your tests (browser). Below, you can see an example of app.config file used in our solution and also, the structure of the project













How to adapt this framework to run tests on applitool?
Open your Driver.cs class and:
  • Use Applitols as reference 
  • Create an Eyes field that is using your API Key

  • Initialize your driver with Applitool
  • To close your driver with Applitools you can use this

  • Driver.Initialize method is used in the Nunit SetUp (i.e. the sequence of code that will be executed before each test that, in our case, is defined in Start.cs class). Driver.Close is used in TearDown  (i.e. the sequence of code that will be executed after each test that, in our case, is defined also in Start.cs class). See an example below:



where GetUnitName is the name of the unit test that will be run and in this case, will also be the name of the Applitool test that will appear in Applitool UI --> Test Results page.




  • Now we are ready to write our tests


  • Check the test result on Applitool UI --> Test Results page.


  • Include your applitool in a CI system, like Jenkins. You can create a job that is running only nunit tests having category = "Applitool". See this link, chapter Specifying Test Categories to Include or Exclude, that shows you how to do this: http://www.nunit.org/index.php?p=consoleCommandLine&r=2.2
  • If a test fails, you will notice in jenkins the error 

I hope you enjoyed this article!
Happy testing and... make it green, becomes a dream :).

For every post I'll recommend something to read. The link for today is: Learning Selenium Testing Tools - Third Edition

Thursday, October 1, 2015

HipChat with Visual Studio Online and Jenkins

Introduction

In this article I'll talk about HipChat and how you can receive notifications from Jenkins and Visual Studio Online related to builds, work item creation, check-ins, etc.

What is HipChat

HipChat is a tool used for... chatting :) from every OS, Mac, Windows, iOS, Linux, from every place, from home, train, your friend's pool. It has a web and a desktop version. You can create a room and invite people from the same project to join the group. 



An interesting feature is that it has the possibility of integration with Jenkins and Visual Studio Online.
For example, we may have a job in Jenkins for selenium tests. When the job is finished, I want to receive a notification in Hipchat about the status of the job and I want to be able to go directly to Jenkins test results page. 
Another example, if my project is managed in Visual Studio Online, I want to be notified when a check in is performed or when a new bug is reported.
For being able to perform this integration, create an account in https://www.hipchat.com/ and download the desktop version from here https://www.hipchat.com/downloads
Open the desktop application, login, go to Lobby and Create a Room. Then you can invite people to join the room.



Jenkins and HitChat Integration

If you have a job in Jenkins and you want to receive notification in HitChat room when the job finishes (i.e. selenium tests job is failing), you need to add a Jenkins plugin: HitChat Plugin.
Then, on your job configuration you need to add a post-build step like in the image below, by providing the HitChat room token, id/name and to set the notification settings

How can you obtain the room id and token? Login with the admin account on https://www.hipchat.com and go to your room Summary and Token areas.






When the job finishes you and all members inside room, will get a notification in HitChat.




Visual Studio Online and HitChat Integration

Similar notifications can be received from Visual Studio Online, when you or anybody in the team adds new bugs, tasks, performs a check in, etc. What do you have to do? Go to you Visual Studio Account in the administration panel and open Service Hooks.



Select a HitChat service.



Then you need to trigger the notification event. In the image below you will trigger new bugs.



Finally, you need to set the token and room id, like you did for Jenkins.



In HitChat (desktop or web) you will be notified when a new bug is created.




Happy testing and... make it green, becomes a dream :).
For every post I'll recommend something to read. The link for today isMastering Jenkins

Wednesday, September 23, 2015

Running your WebDriver tests on different browsers with Jenkins

Introduction

In this article I'll show you how you can run your tests on different browsers by using an app.config in your project and Jenkins. I use C# for my webdriver project.

App.config

In your project that contains the webdriver tests (nunit, mstest etc.) add an app.config file.



This is an .xml file in which you can add a key value for the browser type you want to run your tests. Below, you have an example:
<configuration>
<appSettings>
<add key = "BrowserType"  value = "chrome" />
<add key = "Email"  value = "youremail@com" />
<add key = "Password"  value = "yourpassword />
<add key = "Site"  value = "http://yoursite.com />
</appSettings>

</configuration>

In a basic class you can initialize your Driver like this:

public static void Initialize()
        {
            var browserType = GetConfigProperty("BrowserType");
            switch (browserType)
            {
                case "chromemac":
                    ChromeDriverService service = ChromeDriverService.CreateDefaultService("path to your driver", "chromedriver");
                    Instance = new ChromeDriver(service, new ChromeOptions());
                    Instance.Manage().Window.Position = new Point(0, 0);
                    Instance.Manage().Window.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                    break;
                case "chrome":
                    Instance = new ChromeDriver(@"path to your driver");
                    Instance.Manage().Window.Maximize();
                    break;
                case "firefox":
                    Instance = new FirefoxDriver();
                    Instance.Manage().Window.Maximize();
                    break;
                case "ie":
                    Instance = new InternetExplorerDriver(@"path to your driver");
                    Instance.Manage().Window.Maximize();
                    break;
                case "safari":
                    Instance = new SafariDriver();
                    Instance.Manage().Window.Maximize();
                    break;
            }
        }

where var browserType = GetConfigProperty("BrowserType"); will take your value from the app.config.
In our case, the tests will be executed on Chrome for Windows.
Note that this example shows you how you can run your tests on Chrome for Mac, and gives you a solution on how you can maximize your Chrome browser on Mac OS.

Jenkins

For each configuration where you want to run your tests, store a different app.config file with the desired key values.



For example, for firefox, 
- BrowserType = "firefox"
- Email = "email@firefoc.com", etc



For chrome, BrowserType will be "chrome."

How can you use these new configurations in the Jenkins job? Create your job for running tests on, lets' say Firefox. After the step that is taking the latest sources of the application we have to add a new step that is copying our app.config in the project location. You can do this like in the example below
- for a Windows OS: xcopy /s/y "C:\TFSOnline\PIN\config\firefox\App.config" "C:\Users\Raluca\.jenkins\jobs\xamarin - pin - jenkins - firefox\workspace\aut\Pin_Selenium_Testing-Xamarin\PinTests\App.config"
- for a Mac OS: cp /Users/raluca/Desktop/Raluca/PINHelpers/config/firefox/App.config /Users/raluca/.jenkins/jobs/macPinFirefox/workspace/aut/Pin_Selenium_Testing-Xamarin/PinTests/app.config

In this way you tell your project to run your tests on firefox/chrome/ie/safari driver.
For each configuration you'll need a different job, that will be executed, during night at a specific hour.
That's it.

Happy testing and... make it green, becomes a dream :).
For every post I'll recommend something to read. The link for today is:  Agile Testing: A Practical Guide for Testers and Agile Teams


Popular Posts