Friday, 27 October 2017

UFT:Clicking Checkbox present in Webtable

Rem Method 1
Browser("Creationtime:=0").WebTable("Column names:=;;X;;.*").ChildItem(1,1,"WebCheckbox",0).Set "ON"

Rem Method 2
Set CHKObj = Browser("Creationtime:=0").WebTable("Column names:=;;X;.*").ChildItem(1,1,"WebCheckbox",0)
If typename(CHKObj) = "HtmlCheckBox" Then
CHKObj.Set "ON"
End IF


Rem Method 3
Rem Compare Some text in the table with some unique data

NextRowdata = "XYZ"
Set TableObj = Browser("Creationtime:=0").WebTable("Column names:=;;X;.*")
RC = TableObj.GetROProperty("ROWS")
If RC>0 Then
    For ROWID = 1 To RC
    GetData = TableObj.GetCellData(ROWID,2)   
    If lcase(trim(GetData)) = Lcase(trim(NextRowdata) Then
    Exit For
    End If
    Next

End If

Set CHKObj = TableObj.ChildItem(RowID,1,"WebCheckbox",0)
If typename(CHKObj) = "HtmlCheckBox" Then
CHKObj.Set "ON"
End IF

Tuesday, 24 October 2017

UFT: Browser Methods

'Open a new Browser using Util Object
SystemUtil.Run "iexplore.exe""www.uftHelp.com"

'Browser Sync
Browser("CreationTime:=0").Sync

'Delete cookies from the Browser
Browser("CreationTime:=0").DeleteCookies

'Refreshes the Opened Page
Browser("CreationTime:=0").Refresh

'Opening  a new tab within the same browser
Browser("CreationTime:=0").OpenNewTab()

'Opening Browser in FullScreen mode
Browser("CreationTime:=0").FullScreen

'Sync for new tab
Browser("CreationTime:=1").Sync

'Navigate to perticular URL in the new tab
Browser("CreationTime:=1").Navigate "www.Google.com"
'Find total number of tabs in the browser window
iTabs = Browser("CreationTime:=0").GetROProperty("number of tabs")
msgbox iTabs 'Displays the value 2
'Close all the tabs in the browser window
Browser("CreationTime:=0").CloseAllTabs()

UFT: ADODB connection to Sql Server

'Create Connection Object

Set con=CreateObject("ADODB.connection")

'Create RecordSet Object
Set Rs=CreateObject("ADODB.Recordset")

On Error Resume next

'Open the Connection 
con.Open "Provider = sqloledb.1;Server=SUNSHINE-PC;Database=northwind;User Id=sa;Password=password;"


'run the query with record set object over opened connection


rs.open "select * from [dbo].[Categories]",con

While rs.EOF<>True


msgbox rs.Fields.Item(0)&" "&rs.Fields.Item(1)

'Bad Record for testing
msgbox rs.Fields.Item(100)

rs.MoveNext
   
   
Wend

If Err.Num<>0 Then
   
    msgbox "Something Wrong with ADOBDB"
End If

rs.Close
con.Close

Set rs=nothing
Set con=nothing

Monday, 23 October 2017

UFT FIreEvent:Dealing with MouseOver Menus


'http://way2automation.com/way2auto_jquery/index.php
Browser("Welcome_2").Page("Welcome_2").Link("Widget").FireEvent "OnMouseOver"
Browser("Welcome_2").Page("Welcome_2").Link("Datepicker").Click

Sunday, 22 October 2017

UFT: Drag and Drop (When Object Supports it)

Below Code works if the WebElement has supports Drag and Drop Function.

If Object does not support Drag and Drop , Mercury Device Replay should be tried.

'siteused: http://way2automation.com/way2auto_jquery/droppable.php
Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drag me to my target").Drag , micNoCoordinate, micNoCoordinate, micCtrl
Wait 3
Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drop here").Drop






 'By Using Mercury Device Replay

Set obj = CreateObject("mercury.devicereplay")
Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drop here").Click

absx1=Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drag me to my target").GetROproperty("abs_x")
absy1=Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drag me to my target").GetROproperty("abs_y")

absx2=Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drop here").GetROproperty("abs_x")
absy2=Browser("Welcome").Page("Welcome").Frame("Frame").WebElement("Drop here").GetROproperty("abs_y")

obj.DragAndDrop absx1, absy1, absx2, absy2,0
Set obj = Nothing  

Saturday, 21 October 2017

Excel" Finding the Number of Used Rows

Dim oexl
Set oexl=CreateObject("Excel.Application")
Set owb= oexl.Workbooks.Open("D:\uftCTexit\NandaHybrid\testarea\Range Object.xlsx")
Set  objWS=owb.Worksheets("Sheet1")

 Set objRange = objWS.Range("A1").CurrentRegion
 Msgbox objRange.Rows.Count

=>4

Msgbox objWS.Usedrange.Rows.count
=>4


 'msgbox objWS.Cells(3000, 1).End(xlUp).row
'Rem -4162 is enumeration for  xlup
  msgbox objWS.Cells(3000, 1).End(-4162).row
 
  =>6


 owb.Close

 set owb=nothing
 oexl.Quit


'Add Extra spaces in workbooks after last row

Msgbox objRange.Rows.Count
=>4

Msgbox objWS.Usedrange.Rows.count
=>6






Thursday, 19 October 2017

Vbscript: Finding count of files by file extension




Dim fso
Set fso = CreateObject("scripting.filesystemobject")
vbfilecount=0
xlsxfilecount=0
txtfilecount=0

Set ofolder=fso.GetFolder("C:\Users\sunshine\Desktop\vbscriptprgm\SeparateFiles by Extension\")
set filecol=ofolder.Files
Print "Total files are: "&filecol.Count

For each filename in filecol
    Print filename.Name
    fullname=filename.Name
    If instr(1,fullname,".vbs")>0 Then
    vbfilecount=vbfilecount+1
   
    ElseIf instr(1,fullname,".xlsx")>0 Then
    xlsxfilecount=xlsxfilecount+1
   
    ElseIf instr(1,fullname,".txt")>0 Then
    txtfilecount=txtfilecount+1
       
    End If
Next

Print "vbfilecount is "&vbfilecount
Print "xlsxfilecount is "&xlsxfilecount
Print "txtfilecount is "&txtfilecount

Set fso=nothing



------Output-------------------------

Total files are: 6
BlankFile.vbs
New Microsoft Office Excel Worksheet.xlsx
New Text Document (2).txt
New Text Document.txt
test.txt
xlfile.xlsx
vbfilecount is 1
xlsxfilecount is 2
txtfilecount is 3
 

Tuesday, 17 October 2017

UFT WebTable Descriptive Program to find occurance of a name



Set owebel=description.Create
owebel("micclass").value="WebElement"
owebel("html tag").value="TD"
count=0
 
set webelecol=Browser("Buy Women Clothing Online:").Page("SamplePage").WebTable("empid").ChildObjects(owebel)

For i = 0 To wt.count-1 Step 1

If webelecol(i).GetroProperty("innertext")="vura" Then
    count=count+1
End If
   
Next

msgbox count



========Sample Page

<Html>

<Head>
<Title>Sample Page </Title>
</Head>

<Body>
<Form>
Username:<input type="text" value=""/><br>
Password: <input type="text" value=""/><br>

Gender:<Br>
Male  <input type="radio" name="grp" value="Male" Checked><br>
Female<input type="radio" name="grp" value="Female" ><br>

Indian<input type="radio" name="country" value="Indian Citizen" ><br>
Accept Condition<input type="checkbox" name="accept_condition" value="Indian Citizen" ><br>
Link: <a href=http://www.Google.com/ target="_blank">Googlei<br></a>

</Form>
<br>
<br>
<br>
<br>

<table border=1 cellpadding=3 cellspacing=3 name="tableemp" >
 <tr>
   <th></th>
   <th>empid</th>
   <th>empname</th>
   <th>department</th>
   <th>joindate</th>
   <th>city</th>
 </tr>

 <tr>
   <td> <input type="checkbox" name="accept_condition" value="Indian Citizen" ></td>
   <td>101</td>
   <td>sumit</td>
   <td>uat</td>
  
   <td>29 sep 2014</td>
   <td>mumbai</td>
 </tr>


  <tr>
   <td> <input type="checkbox" name="accept_condition" value="Indian Citizen" ></td>
   <td>102</td>
   <td>vura</td>
   <td>qa</td>
  
   <td>29 sep 2016</td>
   <td>hyd</td>
 </tr>
</table>
<br>
<br>

</Body>

</html>

Monday, 16 October 2017

UFT and XML

REM:Sample File:

REM:Stored location:"C:\Users\sunshine\Desktop\vbscriptprgm\samplexmlnanda.xml"

<Resources>
<Module1>
 <Function_Libararies>
 <Library>module1lib1.vbs</Library>
 <Library>module1lib1</Library>
 <Library>module1lib1</Library>
 </Function_Libararies>

 <Repositories>
 <Repository>module1repo.tsr</Repository>
 <Repository>module2repo.tsr</Repository>
   </Repositories>
</Module1>

<Module2>

 <Function_Libararies>
 <Library>module2lib2.vbs</Library>
 <Library>module2lib2</Library>
 <Library>module2lib2</Library>
 </Function_Libararies>

 <Repositories>
 <Repository>module2repo.tsr</Repository>
 <Repository>module2repo.tsr</Repository>
   </Repositories>
</Module2>

</Resources>










REM:Code to Read File:

Dim Modules
Modules=Array("Module1","Module2")

Const XMLDataFile="C:\Users\sunshine\Desktop\vbscriptprgm\samplexmlnanda.xml"
Set oxml= CreateObject("Microsoft.XMLDOM")
oxml.Async = False
oxml.Load(XMLDataFile)

If oxml.Parseerror.Errorcode<>0 Then
  
msgbox "Error Parsing XMl"
  
End If

For i=lbound(Modules) To ubound(Modules) Step 1

parent=cstr(Modules(i))
  
Set Col=oxml.getElementsByTagName(parent).Item(0)

Set olibcol=Col.getElementsByTagName("Function_Libararies").Item(0)

Set lib=olibcol.Childnodes

For each  strlib in lib

msgbox strlib.text
  
Next
Next

VBscript: Finding Occurance of Each Character in the string

Dim a()
Dim strtext
'Provide the text 
strtext="this is sparta"

'Create An Array with each letter of the text
ReDim a((len(strtext)-1))
For i=1 to len(strtext) Step 1
a(i-1)=mid(strtext,i,1)
Next

'Create Dictionary Object to store unique values of the text

Set odic=CreateObject("Scripting.Dictionary")
'ReDim a((len(strtext)-1))
For i=1 to len(strtext) Step 1

If NOT odic.Exists(mid(strtext,i,1)) Then
    odic.add (mid(strtext,i,1)),"i"
End If
   
Next

alluniquekeys=odic.Keys

'Compare unique items with each item present in the array and increase the counter when there is a match
For i = 0 To ubound(alluniquekeys) Step 1
    count=0
    For j = 0 To ubound(a) Step 1
       
        If alluniquekeys(i)=a(j) Then
        count=count+1
        End If
    Next
    print " "&a(i)& "appears "&count&"times"
Next




REM===> OUTPUT
 t appears 2 times
 h appears 1 times
 i appears 2 times
 s appears 3 times
   appears 2 times
 p appears 1 times
 a appears 2 times
 r appears 1 times

Saturday, 14 October 2017

UFT: Connecting to SQL SERVER with ADODB

Set con=CreateObject("ADODB.connection")
Set Rs=CreateObject("ADODB.Recordset")

On Error Resume next
con.Open "Provider = sqloledb.1;Server=SUNSHINE-PC;Database=northwind;User Id=sa;Password=password;"

rs.open "select * from [dbo].[Categories]",con

While rs.EOF<>True
 
msgbox rs.Fields.Item(0)&" "&rs.Fields.Item(1)


rs.MoveNext
   
   
Wend

If Err.Num<>0 Then
   
  msgbox "Something Wrong with ADOBDB"
End If

rs.Close
con.Close

Set rs=nothing
Set con=nothing

RegisterUserFunc in UFT


RegisterUserFunc "WebEdit""fnWebEditSet""fnWebEditSet"
RegisterUserFunc "WebEdit""fnWebEditSetSecure""fnWebEditSetSecure"


Function fnWebEditSet(objControl,objControlText)

If objControl.Exist(10) Then


           If objControl.GetRoProperty("Disabled")=0 Then
           objControl.Set objControlText
              
           Reporter.ReportEvent micPass,"WebEdit Set",""&objControl.ToString()&" "&"value captured"
              
           Else
           Reporter.ReportEvent micFail,"WebEdit Set",""&objControl.ToString()&" "&"is in disabled state"
              
           End If

Else
Reporter.ReportEvent micFail,"WebEdit Check",""&objControl.ToString()&" "&" does not exist"
End If


End Function





Function fnWebEditSetSecure(objControl,objControlText)

If objControl.Exist(10) Then


           If objControl.GetRoProperty("Disabled")=0 Then
           objControl.SetSecure objControlText
              
           Reporter.ReportEvent micPass,"WebEdit Set",""&objControl.ToString()&" "&"value captured"
              
           Else
           Reporter.ReportEvent micFail,"WebEdit Set",""&objControl.ToString()&" "&"is in disabled state"
              
           End If






Else

Reporter.ReportEvent micFail,"WebEdit Check",""&objControl.ToString()&" "&" does not exist"

End If


End Function



'Browser("Buy Women Clothing Online:").Page("Buy Women Clothing Online:").WebEdit("edsrchword").fnWebEditSet "sumit"

Browser("Buy Women Clothing Online:").Page("Buy Women Clothing Online:").WebEdit("edsrchword").fnWebEditSetSecure "x"

Spring Boot : Exception Handler 14