Tuesday, 20 December 2011

WATIN Review

                      

WatiN Logo

Overview:

WatiN (pronounced as What-in) enables .Net to interact with Internet Explorer 6, 7, 8 as well as FireFox 2 and 3. Inspired by WatiR, Jeroen van Menen started at the end of 2005 the development of WatiN to replace the commercial test application of his company. 2007 Edward Wilde joined the project and enabled WatiN to support FireFox. Furthermore he created WaX, a small framework that allows to create WatiN tests using Microsoft Excel.
The following review will focus on WatiN using C#.

Technique: Star2Star2Star2

WatiN interacts with the Internet Explorer using its COM interface. The FireFox interaction is done using the JSSh (JavaScript Shell Server) extension for Mozilla FireFox. Although the interaction technique for both of the browsers is different, the API of WatiN is not (in most cases). So, actually WatiN tries to hide the type of the automated browser to create browser independent tests. The disadvantage of this technique is, that this special bridge between WatiN and the automated browser has to be developed for every different browser and eventually must be changed when a new browser version gets launched.
Using a more or less native method to interact with the browser and the html controls has some advantages:
  • Neither the browser nor the displayed web site/web application gets changed to enable interaction. So testing with WatiN does not have any side affects.
  • The native interaction enables WatiN to read many details of the current state of the browser so for example WatiN is able to synchronize the test with the browser.

Documentation:

The documentation of WatiN is a bit outdated. The API reference on the other hand is complete and well structured. Also a well documented and illustrated Getting started article is available. A very helpful Mapping Table and FAQs are also available on the web site.

Support (Community/Commercial): Star2Star2Star2

Community support is available through the official mailing lists, and the bug/feature tracker. Furthermore professional support is possible through the developer Jeroen van Menen.

License: Star2Star2Star2

WatiN is available under the Apache License 2.0.

Installation: 

The installation of WatiN is easy and well documented. First download the latest release from sourceforge. The archive contains the binaries (WatiN.Core.dll) the source, some examples and the JSSh Plugin for FireFox. A ReadMe and a compiled help file are also inside the archive. Then you have to decide which IDE (for example the free Microsoft Visual Studio Express Edition) you want to use, because WatiN is just a library which can be used in every .Net language and in every .Net compatible IDE. I will use MSVS 2008 EE in this example.
After creating a new console application you have to link the WatiN.Core.dll to your project:
  • 1Menu: Project/Add Reference
  • Navigate to the "Browse" tab and open the the "WatiN.Core.dll"
  • Accept by clicking "OK"
  • If you reopen the dialog and navigate to the "Recent" tab you will see the referenced dll
Reference the WatiN DLL in MSVC
The next important thing is to add the line "[STAThread]" to your class (read more why this is important). Now you can begin to write your tests with WatiN.

Handling & Usability: 

WatiN is just a .Net library, so the only way to use it is by developing an own test project using a .Net compatible language. The API of WatiN is very easy to use so even a non professional software developer should be able to understand and use it quickly.
So even the API of WatiN is easy to use, WatiN does not provide any help to create automated browser tests. No real (i will mention WatiN test Recorder later) Capture & Replay possibility is available and no tool to reveal the details of an element of the web site to automate exists. So, WatiN says "Don't forget to download the Internet Explorer Developer Toolbar (not compatible to IE 8, use the developer tools included) or Firebug for FireFox. This will help tremendously in exploring the structure of a webpage so you can create your test automation more easily." and this is indeed a good advice. These tools help you to identify the elements (links, buttons, input fields,...) of a web page.
Get the details of an element
With this information it is quite easy to automate a test. First create a new browser instance or attach the test to an existing browser.
Then try to "spy" the web page element to interact with using the installed MS IE Developer Toolbar. Use the "id" or "name" tag to uniquely identify the element. Then use the Mapping Table to map the web element to the appropriate WatiN statement. To identify the element using WatiN you have to use one of its built in Find.By methods for example Find.ByID or Find.ByName. Continue these steps to create a simple web test.
WatiN on its own does not offer for example an automatically generated report or an overview, which steps passed and which failed. Therefore you have to deal with this on your own if you want such functionality.
Creating tests without the possibility of a capture tool has (at least) one major disadvantage: You have to take care of all interaction yourself. On simple web sites the interaction is focused on clicking links or buttons and type some text into fields. The creation of the tests is simple as well. But nowadays many web sites use AJAX more or less and sometimes automating web sites with AJAX can get a real challenge. Some events have to be fired manually using Element.FireEvent to achieve the same result as a normal user would get. To find these events the creator has to understand HTML/JavaScript very well.

Predefined Functionality: 

WatiN offers everything you need to automate the web browser, but not much more. However the given functionality is easy to use. Synchronization with the browser gets done automatically for example by using Element.Click method (Element.ClickNoWait offers no synchronization if needed) or explicit by using Element.WaitUntilExists. Verification is done via the various properties of each WatiN element for example TextField.Text. However WatiN offers no functionality beside the browser interaction.

Data-Driven-Testing: 

WatiN on its own is not designed for Data-Driven-Testing. But because of the use of .Net this feature could be implemented. Furthermore a WatiN FitNesse fixture is available.

Compatibility: 

WatiN is compatible to Internet Explorer 6, 7, 8 and FireFox 2 and 3. It is officially limited to Microsoft Windows and .Net > 2.0. Unofficially some attempts were made to bring WatiN to Mac OSX running with Mono but they are not fully usable yet.

Conclusion: Star2Star2Star2_3

Overall WatiN is a great help for creating automated browser tests in .Net, especially when you are already confident with .Net. Although the functionality is limited, it offers everything you need to interact with the browser.
Creating tests could be difficult because a capture tool is missing. So, sometimes doing the same things with WatiN a normal user would do is a bit tricky, but mostly possible.

WatiN test Recorder

As mentioned earlier WatiN does not offer a capture tool, but someone else does. The tool is called WatiN Test Recorder and is designed to capture browser interaction and to create WatiN compatible source code.

Important links:

Friday, 4 November 2011

SeleniumRC using TestNG


                           Running Selenium RC script

              Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC is great for testing complex AJAX-based web user interfaces under a Continuous Integration system. It is also an ideal solution for users of Selenium IDE who want to write tests in a more expressive programming language like Java,Perl etc.

             Selenium IDE is much simpler and can be used only on firefox. Selenium RC lets you to write much complex scripts using the programming language features. And you can mention the browser on which you want to run the script. Selenium RC supports Internet Explorer, Firefox, googlechrome etc.

              We can use any IDE for developing your scripts. Lets start creating the Selenium RC script using TestNG. You will need to install the following first.

We need to install the following first:

  1. JDK : You can be download it from here http://java.sun.com/javase/downloads/index.jsp Set the classpath.

  1. Selenium RC: Download it from here: http://seleniumhq.org/download  Extract the files in a folder named SeleniumRC.

  1. TestNG : Download TestNG from http://testng.org/doc/download.html Unzip. The files will get extracted to a folder called testng.

  1. Eclipse : Download eclipse from http://www.eclipse.org/downloads/.Choose the "Eclipse IDE for Java Developers".Unzip the downloaded file. The files will get extracted to a folder called 'eclipse'.

  1. TestNG plug-in for Eclipse: Start Eclipse by clicking the eclipse icon in the eclipse folder that's mentioned earlier. Click on Help->Install New Softwares. Enter "http://beust.com/eclipse/" in the "Work With:" and press Enter. You should see TestNG. Select it and then press Next till you reach Finish. Restart Eclipse.

Let's start with the SeleniumRC and TestNG script now:

  1. Open eclipse. Start by creating a new project : File->New->Java Project. Give a name say 'SeleniumLowcostloadtest'.






  1. Add the required jars. Select the project-' SeleniumLowcostloadtest ' , right click Build Path->Configure Build Path. Click Libraries->Add External Jars. You will have to add the testng and selenium jars.


          





  1. Create a new package in the project. Select the project -'SeleniumLowcostloadtest' and then right click New->Package. Give a name say 'First'.









  1. Create a new java program. Select the package-'First' and then right click New->Class. Enter the name as say 'Lowcostloadtestclass'.Press Enter.




  1. Now go to Selenium IDE and open the ‘www.lowcostloadtest.com' script which we had created in our first blog- 'Starting with Selenium IDE'. Click on Options->Format->Java(TestNG)Selenium. You can see that the format of your test case is now changed.

The format is in the testng format now. Your script would look like below:




  1. Copy paste the whole code into eclipse(Lowcostloadtestclass). You can see some errors.



7. Make changes so that your testng script looks like below:



  1. We can run the server in two ways:

i) Using External Tools Configurations:





Click on Run to start the server.

Otherwise we can Run the server by cmd.

ii) Run the Server by cmd

Start your selenium server. Go to command prompt and then go to SeleniumRC/JavaServer folder (where you installed selenium rc). Type in
java -jar selenium-server.jar -port 4444This is assuming that the port 1234 is free. You can give any other port number if 4444 is being used. Just ensure that the port num given in the testng script matches with the port num used to bring up the selenium server.

Your command prompt screen should look something like below:



  1. Now run the testng script by going back to eclipse. Right click on the testng script. Click RunAs->TestNG Test.
    Your script should run successfully.




Parameterization:

For parameterization, just edit the code like below:

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;

import java.io.File;
import java.util.regex.Pattern;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ParaLowcostloadtestclass extends SeleneseTestBase {
     
   @BeforeClass
      //@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
      public void startSelenium() //String seleniumHost, String seleniumPort, String browser, String webSite) throws Exception
      {
            selenium= new DefaultSelenium("localhost",4444,"*googlechrome","http://www.lowcostloadtest.com/");
            selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");
            selenium.open("/");
            //Thread.sleep(5000);
            selenium.windowMaximize();
      }
   @DataProvider(name = "DP1")
      public static Object[][] createData1() throws Exception{
          Object[][] retObjArr=getTableArray("Data.xls","Submit", "Start");
          return(retObjArr);
}
@Test(dataProvider = "DP1")
 public void testLowcostloadtestclass(String Login, String Password) throws Exception {
            selenium.open("http://www.lowcostloadtest.com/");
            assertEquals(selenium.getTitle(), "Low Cost Load Test");
            selenium.click("link=Login");
            selenium.waitForPageToLoad("300000");
            assertEquals(selenium.getTitle(), "Login Form");
            selenium.type("id=login",Login);
            selenium.type("id=password", Password);
            selenium.click("name=Submit");
            selenium.waitForPageToLoad("300000");
            assertEquals(selenium.getTitle(), "Member Index");
            selenium.click("link=Logout");
            selenium.waitForPageToLoad("300000");
            assertEquals(selenium.getTitle(), "Logged Out");
      }
      public static String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception
      {
              String[][] tabArray=null;
             
                  Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
                  Sheet sheet = workbook.getSheet(sheetName);
                  int startRow,startCol, endRow, endCol,ci,cj;
                  Cell tableStart=sheet.findCell(tableName);
                  startRow=tableStart.getRow();
                  startCol=tableStart.getColumn();

                  Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);               

                  endRow=tableEnd.getRow();
                  endCol=tableEnd.getColumn();
                  tabArray=new String[endRow-startRow-1][endCol-startCol-1];
                  ci=0;

                  for (int i=startRow+1;i<endRow;i++,ci++){
                      cj=0;
                      for (int j=startCol+1;j<endCol;j++,cj++){
                          tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                      }
                  }
              return(tabArray);
          }
}




Attach the .xls document to the project which is shown below:





                                                                                                         Hemalatha.P