Thursday, January 7, 2016

Getting Started with FitNesse

Now the world is moving to Agile. Early and continuous feedback is imperial for any scrum team. Because the world is changing, the mindset of testers also needs to be changed.
Instead of “finding bugs, breaking software, measuring requirement”, testers are now thinking of “delivering the quality, right at first time, test without the UI or test even before the UI is available”.

Testers are also now required to respond to change, and hence it is important to come out of black box testing technique and not waiting until the UI is developed; instead, begin testing the intermediately deliverables as well.

 

But WHY?


“NOW THIS IS VERY AGILE PERSPECTIVE”.
Whenever we build software, the lowest layers of tests are kept at the unit/component level. Unit tests are carried out by the development team. These unit tests are very much technology oriented and mostly written in the same language in with the system under test is written.
These unit tests are written with “X unit” test tool. We say in the testing world that if our unit test is rock solid, defects are identified much earlier and the testing above the unit testing layer becomes easy in a stable environment. And when we talk in Agile, we say that if a team is mastered the art of TDD (Test Driven Development), the Unit level tests provide the quickest feedback.
The layer above the unit/component layer is the Acceptance tests layer which is carried out the business. These are functional tests have more coverage than the unit tests and are most often executed by the non-developers. These tests test the layer behind the presentation layer or the APIs. These APIs or methods when are tested, gives a quick feedback and by the time GUI is developed, most of the functionalities are tested.

FitNesse is an example of this Automated Acceptance Tests Layer.

 

What is FitNesse?

FitNesse is “A fully integrated standalone wiki, and acceptance testing framework”. It is Open source, wiki web server. Wiki- because it allows creating your own web pages on which test tables are created. These test tables are nothing but the test data.
Its intention is to support agile style of black box acceptance and regression testing. It’s also a collaboration tool because testers collaborate with developers to prepare the test suite.

 

Why should I use FitNesse?

Agile test team can use FitNesse to prepare test suits that will test the methods in the code. FitNesse is analogous to Junit in a way that it also tests the methods, but it’s different than Junit because the tests are in the form of simple tables which can be used by both developers and non-developers.

Benefits:
  • Early feedback, by executing the automated acceptance tests as often as required.
  • Tests results are deterministic because they are highlighted in either Red or Green.
  • Test data can be designed to suite the quality needs.
  • The tests are written in simple language and easy to understand as they are written in tabular form.
  • These tables are defined in terms of input and expected outputs.
  • See all FitNesse features here.

 

So what all can I create?


In FitNesse you can create Tests and Suite. The terms are very much the same as used in the testing world. Tests are single script and suit is collection / group of tests. When you create a suit and execute it, the advantage is that all the tests in that suit are executed. So you need a proper planning to arrange your tests in suit.

 

Downloading and Configuring FitNesse:

=> To download FitNesse, Click Here


Download the latest version of fitnesse-standalone.jar and save it to your local drive.





Open command prompt and execute the jar file. For ease, I have created a batch file:



Once the jar file is executed, FitNesse is started as shown below: (click on image for enlarged view)
To open FitNesse, open your browser and type: http://localhost:<portnumber>. In this case the port number is 2222.

The page received is shown below: (click on image for enlarged view)





So here, if you can see the Tests dropdown, we can create a “Suite page” as well as a “Test Page”. When you create a suite, all the test scripts within that suite will be executed.
For explanation purpose, I am taking an example of Test Page.

FitNesse Example – The stuff to Test:

As of now, we are testing a simple calculator program shown below.
Here is the code in java, which has 4 methods:
  • addition ()
  • minus ()
  • multiply ()
  • divide ()
(Please see that FitNesse works with any language of your choice. For explanation, I have used java)
This code in FitNesse world is called “Fixture”.
Fixtures are nothing but the sample code – or a link between FitNesse and the application under test. So whenever we want to test a method, we have to write a fixture and this fixture will invoke, and test the method.

So the “Fixture” code for our example is as follows:



package calc;

public class Calculator {

   

    private int first,second;

     public void setFirst(int first)

    {

    this.first=first;

    }

   

    public void setSecond(int second)

    {

    this.second=second;

    }

   

    public int addition()

    {

    return (first+second);

    }

   

    public int minus()

    {

    return (first-second);

    }

   

    public int multiply()

    {

    return (first*second);

    }

     public float divide()

    {

    return (first/second);

}

    }








We would need the class file, so make sure that you compile it.

Writing your test in FitNesse:

Step #1) Let’s go back to the browser where we have the FitNesse front page.
In the front page, click on “Test Page”, enter the name of the test and click on “Save” button. In our case, it’s “Calculator”







Step #2) In your URL, append the name of your test with a dot “.” Operator.
Like: http://localhost:2222/FrontPage.Calculator



 Step #3) Click on the Edit button and enter the lines shown below
 




Here are the Lines entered:

!define TEST_SYSTEM {slim}
!path D:\Lifefray\FitnesseTests\bin
!| import |
| calc |
| calc.Calculator |
| first | second | addition? | minus? | multiply? | divide? |
| 4 | 2 | 6 | 2 | 8 | 2.0 |
| 10 | 5 | 15 | 5 | 50 | 2.0 |
| 10 | 10 | 20 | 0 | 100 | 1.0 |



Let’s understand the lines one by one.

a) The first line says FitNesse to use SLIM test system.
(SLIM – Stands for Simple List Invocation Method. By saying SLIM test system, all the table processing is done by FitNesse. SLIM has SLIM Runner and SLIM Executer. SLIM Runner breaks the test pages into simple instructions and these instructions are passed to SLIM Executer which directs the fixture code to call the system under tests)

b) The second line defines the location of the class file. In this case, the java code is compiled and the class file is kept at location “path D:\Lifefray\FitnesseTests\bin

c)The third & fourth lines states importing of package name "calc" . Fifth line is the name of the Class with Package name. In our case class name is “Calculator

d) Now comes the sixth line:
The first two columns |first|second| are the parameters or the inputs to the java method.
The next 4 columns which are followed by “?” addition?|minus?|multiply?|divide?| are the methods in the java class. These methods will return a value which would be compared against the expected values.

e) The lines:
|4   |2     |6       |2     |8       |2.0   |
|10  |5     |15       |5     |50     |2.0   |
|10   |10   |20       |0     |100     |1.0   |
Are the test cases or I should say Test data for our method.
The first line:
|first|second|addition?|minus?|multiply?|divide?|
|4   |2            |6            |2            |8                |2.0   |
Will take 4 as first parameter and 2 as second parameter and will pass these two values in the addition method of the java class. The method will execute and will return a value. This returned value will be compared against the expected value written under the “addition?” which is |6             |
In the similar fashion, FitNesse will pass the first 2 parameters in the minus? Method of the java class and returns a value. This value will be compared against the expected value against |2         |
In the same way multiply? and divide? will work by taking the values of the first and second parameter and returns value which are compared against |8           |2.0   | respectively
In the similar fashion, the below 2 rows (or I should say, the test cases) are executed.
|10   |5     |15       |5     |50       |2.0   |
|10   |10   |20       |0     |100     |1.0   |


Step #4) Once you have edited your tests, click on the save button and your page will look like:



Step #5) To run the tests, Click on the Test button, and we get the result as follows: (click on image for enlarged view)





For the first row (which is our first test case), the green color highlights that the values, returned from the method addition (), minus (), multiply () and divide () are matching with what is expected i.e. 6, 2, 8 and 2.0 respectively. Similarly for the second row (which is second test case) all the values returned from the methods are matching.

Step #6) Now to demonstrate, let me change few of the expected values to some other values (the values are incorrect, but I have done it purposefully for explanation)


As of now I have:
  • Changed the expected values for minus() for the second test case
Step #7) Run the test by clicking the “Test” button. The above tests should fail. (click on image for enlarged view)

The red color highlights that these tests failed.

Some insights on Fixture/Table styles:

We have seen that In FitNesse the tests are executed by executing rows in a table. Hence to execute different kinds of tests (or I should say to test different kinds of methods), we would need to use different kinds of tables. We use the below Fixture/table styles most often:
  • Column Fixture – is most widely used (and is used in the above example). Here the rows of data represent different sets of input and its expected output.
  • Row Fixtures – It is used for testing queries which returns some set of values.
  • Action Fixtures – It is used to run tests for sequence of events. These events can be like clicking on a button, checking values

 

Recommendation:

I have tried to demonstrate the concepts, so that we can start exploring more on FitNesse. The tester’s mindset also needs to be changed and have to be broadened. We have to stop restricting ourselves to look inside the code. I feel; ultimately we are testing the code, so why don’t we try to see the code and test then and there?
Start sharpening your programming skills and emphasis more on building the logic and rather learning syntax. Once you are well versed with programming concepts and have practice on implementing it, exploring FitNesse will become easier.

 

Conclusion:

Testing in agile comes in 4 flavors:
  1. Automated Unit testing – By using Junit
  2. Automated acceptance verification test – By using FitNesse
  3. Automated UI / regression tests – by using Selenium or QTP
  4. Manual testing

We should try to push maximum of our testing in the unit and the acceptance layer. Till now we have been trying to keep most of our testing for the UI layer using tools like QTP and Selenium, but the disadvantage here is that the these functionalities could not be tested unless the UI is developed. By the time you find a defect, the developers have moved into some other feature development.
On the other hand, if we can test the APIs soon after it has been written, developers can fix it instantly. This would also result in less effort when we test the GUI. Because all the functionalities are tested, testing the GUI becomes easy.


With Agile, the mindset of testers also needs a change and they have to come out of their routine set of testing and now you should look inside code and try to identify defects even the UI is not available.





Note - Above article published first appeard in http://www.softwaretestinghelp.com. Some updates done from my side i.e Configurations changes, code changes with new screenshots which will help for new users without any errors or minimum errors.

Thursday, May 28, 2015

Running your first Selenium Webdriver script with TestNG


Running your first Selenium Webdriver script with TestNG

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand, which will help make your tests easier to read and maintain. It's not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old "main" method.

You can use any IDE for developing your scripts but I like using Eclipse. I will be explaining later how to use Eclipse.

Ok...so lets start creating the
Selenium Webdriver script using TestNG. You will need to install the following first.

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

2. Selenium Webdriver: Download it from here: 
http://seleniumhq.org/download . Extract the files in a folder named Selenium_Webdriver  





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

4. 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'.

5. 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.

Good that we have finished installing everything that we needed to start with selenium and TestNG. You could always work with selenium and testng without eclipse in which case you wont have to install Eclipse and the TestNG plug-in required for eclipse. But its better to have an IDE (like Eclipse or anyother) so that its far easier to write the scripts.

Let's start with the Selenium Webdriver and TestNG script now:

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

2. Add the required jars. Select the project-’Automation’, Right click Build Path->Configure Build Path. Click Libraries->Add External Jars. You will have to add the testng and selenium jars and libs.Also Add the In build TestNG , Junit , Java Libraries from Add Library.

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

4. Create a new java program. Select the package-'First' and then right click New->Class. Enter the name as say ‘GoogleTC’.Press Enter.
5. Copy paste the whole below code into eclipse(GoogleTC).

package First;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class GoogleTC {
     
      public WebDriver driver;
     
     
  @Test
  public void f() throws Exception {
        driver = new FirefoxDriver();
        driver.get("http://www.google.com"); // open google.com
      Thread.sleep(2000); // This statement is used to wait for 2 seconds
      driver.manage().window().maximize(); //Maximize the window
      Thread.sleep(5000);
      driver.findElement(By.id("lst-ib")).clear();
      driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
      driver.findElement(By.name("btnG")).click();
      Thread.sleep(2000);
      driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();
             
      driver.close();
      driver.quit();
  }
}



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

Great!!!!! Your first testng
 script is ready to be run. Wasnt that easy?
This is simple script which search keyword Selenium in google.

7. 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. 

Tuesday, January 20, 2015

Data Driven in Selenium Webdriver

 


Data driven in Selenium webdriver . Read Data from excel sheet and fill the username and password of gmail.


Source code:

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class EmailTest {
private WebDriver driver;

@BeforeClass
public void Startup() {
driver = new FirefoxDriver();
}

@Test(description = "Read Gmail account")
public void Login() throws Exception {
FileInputStream fi = new FileInputStream("E:\\testemail.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
driver.get("http://www.gmail.com");
try {
for (int i = 1; i < s.getRows(); i++) {
// Read data from excel sheet
String s1 = s.getCell(0, i).getContents();
String s2 = s.getCell(1, i).getContents();
driver.findElement(By.name("Email")).sendKeys(s1);
driver.findElement(By.name("Passwd")).sendKeys(s2);
driver.findElement(By.name("signIn")).click();
Thread.sleep(9000);

}
} catch (Exception e) {
System.out.println(e);
}
}

@AfterClass
public void teardown() {
driver.quit();
}
}





Monday, January 12, 2015

Display Alert Message of Test case

We can display alert message in screen to know which test case is executing or to show user of respective action.



Method to display alert Message of test case -

public void Show_ScriptMessage_Close(String Message , int time) throws Exception
  {
     
      time = time * 1000; // 1 value to 1000
       final ImageIcon icon = new ImageIcon("C:\\Selenium\\Test_data\\prathip.png");
        final JOptionPane optionPane = new JOptionPane(Message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, icon , new Object[]{}, null);

        final JDialog dialog = new JDialog();
        dialog.setTitle("Selenium Scripting - Prathip");
       
        dialog.setModal(true);
       
        dialog.setLocationRelativeTo(null);
        dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - dialog.getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - dialog.getHeight()/2);
        dialog.setContentPane(optionPane);

        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
         
     
        dialog.pack();
       
      //create timer to dispose of dialog after 5 seconds
        // 1 sec = 1000
        Timer timer = new Timer(time, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                dialog.dispose();
            }
        });
        timer.setRepeats(false);//the timer should only go off once

        //start timer to close JDialog as dialog modal we must start the timer before its visible
        timer.start();

        dialog.setVisible(true);
     
  }




how to call the function -

Show_ScriptMessage_Close("Google Search" , 5);


Saturday, December 13, 2014

Run Selenium IDE Script in Different Browsers using WebDriver Playback

Run Selenium IDE Script in Different Browsers using WebDriver Playback


WebDriver Playback is a newly introduced feature in Selenium IDE. It allows users to run Selenium IDE tests in multiple browsers. Thus increasing usefulness of Selenium IDE as test automation tool. This article explains how we can use WebDriver Playback feature and run our Selenium IDE script in different browsers other than Mozilla Firefox.
We will try to run a Selenium IDE test script on Google Chrome browser. Of course, you can run your test on other browsers such as Internet Explorer, Opera, Android, iPhone, iPad etc. For that, you’ll just have to replace the browser name in WebDriver Playback tab. We’ll see this in our example steps. For now, let’s follow below steps to run Selenium IDE script on Google Chrome.
  • Open Selenium IDE.
  • Go to Options–>Options–>WebDriver tab.
  • Check the option to ‘Enable WebDriver Playback’ from opened tab.
  • Provide ‘chrome’ value in text box and click OK button. We can specify browser name of our choice in the text box. Browser names are given in ‘Browser choices’ text displayed below this text box. Your options dialog should look similar to below image after making these changes.


  • Restart Selenium IDE. Restart is required for the changes to take effect.
  • We will need respective browser driver(in our case, Chrome driver) along with Selenium Standalone JAR file. You can download them from here and here. Once you have these files, place both of them in same folder.
  • Now open your Command Prompt and navigate to the directory where driver and JAR files are stored.
  • Run following command from Command Prompt to start Selenium server and Chrome Driver server. Use respective driver name if you are using a different browser.
java -jar selenium-server-standalone-2.43.1.jar -Dwebdriver.chrome.driver=chromedriver.exe

  • Now, open your Selenium IDE script and run it as you usually do.
  • You should see your test script getting executed in Google Chrome browser.
Please note that the browser will remain open after test script is executed. You will have to manually close it.
As seen from the above example, we can use WebDriver Playback to easily run our Selenium IDE tests in browsers other than Firefox. This feature adds more flexibility in Selenium IDE which can be of great use to all Selenium users. We hope you would find the WebDriver Playback feature useful. Let us know in comments how it goes for you.