Showing posts with label Selenium and FitNesse. Show all posts
Showing posts with label Selenium and FitNesse. Show all posts

Monday, January 2, 2012

FitNesse and Selenium

When we create our Selenium tests in Java normally we use JUnit or TestNG frameworks to execute our test scripts. We can also use FitNesse tool as a framework for our Selenium tests. FitNesse can be used when we have the below scenarios:

  • When we need to pass number of parameters to the script
  • When we need to run the same script with different parameters
  • When we need to run the same script with different URLs as parameters (functionality will be same).

You might have this question running in your mind!! Can't we use JUnit or TestNG to achieve the above things. Answer to this question will be, YES, we can use. But, we need to go for the concepts like Data Providers and JXL (Read data from Excel sheet) while using JUnit or TestNG.

When we use FitNesse, we can simply achieve the above things without Data Provider and JXL. Also, we doesn't require any technical person to execute our scripts. Once the test scripts are configured with FitNesse, any one can execute the scripts from there.

Just to give a try, follow these steps:
  1. Download FitNesse from here.
  2. Once its downloaded, create a folder called FitNesse in C or D drive and move the downloaded file(fitnesse.jar) into this folder. 
  3. Go to command prompt and open this folder C:\FitNesse
  4. Type java -jar fitnesse.jar and press enter. FitNesse jar file will be unpacked and you will find a additional folder called FitNesseRoot which will be having all the supporting files
  5. Again type java -jar fitnesse.jar and press enter. This time your FitNesse server will be started. If it ask you to choose the different port, use -p argument with the above command: java -jar fitnesse.jar -p 8081
  6. Now open your browser and go to http://localhost. You will see the FitNesse environment.
  7. You will see some introduction notes on the home page. Click on Edit from left navigation and remove all the notes and configure your Java test scripts and jar files. You need to include the below things: 
    • !contents
    • !path C:\Fitnesse\FitNesseRoot\files\classes
    • !path C:\Fitnesse\lib\*.jar
  8. Make sure you have created the folder called "classes" under C:\Fitnesse\FitNesseRoot\files location and included all your test scripts (Java class files). Also make sure you have created lib folder under C:\FitNesse and included all the necessary jar files: FitNesse.jar, selenium-java-client-driver-2.x.jar, selenium-server-standalone.jar
  9. Once you have configured your test scripts and jar files, click on save to update the changes. Now you can only see your test script and jar file configuration paths on your FitNesse home page. Whenever you open FitNesse, you will see this path.
  10. Now click on Add Child on the top of the home page to create a Test Suite and Test and enter a name. Make sure you have minimum two words in the name and each word should start with capital letter. For Example, TestSuite - SampleSuite and Test - SearchCheck.
  11. Now you can see your suite on the home page. Click on that, you will find your Test. When you open that, you will get Edit option in the left navigation. Click on that and call your test script. Follow the below format: 
    • !|package name.class name|
    • |parameter name|method name|
    • |parameter value|Expected Outcome|
  12. For Example, if you have created a class SampleClass under the package Sample in Java. Assume that you have a test method "firstMethod()" and a parameter "searchString"
    • !|Sample.SampleClass|
    • |searchString|firstMethod()?|
    • |Sachin|True|
  13. Remember that we are passing the value for the variable searchString from FitNesse, but the variable should be declared in your java code. 
  14. If you want to pass number of parameters, you need to include as follows:
    • !|Sample.SampleClass|
    • |searchString|firstMethod()?|
    • |Sachin|True|
    • |Yuvraj|True|
    • |Dravid|True|
    • |Dhoni|True|
  15. After adding all your parameters, save the changes and click on Test on the left navigation. But, make sure you test script is created and the class file has been stored in this location: C:\Fitnesse\FitNesseRoot\files\classes
  16. I have created a test script with Same class and test method. You can try this example: 
package Sample;

import com.thoughtworks.selenium.DefaultSelenium;

import fit.ColumnFixture;

public class SampleClass extends ColumnFixture {
public DefaultSelenium selenium;
public String searchString;
public SampleClass()
{
selenium = new  DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
selenium.start();
}
public boolean firstMethod() throws Exception
{
selenium.open("/");
selenium.waitForPageToLoad("30000");
selenium.type("name=q", searchString);
selenium.click("btnG");
Thread.sleep(5000);
return selenium.isTextPresent("News for "+searchString);
}
}

Screenshot of FitNesse test environment: