Monday, 5 January 2015

How to access the excel as a database using adodb.connection in QTP?

We can work with excel in 3 ways in QTP as mentioned below.

Using Excel.Application
Using Adodb.Connection
Using DataTable


To know how we can use Excel.Application to read or write the excel files, you can refer these links.

To know how to use datatable to load excel into it, use these links.

In this article we will see how we can read or write excel files using Adodb.Connection object.
Excel workbook is considered as a database. Each sheet in the excel workbook is considered as a table.
First row in the sheet is considered as column header and all other rows are considered to be records.

Here is the sample code to connect to excel database.

Set excelConnection = createobject("Adodb.Connection")
excelConnection.open "Data Source=c:\abc.xlsx;Provider=Microsoft.Jet.OLEDB.4.0"
Set rs = excelConnection.execute "Select * from [sheet1$]"
For i=0 to rs.recordCount-1
 For j=0 to rs.fields.count-1
  print rs.fields(j).name & rs.fields(j).value
 Next
Next

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

color of the link changes when hovered on that link, how do you get that changed color?

To do this you need to switch QTP replay mode to Mouse replay mode, fire an OnMouseOver event, get the color of the object using current style method, and then switch back the QTP reply mode Event Replay mode. Please look at the following code:

Dim oColor, oColorMouseOver

oColor=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 2

Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").FireEvent "onMouseOver"

oColorMouseOver=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 1

msgbox "Color of the object is: " & oColor & ". And the Color of the object on MoouseOver is: " & oColorMouseOver

 

You can also use this CurrentStyle Object method to get the FontSize, FontStyle, BackGroundColor etc., in the following way:

oFontSize= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontSize
oFontStyle= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontStyle
oBackGroundColor= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.backgroundcolor

Execute QTP from Command Line (DOS prompt)

When I first started working on Quick Test, I never knew the concept of executing QTP from the DOS prompt or the command line. Well even if I knew, I would not have understood where to implement this. I want all my posts in this blog to have practical applications rather than just explaining the concepts. I will try my best to achieve this "desire" of mine. Ok...so lets continue on the topic. I would like to give another term for this execution of QTP from the command line - Automating the Automation. Well yes, practically thats what this whole thing is. Lets dig more to see what I mean by this.
Usually we open QTP from our computer, record scripts and then execute them by clicking on Run or pressing F5 in QTP. So we automated a test scenario...didnt we ? So what is this "Automating the Automation" ?? Well, we did automate a test scenario but did you ever think that you still had to have a manual intervention to open the test case and actually click on the Run button !!!!! Aha....well we are going to automate even this step.... and here is where executing QTP from command line is going to help.
Well its true that we indeed saved a lot of time by automating the tests using QTP and most companies are OK with the time that you spend on opening the test from QTP and executing it. But there are cases where you do not even have that time to wait for manual intervention to execute the automated tests. I am sure you are thinking now as to what is that case....well let me explain a situation....
Lets say in the QA phase the below mentioned steps need to be executed in a particular order as part of the testing
Step 1: Execute some SQL queries or SQL scripts
Step 2: Take a backup of the database
Step 3: Execute the automated scripts of QTP
Step 4: Take a backup of the database
Step 5: Execute some more SQL scripts
Step 6: Execute some more automated scripts of QTP
Now lets say that you want to execute these during the weekend so that by the time you are back to office on Monday (with the Monday blues ;-) you are ready with the results. Lets say we are using a scheduler like Windows Scheduler or Autosys to schedule (automatically execute at a particular time) these jobs (thats what you usually call them). As of now, just understand that there are ways the SQL queries and database backups can be submitted as a job to the scheduler. Now lets learn how do we execute the QTP scripts. There are two scripts that are going to be involved here
1. The script that you developed using QTP
2. A VB script (.vbs file) that is going to call the script developed in QTP
And note this... a lot of people get confused with the VB script file ... understand that the VB script file is not going to be executed in QTP, its going to be executed at the command prompt. The reason for the confusion is that if you use the code that we have in the VB script file in a QTP script, you will still see that you will be able to open the test script and execute it...but remember...thats not our purpose !!!
Once we have these two ready, we just need to go to the DOS prompt and execute the following
C:\cscript "VB script file"
Ok..so what is cscript !!!
With Cscript.exe, you can run scripts by typing the name of a script file at the command prompt.When you start a script from your desktop or from the command prompt, the script host reads and passes the specified script file contents to the registered script engine. The script engine uses file extensions (that is, .vbs for VBScript and .js for JScript) to identify the script.
I am sure you must be thinking that so dont we manually need to type this command at the dos prompt. No !!! Thats where a scheduler comes into the picture. You pass this command as a job to the scheduler. So depending on the time when this job is scheduled, your QTP script is automatically going to execute. Voila !!!!
Want to go deeper....Ok...lets go for it..
First we need a QTP script. So lets develop one and call it SimpleLoop and here are the contents
Open QTP and enter the below in a new test
'============SimpleLoop Script=============
Option Explicit
Dim curIteration
For curIteration = 1 to 5
Wait(1)
Next
'End of SimpleLoop Script
Next what we need is a VB script file that is going to call the QTP script developed above. Lets call that qtpLaunch.vbs and here are its contents
Open Notepad and type in the following and save the file as "qtpLaunch.vbs"
'============qtpLaunch.vbs==============
Option Explicit
Dim qtApp, Test_Path
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
Test_Path = "C:\SimpleLoop"
qtApp.Open Test_Path,True
Dim qtTest
Set qtTest = qtApp.Test
qtTest.Run
qtTest.Close
qtApp.Quit
Set qtTest = Nothing
Set qtApp = Nothing
'End of qtpLaunch.vbs
We are ready. So remember what we do next, right !!! Go to the command prompt and just enter this... You dont need to have QTP open..it will open by itself...
C:\cscript qtpLaunch.vbs
How did it go !!!! I am sure it went well.... So as I mentioned above, if you pass the above command to a scheduler, you have actually "Automated the Automation". This is just the start....there is much more to this. As we go further, we will learn that we can connect to Quality Center and execute the tests from there using Open Test Architecture or OTA. This is also developed using VB or VB script. I will cover that in another post.

A request to my readers : This particular page is viewed numerous times in a day by several people. 



Way 2 :-**********************************
'RunThisTest
'by Michael Innes
'November 2012

testResourcePath = "C:\Test Logs and Results\"

'Getting the test path
Dim objArgs
Set objArgs = wscript.Arguments
testPath = objArgs(0)

'Determining that the test does exist
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
DoesFolderExist = objFSO.FolderExists(testPath)
Set objFSO = Nothing

If DoesFolderExist Then
    Dim qtApp 'Declare the Application object variable
    Dim qtTest 'Declare a Test object variable
    Set qtApp = CreateObject("QuickTest.Application") 'Create the Application object
    qtApp.Launch 'Start QuickTest
    qtApp.Visible = True 'Make the QuickTest application visible
    qtApp.Open testPath, False 'Open the test in read-only mode
    Set qtTest = qtApp.Test

    'Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
    'qtResultsOpt.ResultsLocation = testResourcePath ' Specify the location to save the test results.
    'qtTest.Run qtResultsOpt,True 'Run the test and wait until end of the test run

    qtTest.Run 'Run the test
    qtTest.Close 'Close the test
    qtApp.Quit
Else
    'Couldn't find the test folder. That's bad. Guess we'll have to report on how we couldn't find the test.
    'Insert reporting mechanism here.
End If
 
 
Note :- To use the code above, execute a command like this: cscript.exe "C:\RunThisTest.vbs" "L:\Test Path\The Test Itself\" 

Using Message Boxes That Close Automatically

Public Sub MsgBoxTimeout (Text, Title, TimeOut)

Set WshShell = CreateObject("WScript.Shell")

WshShell.Popup Text, TimeOut, Title

End Sub

If TimeOut is 0, it behaves just like a normal message box. If TimeOut is greater than 0, the dialog box disappears after the specified number of seconds.

Sunday, 4 January 2015

Different ways to Maximize and Minimize a Browser using QTP

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "%{ }"
wait(3)
WshShell.SendKeys " x"
Set WshShell=Nothing
wait(2)


--

mode_Maximized = 3 'Open in maximized mode
mode_Minimized = 2 'Open in minimized mode

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "iexplore.exe http://www.google.com", mode_Maximized, False
WshShell.Run "iexplore.exe http://www.
google.com", mode_Minimized, False
Set WshShell = Nothing


--

'Create an object of Internet Explorer type
Set oIE= CreateObject("InternetExplorer.Application")
oIE.visible = True
oIE.navigate "http://www.automationrepository.com"
 Wait(5)
'Maximize the Browser Window
Window("hwnd:=" & oIE.HWND).Maximize

'Minimize the Browser Window
Wait(2)
Window("hwnd:=" & oIE.HWND).Minimize

How to perform Page Scroll using QTP


Way 1:- How to perform Page Scroll using QTP

object .doScroll(“Scrollingaction”)

Scrolling Actions:

1.Up

2.down

3.pageUp

4.pageDown

5.pageLeft

6.pageRight and many more….

Function Scroll()

Browser(“micclass:=Browser”).Page(“micclass:=Page”).Sync

Set oDoc=Browser(“micclass:=Browser”).Page(“micclass:=Page”).Object.Body

oDoc.doScroll(“pageDown”)

Set oDoc=Nothing

End Function


Way 2:- How to perform Page Scroll using QTP 

'For page up

Set Obj=Browser("browsername").Page("pagename").Object.body

Obj.doScroll("pageUp")

'For page down

Set Obj=Browser("browsername").Page("pagename").Object.body

Obj.doScroll("pageDown")


Or you can also the following Send Keys method:

'For page up

Set objShell=CreateObject("WScript.Shell")

objShell.SendKeys "{PGUP}"


'For page down

Set objShell=CreateObject("WScript.Shell")

objShell.SendKeys "{PGDN}"