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.

Tuesday 4 March 2014

Function to Create Log file

When we have developed automation framework, we also keeping execution log in it. Here I have given sample function for creating log file.

Function CreateLogFile
            Dim Curtime, LogFilePath, FilePath ‘Declaration part
            Curtime=Now() ‘This will give result like 1/1/2015 12:00:00 PM
            Curtime=Replace(Curtime,”/”,”-“)
            Curtime=Replace(Curtime,”:”,”_”)
Environment(“FilePath”)=”C:\QTP  ‘Creating Environment variable
Environment(“FilePath”)=FilePath
LogFilePath=FilePath & “Results\” & Curtime & “.log”
Environment(“LogFilePath”)=LogFilePath
Set Fso=CreateObject(“Scripting.FileSystemObject”)
Fso.CreateTextFile LogFilePath

End Function

Monday 3 February 2014

Reporter Object in QTP

In this post, we'll see how to customize QTP Test Result file. Test Result is automatically generated after every test run.


Reporter Object is used for sending information/message to the QTP test results. Following are the method/properties we can use with ‘Reporter’..


•    ReportEvent Method
•    Filter Property
•    ReportPath Property
•    RunStatus Property


ReportEvent Method


ReportEvent is commonly used method od Reporter and most of you must be already aware of this!

As the name suggest, it reports an event to the test results. Means, by using Reporter.ReportEvent, you can send information to QTP test result file.

Syntax:
Reporter.ReportEvent EventStatus, ReportStepName, Details

As we can see above, ReportEvent takes 3 arguments. Let's discuss these briefly-


EventStatus:  Status of the step i.e. Pass/Fail etc. It can be four types..


0 or micPass: Causes the status of this step to be passed and sends the specified message to the report.

1 or micFail: Causes the status of this step to be failed and sends the specified message to the report. When this step runs, the test fails.

2 or micDone: Sends a message to the report without affecting the pass/fail status of the test.

3 or micWarning: Sends a warning message to the report, but does not cause the test to stop running, and does not affect the pass/fail status of the test

ReportStepName: It is name of step. It can be any string value.

Details: It is also any string value. You should provide description of steps here.

Example of ReportEvent Method

The following examples use the ReportEvent method to report a failed step.

Reporter.ReportEvent 1, "Custom Step", "The user-defined step failed."
or
Reporter.ReportEvent micFail, "Custom Step", "The user-defined step failed."

You can leave last two arguments (ReportStepName and Details) blank as well but recommended.

Reporter.ReportEvent 1, "", ""
or
Reporter.ReportEvent micFail, "", ""


Okay friends! It was most important part of Reporter. Let's quickly discuss remaining three properties also.


Filter property

Using Filter property, you can instruct QTP about what type of event to display in result.

Syntax:
Reporter.Filter = Mode


What is the use of Filter property?

Filter is used if you dont want to display all types of events in QTP test result. For example you have 10 Reporter.ReportEvent statements in your test (5-
Pass and 5-fail). By default all 10 will be displayed in result. You can use Filter if you want to display only pass or only fail events in the result. One way to comment all unwanted type of Reporter statements, but what if you can do that by writing just one line in your code!


There can be four types of Filter modes..

0 or rfEnableAll: This is the default mode. All reported events are displayed in the Test Results.

1 or rfEnableErrorsAndWarnings: Only those events with a warning or fail status are displayed in the Test Results.

2 or rfEnableErrorsOnly: Only those events with a fail status are displayed in the Test Results.

3 or rfDisableAll: All events in the Test Results are disabled. No event will be displayed in result file.


Example of Filter Property: 

Reporter.Filter = 2
or
Reporter.Filter = rfEnableErrorsOnly



ReportPath Property


It simply retrieves the folder path in which the current test's results are stored.

Do not use this property to attempt to set the results folder path. Means it is used for 'getting the path' not 'setting the path'.

Syntax:
Path = Reporter.ReportPath


Example of RepoertPath Property

Dim Path
Path = Reporter.ReportPath
MsgBox (Path)

Above example uses the ReportPath property to retrieve the folder in which the results are stored and displays the path in a message box.


RunStatus Property

It retrieves the run status at any particular point during the run session. For tests, it returns the status of current test during the test run.

Syntax:
Reporter.RunStatus

Example of RunStatus Property

If Reporter.RunStatus = micFail Then
Msgbox "Run Status is fail. Exiting from Action iteration."
ExitAction
End If


Above example uses the RunStatus property to retrieve the status of the run session at a specific point and exit the action if the test status is fail. If the test status is not fail, the test run continues.

In case of any queries/suggessions, please post your comments.