Deep Patel: VB
Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Tuesday, February 26, 2013

Generic vb Functions for any Webpage



Generic functions which can be used in vbscript or vba macro to open URL, enter values in text box, click a link and get all links on a webpage.
The below code shows a sample of the usage.
It asks for username and password, log into the web application and clicks on all links one by one.

You can use the generic functions/subs available at the lower part of the code

Dim IE
Dim IE2
uname=InputBox("Enter your uname")
passs=InputBox("Enter your password")
Set IE = WScript.CreateObject("InternetExplorer.Application")

'set the ie properties
IE.ToolBar = 0
IE.StatusBar = 1
IE.Width = 999
IE.Height = 999
IE.Left = 0
IE.Top = 0
IE.Visible = 1

'navigate to a web page
IE.Navigate("http://www.yourURL/")
WaitForLoad IE


WebFormFill "", , 0 
WebFormFill "",,1 
WaitForLoad IE

ClickLink ""
WaitForLoad IE


Set ElementCol = IE.Document.getElementsByTagName("a")
'Get all links on web page
'Grap/click the link we are looking for...
For Each Link In ElementCol
If instr(1,Link.innerHTML,". http://")>1 Then
mylink = Link.innerHTML

ClickLink mylink


End If
Next




Msgbox "Completed"


Sub GetOtherIE(URL)
  Dim objInstances, objIE
  Set objInstances = CreateObject("Shell.Application").windows
  If objInstances.Count > 0 Then '/// make sure we have instances open.
    For Each objIE In objInstances
 If InStr(objIE.LocationURL,URL) > 0 then
   Set IE2 = objIE
 End if
    Next
  End if
End Sub

'Attaches to an existing instance of IE with matching URL
Sub GetIE(URL)
  Dim objInstances, objIE
  Set objInstances = CreateObject("Shell.Application").windows
  If objInstances.Count > 0 Then '/// make sure we have instances open.
    For Each objIE In objInstances
 If InStr(objIE.LocationURL,URL) > 0 then
   Set IE = objIE
 End if
    Next
  End if
End Sub

'fills a form field and optionally submits form
Sub WebFormFill(fieldname,fieldvalue,submit)
  Dim FormNr
  Dim ItemNr
  Dim TheForm

  if IE.Document.All.Tags("FORM").Length = 0 then
    MsgBox("No form found in page")
  else
    for FormNr = 0 to IE.Document.Forms.Length - 1
      Set TheForm = IE.Document.Forms(FormNr)
      for ItemNr = 0 to TheForm.Elements.Length - 1
        if TheForm.Elements(ItemNr).Name = fieldname then
          TheForm.Elements(ItemNr).Value = fieldvalue
          If submit=1 then
            TheForm.submit
          end if
          exit for
        end if
      next
    next
  end if
End Sub

'Navigates IE to specified URL
Sub Navigate(URL)
  IE.Navigate URL
  do while IE.Busy
  loop
End Sub

'clicks specified link
Sub ClickLink(linktext)
  Dim anchors
  Dim ItemNr

  Set anchors = IE.document.getElementsbyTagname("a")
  For ItemNr = 0 to anchors.length - 1
    If anchors.Item(ItemNr).innertext = linktext Then
 anchors.Item(ItemNr).click
End If
  next

  'do while IE.Busy
  'loop
End Sub

'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
  dim t
  set t = IE.document.getElementsbyTagname(Tagname)
  if all=1 then
    ExtractTag = t.Item(Num).outerHTML
  else
    ExtractTag = t.Item(Num).innerText
  end if
End Function

Sub WaitForLoad(obj)
Do While obj.Busy
loop
wscript.sleep(100)
End Sub

Monday, January 21, 2013

QC OTA code for getting Bug/Defect History

Get all the required history details of a Defect/Bug in QC using the OTA apis.

Use the below code to access the history of a defect

Dim BugFactory, BugList
Set BugFactory = QCConnection.BugFactory  
Set bugFilter = BugFactory.FilterSet BugList = bugFilter.NewList
Set Bug = BugList(0) 'gives the first bug in bug list

Set hst = Bug.history
Set hstList = hst.NewList("")
For j = 1 To hstList.Count
                Set hstRec = hstList.Item(j)
                HTMLMSg_Content = HTMLMSg_Content & " " & hstRec.FieldName
                HTMLMSg_Content = HTMLMSg_Content & "
"
                HTMLMSg_Content = HTMLMSg_Content & hstRec.ChangeDate & "
"
                HTMLMSg_Content = HTMLMSg_Content & "" & hstRec.NewValue & "
" 'Target Test Date
Next j


To get specific items from the list use if conditions as below:

Set hst = Bug.history
Set hstList = hst.NewList("")
For j = 1 To hstList.Count
                Set hstRec = hstList.Item(j)
                If hstRec.FieldName = "BG_STATUS" And hstRec.NewValue = "Ready to Test" Then
                                'HTMLMSg_Content = HTMLMSg_Content & " " & hstRec.FieldName
                                'HTMLMSg_Content = HTMLMSg_Content & "
"
                                HTMLMSg_Table2 = HTMLMSg_Table2 & "" & hstRec.ChangeDate & ""
                                'HTMLMSg_Content = HTMLMSg_Content & hstRec.ChangeDate & "
"
                                'HTMLMSg_Content = HTMLMSg_Content & "" & hstRec.NewValue & "
" 'Target Test Date
                                Exit For
                 End If
Next j

Monday, February 27, 2012

QTP Random Number Generation - RandomNumber.Value vs Rnd and Randomize

Recently I was facing a problem in generating Random numbers using QTP.

I started with QTP's inbuilt RandomNumber.Value(upperlimit,lowerlimit)

and the result was:

It would generate random numbers for that instance of QTP. But once QTP is restarted it will generate same numbers in same sequence. Example is shown in the images below.

The QTP always starts the Random Number Generation from the same number. To elaborate, QTP's Random Number function gets initialized with the same number(the seed) every time QTP's instance is created.

So for better performance we can use vb's Rnd function with call to Randomize. Call to Randomize initializes the Rnd function with a new seed value every time(this is based on the system time).
If Randomize is not used, Rnd uses the same number as a seed the first time it is called.
Hence it will generate the same value everytime the function is called.


Below are the results of all the cases. The execution 1 and execution 2 are of separate QTP instances.

Using QTP’s RandomNumber.Value(a,b) function.



Using vb’s Rnd function with Randomize.
This will generate random numbers every time!


Using vb’s Rnd function without Randomize.


Wondering what would happen on using Randomize with QTP's RandomNumber.Value(a,b) !!
Here are the results...

For more on Rnd Function follow check this link


For more on Randomize Function follow check this link