March 13, 2018
UI Automation | Advance Script With OR
The below piece of code demonstrates the usage of advance methods for UI Automation. It invokes the sample application which is provided with the tool. This code also uses object repository which is attached in the post.
Setup:
- Copy the below VBScript code in a text file with extension .jts
- In the same folder as script add a config file (mandatory to report path in config file)
- Save the repository file (embedded below) as XML and update the repository path in config file
- Run
Repository
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> <Objects> <ky_Main>Window,Name::Form1 <ky_TabControl>Tab,AutomationId::tabControl1<ky_TabPage1>TabItem,Name::tabPage1</ky_TabPage1><ky_TabPage2>TabItem,Name::tabPage2</ky_TabPage2><ky_TabPage3>TabItem,Name::tabPage3</ky_TabPage3><ky_TabPane>Pane<ky_Tree>Tree</ky_Tree><ky_DataGrid>Table,AutomationId::dataGridView1</ky_DataGrid><ky_Checkbox1>CheckBox,AutomationID::checkBox1</ky_Checkbox1><ky_RadioButton1>RadioButton,AutomationID::radioButton1</ky_RadioButton1><ky_RadioButton2>RadioButton,AutomationID::radioButton2</ky_RadioButton2></ky_TabPane></ky_TabControl> <ky_Textbox1>Edit,AutomationID::textBox1</ky_Textbox1> <ky_Popup>Window<ky_OK_Button>Button,Name::OK</ky_OK_Button></ky_Popup> <ky_Toolstrip1>Toolbar,Name::toolStrip1 <ky_Button1_TS>Button,Name::btn1</ky_Button1_TS><ky_List1>ComboBox</ky_List1></ky_Toolstrip1> <ky_Button1>Button,Name::button1</ky_Button1> </ky_Main> </Objects> |
VBScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 'Launch the app if not already launched. If Not(uia.Exist("ky_Main")) Then hwnd = util.StartApp("C:\Testmate\Sample\SampleTest.exe") End If 'uia.SetRoot "ky_Main" uia.SetRoot hwnd 'Select the tab uia.Select("ky_TabPage1") ' Click on the button uia.Click "ky_Button1" 'Fail the step if Popup Appears - Explicit failure for demonstration If uia.Exist("ky_OK_Button",3) Then Logger.Fail "Step 1", "Popup has been displayed", True uia.Click "ky_OK_Button" End If 'Set the Textbox value uia.SetValue "ky_Textbox1" ,cstr(Time) 'Toggle the checkbox uia.Toggle("ky_Checkbox1") 'Validate the Checkbox is checked If uia.GetLegacyProperty("ky_Checkbox1","state")="1048596" Then 'Report the passed step Logger.Pass "Step 2", "checkbox checked" Else 'Report failure Logger.Fail "Step 2", "checkbox checked",True End If 'Click on radio button 2 uia.Click("ky_RadioButton2") 'Select Tab 2 uia.Select "ky_TabPage2" 'Perform Expand node operations on the the tree view uia.ExpandTreeNode "ky_Tree" ,"Node0" uia.ExpandTreeNode "ky_Tree" ,"Node0;Node2" uia.ExpandTreeNode "ky_Tree", "Node0;Node2;Node3" 'Perform Select Node Operation uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree" ,"Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" 'Perform Expand select node operation uia.ExpandTreeNode "ky_Tree", "Node1" uia.SelectTreeNode "ky_Tree", "Node1;Node17" 'Collapse all the tree nodes uia.CollapseTreeNode "ky_Tree", "Node0" uia.CollapseTreeNode "ky_Tree", "Node0;Node2" uia.CollapseTreeNode "ky_Tree", "Node0;Node2;Node3" uia.CollapseTreeNode "ky_Tree", "Node1" 'Select Value from the List view uia.SelectFromDropDown "ky_List1" ,"Third" 'Select Third Tab uia.Select "ky_TabPage3" 'Set the values in data grid uia.SetCellData "ky_DataGrid", 1,1 ,Cstr(Time) uia.SetCellData "ky_DataGrid",1,2 ,"Test" uia.SetCellData "ky_DataGrid",1,3 ,Cstr(Time) 'Select The Tab uia.Select("ky_TabPage1") 'Click on the button 1 uia.Click "ky_Button1" 'Handle popup If uia.Exist("ky_OK_Button",3) Then 'Click Ok uia.Click "ky_OK_Button" End If 'Set the Textbox value uia.SetValue "ky_Textbox1" ,cstr(Time) 'Toggle the checkbox uia.Toggle("ky_Checkbox1") 'Validate the Checkbox is not checked If uia.GetLegacyProperty("ky_Checkbox1","state")="1048596" Then 'Report the passed step Logger.Fail "Step 3", "checkbox checked"' Else 'Report failure Logger.Pass "Step 3", "checkbox checked",True End If 'Click on radio button 2 uia.Click("ky_RadioButton2") 'Select Tab 2 uia.Select "ky_TabPage2" 'Perform Expand node operations on the the tree view uia.ExpandTreeNode "ky_Tree" ,"Node0" uia.ExpandTreeNode "ky_Tree" ,"Node0;Node2" uia.ExpandTreeNode "ky_Tree", "Node0;Node2;Node3" 'Perform Select Node Operation uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree" ,"Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" uia.SelectTreeNode "ky_Tree", "Node0;Node2;Node3" uia.SelectTreeNode "ky_Tree", "Node0;Node2" 'Perform Expand an select node operation uia.ExpandTreeNode "ky_Tree", "Node1" uia.SelectTreeNode "ky_Tree", "Node1;Node17" 'Collapse all the tree nodes uia.CollapseTreeNode "ky_Tree", "Node0" uia.CollapseTreeNode "ky_Tree", "Node0;Node2" uia.CollapseTreeNode "ky_Tree", "Node0;Node2;Node3" uia.CollapseTreeNode "ky_Tree", "Node1" 'Select Value from the List view uia.SelectFromDropDown "ky_List1" ,"Third" 'Select Third Tab uia.Select "ky_TabPage3" 'Set the values in data grid uia.SetCellData "ky_DataGrid", 1,1 ,Cstr(Time) uia.SetCellData "ky_DataGrid",1,2 ,"Test" uia.SetCellData "ky_DataGrid",1,3 ,Cstr(Time) 'Reset the root uia.ResetRoot 'Close the Window uia.CloseWindow "ky_Main" |