Chat Beta

Showing posts with label Telerik Test Studio. Show all posts
Showing posts with label Telerik Test Studio. Show all posts

12/21/2016

Unit Tests With Telerik- A Simple End-To-End UI Test Automation Solution

Thinking about automating functional tests (UI) for the web application that I work with, and having the idea to make it an "End-to-end" solution where tests are written and pushed to the test runner, are then picked up and run against SUT automatically, and finally a result of the test status is sent to user in the form of a mail, following was an attempt that I made during last couple of days which I guess works fine at the moment. Sharing the experience hoping it will shed some light to others

If you have a working CI environment like Jenkins or TFS, it may be handy as it  will do the most of the things out of the box, but in my case I had to come up with my own way of achieving it.

The entire project can be found here .

Following technology/ tools stack was used to achieve this
 1. Telerik Testing Framework - This is the tool used for writing UI tests. You might probably be using Selenium webdriver over Telerik as your preference  
 2. Visual Studio 2013/15- 'VSUnit' unit-test framework with C#  
 3. Python scripting language - To automate some background tasks like sending mail, reading log files, achieving files etc.  
 4. DOS batch files- To initiate the test building and running  
 5. SMTP Server - This is to send out the status mail that is composed at the end of the test run. I use a free version of an easy to setup mail server called MailEnable for this purpose.  
 6 . Windows Task Scheduler -To schedule test runs  
 7. Command line tools - MSBuild.exe, MSTest.exe - This is to build the visual studio project and run the tests in 'Test Runner' machine  
 8. Dedicated VM for running the tests- Which I call it the 'Test Runner'. Test Runner will run the UI tests at scheduled time without user-intervention  
 9. Github repository for version controlling and pushing tests to 'Test Runner' after they are being developed and tested locally.  
 10. Git command "git pull" to pull new changes back to the test runner  

Telerik Test Studio is a commercial grade UI Test automation platform that helps QA professionals to automate their functional test in Web , Silverlight and WPF applications. 

Telerik Test Framework is their .net library that the Test Studio is built upon which can be freely downloaded from there site. Its free to use under certain conditions. It contain automation infrastructure library + various other libraries to support browsers, proxy , Silverlight etc. We will have to write code to accomplish what is expected from our tests.

We adopt the 'Page object model" when handling locators which could reduce potential maintenance overhead in the long run. If you want to know what it is, click here and here

Once the Telerik Framework is installed in the PC, we can start using it via Visual studio.

We start with a new project in VS, and select "Unit Test Project" from "Test" under "Templates"




 We will need to add references to the solution to make it work




Then use "VsUnit" test under "Telerik TestingFramework" . VsUnit is the Visual studio team test with unit testing framework. Since we develop our tests using Visual studio its a better option to use VsUnit rather than other unit test frameworks lik MBUnit or NUnit




In VsUnit test template, you get several methods and you can write your tests under [TestMethod] section




Once tests are built, they will appear in "Test explorer" in VS



we can manually execute tests from the "Test explorer"




The project  structure looks like following



I have developed some utility methods that can be reused in tests which ease the development efforts. You will find them under 'common methods' folder

Following methods are available.
 1. A separate class has been written to handle database interactions. It has methods to connect, execute and retrieve result from the database.  
 2. A method is implemented to capture screenshots and error logs, which can be used for error handling purposes.  
 3. An email sending method has been added which can be called in a test when you need to send a mail to a recipient.  
 4. A method to provide keyboard inputs to a text field in your application. It has been overloaded so that it can handle text fields in both documents and iframes.  
 5. A random data generating method which can be used as an input.  

Tests can be scheduled to run automatically against a SUT in the following manner.



We can create a batch scripts to initiate the process, first by pulling the new changes from the remote repository to the test runner, and then run MSBuild.exe to rebuild the project and run the test using MSTest.exe. Here the result output is directed to a log file instead of them being printed in the console. This log file later be fed to the email body.
 @echo off 
 "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" "C:\GIT\Telerik\CS.sln" /p:configuration=debug  
 call pull.bat
 "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe" /testcontainer:"C:\GIT\Telerik\CS\bin\Debug\CS.dll" /testsettings:"C:\GIT\Telerik\Settings.testsettings" >>"C:\GIT\Telerik\CS\TestResults\Summary.log"  
 exit  

Also add a testsettings file to the project where we can invoke some actions before and after the test execution


For example, here I call a script to delete files in the result log before a new test run begins and call a batch script to send out the mail after the test run is finished. 

How the mail is composed (by reading log files and attaching error-dumps), is done using a python script which can be found in the git repo.
 https://github.com/tharinda2012/Telerik/blob/master/CS/SupportingScripts/SendSummaryMail.py  
 https://github.com/tharinda2012/Telerik/blob/master/CS/SupportingScripts/utils.py  

The parameters that must be fed to the tests can be given in a config like following


The Status of each test run will be notified to the user via an email as below




In case you use mouse or key board actions inside the test scripts to achieve certain tasks, you might encounter issues when running tests if the running machines are locked (no active session). In this case following approach can be used to remedy it.





10/10/2014

Page object Model- Object repo creation script in Python using MS Excel

In UI Test automation, its important that we create an object repository. Object repository can be created in many ways and one of the ways is that each element is represented as a class property. Telerik Test automation framework is considered in this example. Telerik is a useful UI test automation tool compared to other commercial and open-source solutions 
  
Public class object_repo  
   
  {           
       public Element UserName  
         {  
           get  
             {  
               return _manager.ActiveBrowser.Find.ById("user9874");  
             }  
         }       
  }  
where this can be accessed via a class object in your code. Creating above class structure for many elements manually can be a time taking boring job. A simple script can do the job in a second. We add the required information to an excel file ex: Element name, attribute id, attribute value, and find By criteria, and the script will read each row and convert them in to a property in the class




The script was written in Python 3.4. I used PyCharm community version as the IDE.

You import following modules


 import os  
 import sys  
 import xlrd  
 import string  

Python module "xlrd" is dealing with Excel.
If it complains xlrd is not available, you can do a quick installation via easy_install.py which is located in 
%python_installed_dir%/Lib/site_packges
>>easy_install xlrd  will install the module.
  • workbook = xlrd.open_workbook(read_excel) will open the excel to access
  • sh = workbook.sheet_by_name(sheet) specifies which work sheet in the excel to be used
  • sh.nrows specifies the number of rows that data is available in the sheet
  • sh.ncols specifies the number of columns that data is available in the sheet
       for row in range(1, sh.nrows):
            for column in range(sh.ncols):
                item = sh.cell_value(row, column)
                list.append(item)

above piece of code will read cell of each row column by column until condition satisfies and append data to a python list. And list is read by its index and used where required.

The script is as following:
 #importing required modules  
 import os  
 import sys  
 import xlrd  
 import string  
 #variables declaration  
 read_excel = os.curdir + "\\" + "element_file.xlsx"  
 write_file = os.curdir + "\\" + "element_output.txt"  
 class_name = 'Public class element_Class \n { \n'  
 class_constructor = '''  
         private Manager _manager;  
   
         public constructor(Manager m)  
           {  
             _manager = m;  
           }  
       '''  
 end_bracket = '\n }'  
 #main function which reads the excel and write to the text file  
 def read_from_excel():  
   file_cleanup()  
   sheet = input("\nIndicate the sheet name of the excel: Press [Enter] if the default is 'Sheet1' : ")  
   if sheet is '':  
     sheet = "Sheet1"  
   workbook = open_excel()  
   try:  
     sh = workbook.sheet_by_name(sheet)  
     list = []  
     write_to_file(class_name)  
     write_to_file(class_constructor)  
     for row in range(1, sh.nrows):  
       for column in range(sh.ncols):  
         item = sh.cell_value(row, column)  
         list.append(item)        
       msg = '''  
       public Element ''' + list[0] + '''  
         {  
           get  
             {  
               return _manager.ActiveBrowser.Find.By''' + string.capwords(list[1]) + '''("''' + list[2] + '''");  
             }  
         }  
       '''  
       list = []  
       write_to_file(msg)  
     write_to_file(end_bracket)  
     input("\nOperation successful. Press [Enter] to Exit...")  
   except Exception as e:  
     print(str(e)+' available. Please retry. (Hint: check case and spelling...)')  
     read_from_excel()  
   
 #function for opening the excel file  
 def open_excel():  
   try:  
     workbook = xlrd.open_workbook(read_excel)  
     return workbook  
   except FileNotFoundError as e:  
     print(str(e))  
     sys.exit()  
   
 # function for writing the result to the text file  
 def write_to_file(msg):  
   try:  
     file = open(write_file, "a+")  
     file.write(msg)  
     file.close()  
   except Exception as e:  
     print(str(e))  
   
 #function for delete the existing file for next round  
 def file_cleanup():  
   if os.path.exists(write_file):  
     os.remove(write_file)  
   
 #calling the function to get the job done  
 read_from_excel()