Friday, 20 February 2015

How to pass class Object through Dataprovider TestNG

A DataProvider in TestNG is a method in a test class, which provides an array of varied actual values to dependent test methods.(WIKI)

Check below how to pass class object to test Method using DataProvider.

Please note in Test method, passed parameter type should be of type of Object that you have added to your object array in dataprovider method.


 @DataProvider(name = "dataForTest")
       public static Object[][]  data() 
    {
       Object[][] data = new Object[1][1];
        Dummy dum = new Dummy("DummyName ", 246);
        return new Object []{dum};
      }

public class testDataProvider {
    
    @Test(dataProviderClass = DataProvider.class,dataProvider = "dataForTest")
    public void testMethod(Dummy Dumb) {
        System.out.println("Dumb Name" +"  " +  Dumb.getName() +"   " + " Dumb Salary " + Dumb.getSalary());
    }
}

Here is my Dumb class

public class Dummy {
    private String name;
    private int salary;
    public Dummy(String name, int salary){
        this.name=name;
        this.salary=salary;
    }
    public String getName() {
return name;
    }
      public int getSalary() {
return salary;
    }
   }

One must Remember Point about test iterations

Object[][] data = new Object[1][1];
With above data ,your test will run 1 time and will take one parameter

Similarly
Object[][] data = new Object[2][1];
and this one will run 2 times and will take one parameter.





Tuesday, 10 February 2015

Reading CSV file using Java Code

CSV:CSV is a comma separated values file, which allows data to be saved in a table structured format. CSVs look like a garden-variety spreadsheet but with a .csv extension (Traditionally they take the form of a text file containing information separated by commas, hence the name).(Source Wiki)

If someone opens CSV with notepad or textpad or some other editor it may look clumsy and difficult to read,Its always a good idea to open it using MS Excel for easy formatting and readability.


Below is the Java code to read from CSV file, herein we are trying to read each line from csv file, splitting it by delimiter comma and saving each item in a String array.

CSVFile.csv
This,is,code,for,reading,csv,file
this,is,first,line
this,is,second,line
this,is,third,line



****************************************************************************************

package test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;


public class ReadCSV {
  public static void main(String args[])
   {
   
  ReadCSV test = new ReadCSV();
  test.readCSV();

    }
  public void readCSV()
  {
  String filename = "C:\\CSVFile.csv";
    BufferedReader csvFileReader = null;
    String line = "";
    String[] readLine;
  try {

  csvFileReader = new BufferedReader(new FileReader(filename));
  int k=0;
  while ((line = csvFileReader.readLine()) != null) {
  
  System.out.println("Line" + "  "+ k + "  " + "Starts from here" + "*******************");  
      
         readLine = line.split(",");
         for(int i=0; i< readLine.length;i++)
         {
                     
          System.out.println("readLine"+ "[" + i + "]" + "  "+ readLine[i]);
          
         }
         System.out.println("*********************************************");
         k++;
  }

}
  
  catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (csvFileReader != null) {
         try {
          csvFileReader.close();
         } catch (IOException e) {
               e.printStackTrace();
         }
  }
}
  }
}


==============================================


Output:
Line  0  Starts from here*******************
readLine[0]  This
readLine[1]  is
readLine[2]  code
readLine[3]  for
readLine[4]  reading
readLine[5]  csv
readLine[6]  file
*********************************************
Line  1  Starts from here*******************
readLine[0]  this
readLine[1]  is
readLine[2]  first
readLine[3]  line
*********************************************
Line  2  Starts from here*******************
readLine[0]  this
readLine[1]  is
readLine[2]  second
readLine[3]  line
*********************************************
Line  3  Starts from here*******************
readLine[0]  this
readLine[1]  is
readLine[2]  third
readLine[3]  line
*********************************************




Saturday, 19 October 2013

Selenium WebDriver

WebDriver is a web automation framework which allows us to execute tests against different browsers.We can use any programming language(Java,.Net,PHP,Python,Perl,Ruby) to write test scripts.

we can use conditional operations like if-then-else or switch-case

Lets see few similarities between WebDriver and Selenium RC
Both allows us to use programming language in designing test .
Both allow us to run tests against different browsers.



Selenium RC’s architecture is bit more complicated.
We need to launch a application called Selenium Remote Control Server before starting testing
Selenium RC works only using JavaScript for its every command. That means that everything you write is eventually translated into Javascript and run in the browser.
The browser will obey the instructions of Selenium Core, and will relay its response to the RC Server.
The RC Server will receive the response of the browser and then display the results.
RC Server will fetch the next instruction from your test script to repeat the whole cycle.

However WebDriver interacts directly with browser and uses the browser’s own engine to control it,that is what makes webdriver faster.


Remember that WebDriver operates on the OS level. Also remember that different browsers communicate with the OS in different ways. If a new browser comes out, it may have a different process of communicating with the OS as compared to other browsers. So, you have to give the WebDriver team quite some time to figure that new process out before they can implement it on the next WebDriver release.
However, it is up to the WebDriver’s team of developers to decide if they should support the new browser or not.


Note that while Selenium RC has been oficially deprecated, the WebDriver is now being developed rapidly and it still suffers from several child-illnesses and is not in its full strength. That said, using WebDriver, you can do anything Selenium RC can do. And sometimes more. With an occasional minor bug.


I will recommend the following link for any one who is trying to learn webdriver
Seleniumhq.org

Monday, 8 April 2013

Test Automation with VSTS


UITest Framework Architecture: How Test Automation Works in VSTS 2010
We have been using VSTS CodedUI Tests for over 3 years now for functional regression testing and have been intrigued by this complex yet simple tool. Yes you read it right complex yet simple because it must have a very complex architecture to support so much in a single tool yet for the user it is very simple and a breeze to work. The mechanics behind any test automation tool are very intricate yet very interesting. The basics remain the same across the tools with differences in the architectural details.Let’s take a plunge into the architectural details of UITest Framework that the testing components of Visual Studio use and understand how an automation tool works. Let’s have a glance at the architecture of CUIT Framework:

  

Let us go through the various blocks one by one and try to understand their significance starting from the plug-ins.
1.       Plug-ins / Technology Adapters: A plug-in or a technology adapter is a module that understands the corresponding User Interface technology and provides the UI technology specific services to rest of the modules. The role of a technology adapter is to understand the technology for which it is designed and provide services to the rest of the layers especially the abstraction layer. For example, to record/playback user actions on IE we have the Web Plug-ins (MSHTML/DOM) that understands the technology on which IE is based (i.e. MSHTML/DOM). It can thus communicate with IE and the automation tool thus providing a communication medium between the two thereby enabling the record and playback services.
2.       Abstraction Layer: Next up is the abstraction layer which helps abstract rest of the code from various technologies. The abstraction layer has a very important role to play when supporting multiple technologies. This layer sits between the plug-ins and rest of the modules. The record and playback engine speaks to the abstraction layer which makes the engine independent of the technology being automated. The abstraction layer translates everything coming from the plug-ins and feeds the test engine with the input that it can understand and also send instructions back to the plug-in for playback.
3.       Recorder and Playback Modules
Recorder: The recorder first records the raw steps (user actions) and based on the filter\aggregation rules, these raw steps are converted into filtered steps or user-intention.
-          Filter rules are the rules based on which the recorder can filter out any unwanted/unintended actions like back-spaces pressed while typing into and edit-box etc.
-          Aggregation rules are used to club multiple user actions into a single step wherever applicable. Eg. Going to start menu, launching IE and typing URL in the address bar can be aggregated into a single step as it can be performed in a single step while playing back the recording. This is also called as Intent Based recording.
Playback:  The playback module has a rich set of public APIs for the users that they can use to write robust tests. The APIs can be used to interact with the AUT in many ways like performing click action on a button or a hyperlink or selecting an item from a drop-down list. It also has property provider which gives information on properties supported by each control in the AUT and browser services for browser specific operations like navigate to URL, clear cache etc.
4.       The two clients that are available as of today sit on the top layer.
-          Test Runner: The Test Runner uses the UITest framework to do Fast Forwarding for manual tests. The Test Runner interprets (using the interpreter module which actually forms a part of the Test Runner) the recording on the fly and calls appropriate API on the playback side to perform the user actions on the UI of AUT.
-          Coded UI Test (CUIT):The Coded UI Test which effectively is a Visual Studio client generates code out of the recording performed by the Recorder module. It uses information provided to it by property provider for each control to create definitions for the controls (in the AUT) and add appropriate API calls to replicate the user actions performed during recording session. These properties are used to identify the controls in the AUT during playback session. The users can alternatively hand-code the entire CUIT using the rich set of public APIs.
To summarize we can generalize the above discussed components and understand how automation tools work in general (of course the implementation/architectural details will remain different for different tools). Happy Automation!

Friday, 15 March 2013

Testing using PERL


PERL:Practical Extraction and Reporting Language.

There are lot many things you can do with PERL while testing your application, 

PERL is great language to play with (my personal choice), trust me you will again fall in love with your work once you start working with PERL.

In this section, tester can find how effectively and efficiently they can use perl for their testing their applications.

Please see, i am new blogger and my only purpose here is to make Testing more intresting and
All things i am putting here is based on the things i worked on.

I will keep updating  this blogs with more things on PERL .

If you are testing a client server application, You might be interacting with server logs, to check what request client is sending and what response server is sending back to client.

Some times logs are too big to go through all of it and checking for some special message.

You can obviously use some Editor's search option but that  will be tedious job to do.

Here, i will first show you how to find a particular pattern/text in log file.

*********************************************************
#PERL script to find a text/Pattern in log file


$Han = "handler";

open(Han,"logs") or die "Unable to open logfile:$!\n";
#Here logs is the name of file you want to open.
while(<Han>){
print if /\bMSG\b/i;
# searching for text "MSG"  in our log file.
}
close(Han);
************************************************************
copy -paste the above code in text file and name it as per your choice(i am giving search.pl) with extension for file as ".pl"
The output you will get will show  all lines containing text "MSG". 

Now if you want to modify it more, so that your output should show how many times that text is present , you can use like this:

******************************************************************
#PERL script to search for a text/Pattern and print the number of occurrences of text/pattern


my $val = "filename.trace";
chomp ($val);
my $count=0;
open (FilHan, "$val") || die "wrong filename";
while ($val = <FilHan>)
{
  while ($val =~ /\bError\b/ig)
  {
        ++$count;
  }
}
print "Total instances present: $count\n\n";
close (FilHan);



Output will be:
Total instances present: 489
****************************************************************


Lets see how we can test  webpage using PERL script, written below is the script that will open a webpage and insert your details.


#PERL script to open webpage and fill the login fields 
 use Win32::IEAutomation;
        # Creating new instance of Internet Explorer
        my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
        # Site navigation
        $ie->gotoURL('http://www.gmail.com');
        $ie->getTextBox('name:', "Email")->SetValue("give_your_username_here");
        $ie->getTextBox('name:', "Passwd")->SetValue("give_your_password_here");
        # Finding button and clicking it
        $ie->getButton('name:', "signIn")->Click;
  *********************************************************


Sometimes, during testing you need to get information from Server,It will be tedious to open putty and Telnet the server every time and execute the command , script below will help in executing the command onto server and get response,




**********************************************************
#PERL script to telnet a server and execute the command 
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die',
Prompt => '/\$ $/i');
$telnet->open('www.provideiphere.com');
$telnet->login('username', 'password');
print $telnet->cmd('who');


**********************************************************








Lets see how to compare two huge files and find out 
1).Common between two files
2).Unique in both files.


**********************************************************
#PERL script to find common items between two files and also those which are present in one file only.
@both = ();

@second = ();
open FH1, "firstfilename.dat" or die $!;
while(<FH1>)
{
# each line has become a key
$first{$_} = undef;
}
close FH1;
open FH2, "secondfilename.dat" or die $!;
while(<FH2>)
{
if(exists($first{$_}))
{
push(@both, $_);
delete $first{$_};
}
else
{
push(@second, $_);
}
}
close FH2;
#$, = "\n";
print "in Both\n";
print @both;
print "only in second file\n";
print @second;
print "only in first file\n";
print keys(%first);

**********************************************************




Please feel free to post your comments for any doubts.


happy testing :)


Keep watching for more stuff on testing with PERL