Wednesday 4 April 2018

How to use a same TestNG data provider to multiple test methods with different sets of data

There are many times different TestNG data provider required in a Selenium script, sometimes the same set of data & sometimes different sets of data. There is a way to use a same TestNG data provider with different sets of data and by doing this we can prevent redundant code. 

Let's say we need data provider for "LoginTest" and "RegistrationTest" and both would require different sets of data. To do so we can use below code to use same data provider with different sets of data.


@DataProvider(name="sampleDataProvider")

//When sampleDataProvider is called in any test method, Method m will automatically pass a name of that method as a reference value
    public static Object[][] abcData(Method m){
        Object data [][]=null;
//Data for LoginTest
        if(m.getName().equals("LoginTest")){
            data=new Object[1][2]
            data[0][0]="Username1";
            data[0][1]="Password1";
        }
//Data for RegistrationTest
        else if (m.getName().equals("RegistrationTest")){
            data=new Object[2][3];
            data[0][0]="emailAddress1";
            data[0][1]="Username1";
            data[0][2]="Password1";

            data[1][0]="emailAddress2";
            data[1][1]="Username2";
            data[1][2]="Password2";

        }
        return data;
    }

Using above code we can create a single data provider for all the Selenium scripts.

Friday 12 September 2014

A Walk-through to Selenium

Introduction to Selenium
·         Selenium is an open-source testing framework for web applications. It is used to automate the web applications.
·         Selenium is compatible with the following browsers
    • Firefox
    • IE
    • Chrome
    • Safari
    • Opera
·         Selenium works with following Programming Languages
    • C#
    • Java
    • Perl
    • Python
    • Ruby
Selenium-Components
·         Selenium - IDE
·         Selenium – RC
·         Selenium Webdriver
·         Selenium Grid
Selenium IDE
Ø  Selenium IDE is a plugin to Firefox.
Ø  It is a tool to record and playback tests (like Win Runner, QTP).
Selenium RC (Remote Control)
Ø   Test tool that allows you to write automated web application UI tests in  any programming language against any HTTP website.
Ø  Support multiple browsers.
Selenium Grid
Ø  Selenium Grid provides a hub allowing the running of multiple Selenium tests concurrently on any number of local or remote systems, thus minimizing test execution time.
Why Selenium Webdriver or RC , not IDE?
Ø  The main limitation of Selenium IDE is that it is supported in only Firefox browser
Ø  Selenium RC  and Webdriver Supports Multiple Browser
Ø  Selenium RC also has its limitation that it needs to start selenium server before starting  running the test.
Ø  So to overcome all these issues a new version was introduced “Selenium Webdriver”.
Selenium Webdriver requirements:
To start up with automation using selenium, we need IDE like Eclipse to write the scripts. Below are the initial steps to do.
Step1: Install Java on your computer.
             Download and install the Java Software Development Kit (JDK) .
Step2: Install Eclipse IDE.
             Download as a Zip file.
             Inside the Zip file, there is an “Eclipse” folder.   
Step3: Download Selenium-Server-Standalone-2.25.0 jar
Step4: Configure Eclipse with selenium webdriver.
Configuration Eclipse-Selenium Webdriver
Ø  Launch the "eclipse.exe" file inside the "eclipse" folder.

Ø  It will ask to create workspace.




Ø  Create a new project through File > New > Java Project.
Ø  Name the project as “seleniumproject".
Ø  Right-click on the newly created project and select New > Package, and name that package as “seleniumpackage“
Ø  Create a new java class under “seleniumpackage” . Right click the package, and then select New > Class and name it as “seleniumclass”.
Ø  Once you completed all the above steps, your Eclipse IDE will look like













Ø  Import the selenium standalone jar to the project
Ø  Right click on the project > Bulid path> configure Build path > Libraries >Add External Jars .


Sample script with Selenium Webdriver:
Ø  To Open Google Page:

Selenium Webdriver API:
Ø  In the above script “driver.get” is one of the API’s of webdriver.
Ø  API Means “Application program Interface”.
Ø  API is a set of protocols, routines and tools for building software application.

  SOME IMPOTTANT API:
Ø  driver.get
Ø  driver.findelement
Ø  By.id, By.linkText…
Ø  driver.click
Ø  driver.sendkeys

Let’s see how to search something in google:


Ø    In the above script “driver.findElement(By.id("gbqfq")).sendKeys("selenium for beginners");” “gbqfq” is a locator.
Ø  So, What is Locator?
Ø  Locator is HTML element to find and match the elements of your page that it needs to interact with.
Ø  How to find the locators.?

Find Locator:
Ø  Open your application Ex: Google
Ø  Press F12.

Monday 24 March 2014

How to get current browser URL in QTP

There are two ways:
 1.  Using GetROProperty("URL") method (run-time object property)
       2. Using "URL" property of "Object" (IE DOM Object)
Using GetROProperty("URL") method (run-time object property)

GetRoProperty function reads properties of a run-time object in an application.

So, for example:

  • To check whether a link is visible or not, use:
    Browser("bname").Page("pname").Link("lname").GetROProperty("visible")
  • To get a page's title, use:
    Browser("bname").Page("pname").GetROProperty("title")
  • To check whether a window is maximized, use:
    Window("wname").GetROProperty("maximized")
In our case, to get URL of the current WebPage, we will use:
Browser("bname").Page("pname").GetRoProperty("URL")

"URL" property of "Object" (IE DOM Object)

The sample syntax is:

Browser("bname").Page("pname").Object.URL


Tuesday 18 March 2014

Actions vs Functions in QTP

There are few pros and cons in both approaches but I prefer functions over actions.
Some key points when using Actions are:

·    Action parameters can’t accept complex values like arrays or object.
·          Actions allow us to have optional parameters.
·         A blank Action with no object and no code occupies 150+KB of spaces, so using too many Actions increases the size of scripts.
·         Re-usable Actions, when called in test are read only. So if, there is an issue in running the test and the Action’s code need to be updated for fixing it, we would need to close the test and re-open it containing the re-usable action. After making changes to the Action code we need to open the test again for execution and in case it fails we must repeat the procedure for fixing the code.
·         Actions create a separate section in the test results summary making it easier to differentiate functionality.
      Compared to Actions, Functions have few advantages:
·         Functions don’t take much space since they don’t have associated Local OR or DataTable.
·         Functions can be edited easily as compared to external actions. This make maintenance much easier.
·         Functions parameters can take any possible value supported by VB Script.
·         Functions can easily be overridden by redefining them.
So, I prefer Functions because maintenance becomes huge when we have too many Actions.

Monday 10 March 2014

How to split string without using split function

Dim str,strRes,strchar
str="i am busy with work"
dim strarray()
Redim preserve strarray(0)

for i = 1 to len(str)

    strchar=mid(str,i,1)
    if strchar<>" " then
        strRes=strRes & strchar
    End if
       
        If strchar=" " Then
                  strsize=ubound(strarray)
                  Redim preserve strarray(strsize+1)
                 strarray(strsize+1)=strRes
                 strRes=""
        End If

        If len(str)=i Then
                strsize=ubound(strarray)
                  Redim preserve strarray(strsize+1)
                 strarray(strsize+1)=strRes
        End If
 
Next

for i=1 to ubound(strarray)
msgbox strarray(i)

next

Thursday 6 March 2014

Dynamic Synchronization in QTP

Dynamic synchronization allows waiting till the object property is changed or till time out.
Sample example:

Window("WinFlight").ActiveX("ThreedPanelControl").WaitProperty "text", "Insert Done...", 10000

The waitproperty can be inserted using Insert Synchronization point or we can manually add the waitproperty method by typing  typing it.
Apart from this we can also add synchronization point using following code also

Sample code: 1
Function ManualSyncPoint (Object,propertyname,propertyval,timeout)
Do
If Object.GetRoProperty(propertyname)=propertyval Then
            'if propertyname and propertyval match, come out of the loop and execute next step
           
                        Exit Do
           
            Else
                        Wait(1)
            End If
Loop while(i<=timeout)

End Function

In the preceding method we have used the static wait; we can implement it without using wait statement as well.

Sample code:2

Function ManualSyncPoint2(Object ,propertyname, propertyval, timeout )
sttimer = Timer
'Timer allows to get the number of seconds elapsed since midnight
(12:00 AM)
Do
If Object.GetROProperty(propertyname) = propertyval then
Exit Do
else
end if
endtimer = Timer
'The difference between the two timer objects provides the number of seconds we need to determine for timeout
Loop While ( int (endtimer-sttimer) <= -timeout)
End Function

Function that allows to wait till object exists
Sample code:3

Function WaitTillExists(Object, timeout )
loadtimer = Timer
Do
isObjectExists = Object.Exist
If isObjectExists = true Then
Exit Do
else
End If
completetimer = Timer
Loop While(completetimer-loadtimer <= timeout)
End Function

           
                       

            

Wednesday 5 March 2014

Different ways to add Test Object to Object Repository

There are multiple ways to add test object to Object Repository in QTP.

1.    Record a test step.
2.    Add object to local.
3.    Create a test object by using define new test objects.
4.    Creating an Object Description.
5.    OR Manager Navigate and Learn or Add object option.
6.    Adding test objects to local object repository from Active Screen.

Recording a test step

Test objects are created automatically and added to the local OR when a user records
a step. Add object to local.

Adding a test object to OR

Add a test object to the local OR through the following steps:
1. Navigate to Resources | Object Repository | Add object to local.
2. Click on the button Add Object to local.
3. Click on the hand pointer to the object we want to add from AUT
4. Click on OK.
Create a test object by using define new test objects

To define a new object, we should know its class and its identification properties.
Perform the following steps:
1. Navigate to Resources | Object Repository | Define New Test Object.
2. Filter Environment from the Define New Test Object window.
3. Select the class from the Class drop-down box.
4. Enter a logical name.
5. Provide the value for the Identification property (1 to n).
6. If required, add some identification properties.
7. Click on the Add button to add a test object to the OR.

Creating an Object Description

The test object can be created without recording, by providing an object description.

We can use the object description in the test steps directly, as shown in the following
code:
Window("regexpwndtitle:=FlghtReservation").WinComboBox("attached
text:=Fly From:").Select "London"


OR Manager Navigate and Learn or Add object option

The Navigate and Learn toolbar

This toolbar allows you to add multiple test objects to a shared object repository
while you navigate through your application. Following are the steps to learn to
navigate the object in the object repository manager:

1. Navigate to Resources | Object Repository Manager | Object | Navigate
and Learn or press F6.


2. Select a window to learn. The selected window and its descendant objects
are added to the active shared object repository according to a predefined
object filter.

Adding a test object using the OR Manager Add Object option

1. Navigate to Resources | Object Repository Manager | Objects | Add
Object. Click on the hand pointer and select the window.

2. The object filter is used for the Navigate and Learn option as well as the Add
Objects option.

Adding test objects to the local object repository from Active Screen

Select the required object from Active Screen and add it to the object repository. In
order to add test objects to the object repository using Active Screen, Active Screen
should have the object. Perform the following steps:

1. Go to the Active Screen pane.
2. Select an object and right-click on it.
3. Click on View / Add Object.
4. Click on the Add to Repository button.