1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Xml . Serialization ;
4
+ using System . Data ;
5
+ using System . Windows . Forms ;
6
+ using System . Collections . Generic ;
7
+ using taskt . UI . Forms ;
8
+ using taskt . UI . CustomControls ;
9
+ using Microsoft . Office . Interop . Outlook ;
10
+
11
+ namespace taskt . Core . Automation . Commands
12
+ {
13
+ [ Serializable ]
14
+ [ Attributes . ClassAttributes . Group ( "Data Commands" ) ]
15
+ [ Attributes . ClassAttributes . Description ( "This command allows you to get the item count of a List" ) ]
16
+ [ Attributes . ClassAttributes . UsesDescription ( "Use this command when you want to get the item count of a List." ) ]
17
+ [ Attributes . ClassAttributes . ImplementationDescription ( "" ) ]
18
+ public class GetListItemCommand : ScriptCommand
19
+ {
20
+ [ XmlAttribute ]
21
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the List Name" ) ]
22
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
23
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter a existing List." ) ]
24
+ [ Attributes . PropertyAttributes . SampleUsage ( "**myData**" ) ]
25
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
26
+ public string v_ListName { get ; set ; }
27
+
28
+ [ XmlAttribute ]
29
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please enter the index of the List item" ) ]
30
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
31
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter a valid List index value" ) ]
32
+ [ Attributes . PropertyAttributes . SampleUsage ( "0 or [vIndex]" ) ]
33
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
34
+ public string v_ItemIndex { get ; set ; }
35
+
36
+ [ XmlAttribute ]
37
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Assign to Variable" ) ]
38
+ [ Attributes . PropertyAttributes . InputSpecification ( "Select or provide a variable from the variable list" ) ]
39
+ [ Attributes . PropertyAttributes . SampleUsage ( "**vSomeVariable**" ) ]
40
+ [ Attributes . PropertyAttributes . Remarks ( "If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended." ) ]
41
+ public string v_UserVariableName { get ; set ; }
42
+
43
+ public GetListItemCommand ( )
44
+ {
45
+ this . CommandName = "GetListItemCommand" ;
46
+ this . SelectionName = "Get List Item" ;
47
+ this . CommandEnabled = true ;
48
+ this . CustomRendering = true ;
49
+
50
+ }
51
+
52
+ public override void RunCommand ( object sender )
53
+ {
54
+ var engine = ( Core . Automation . Engine . AutomationEngineInstance ) sender ;
55
+ var itemIndex = v_ItemIndex . ConvertToUserVariable ( sender ) ;
56
+ int index = int . Parse ( itemIndex ) ;
57
+ //get variable by regular name
58
+ Script . ScriptVariable listVariable = engine . VariableList . Where ( x => x . VariableName == v_ListName ) . FirstOrDefault ( ) ;
59
+
60
+ //user may potentially include brackets []
61
+ if ( listVariable == null )
62
+ {
63
+ listVariable = engine . VariableList . Where ( x => x . VariableName . ApplyVariableFormatting ( ) == v_ListName ) . FirstOrDefault ( ) ;
64
+ }
65
+
66
+ //if still null then throw exception
67
+ if ( listVariable == null )
68
+ {
69
+ throw new System . Exception ( "Complex Variable '" + v_ListName + "' or '" + v_ListName . ApplyVariableFormatting ( ) + "' not found. Ensure the variable exists before attempting to modify it." ) ;
70
+ }
71
+
72
+ dynamic listToIndex ;
73
+ if ( listVariable . VariableValue is List < string > )
74
+ {
75
+ listToIndex = ( List < string > ) listVariable . VariableValue ;
76
+ }
77
+ else if ( listVariable . VariableValue is List < MailItem > )
78
+ {
79
+ listToIndex = ( List < MailItem > ) listVariable . VariableValue ;
80
+ }
81
+ else if ( listVariable . VariableValue is List < OpenQA . Selenium . IWebElement > )
82
+ {
83
+ listToIndex = ( List < OpenQA . Selenium . IWebElement > ) listVariable . VariableValue ;
84
+ }
85
+ else if ( ( listVariable . VariableValue . ToString ( ) . StartsWith ( "[" ) ) && ( listVariable . VariableValue . ToString ( ) . EndsWith ( "]" ) ) && ( listVariable . VariableValue . ToString ( ) . Contains ( "," ) ) )
86
+ {
87
+ //automatically handle if user has given a json array
88
+ Newtonsoft . Json . Linq . JArray jsonArray = Newtonsoft . Json . JsonConvert . DeserializeObject ( listVariable . VariableValue . ToString ( ) ) as Newtonsoft . Json . Linq . JArray ;
89
+
90
+ var itemList = new List < string > ( ) ;
91
+ foreach ( var jsonItem in jsonArray )
92
+ {
93
+ var value = ( Newtonsoft . Json . Linq . JValue ) jsonItem ;
94
+ itemList . Add ( value . ToString ( ) ) ;
95
+ }
96
+
97
+ listVariable . VariableValue = itemList ;
98
+ listToIndex = itemList ;
99
+ }
100
+ else
101
+ {
102
+ throw new System . Exception ( "Complex Variable List Type<T> Not Supported" ) ;
103
+ }
104
+
105
+ var item = listToIndex [ index ] ;
106
+
107
+ Script . ScriptVariable newListItem = new Script . ScriptVariable
108
+ {
109
+ VariableName = v_UserVariableName ,
110
+ VariableValue = item
111
+ } ;
112
+
113
+ //Overwrites variable if it already exists
114
+ if ( engine . VariableList . Exists ( x => x . VariableName == newListItem . VariableName ) )
115
+ {
116
+ Script . ScriptVariable temp = engine . VariableList . Where ( x => x . VariableName == newListItem . VariableName ) . FirstOrDefault ( ) ;
117
+ engine . VariableList . Remove ( temp ) ;
118
+ }
119
+ engine . VariableList . Add ( newListItem ) ;
120
+
121
+ }
122
+
123
+ public override List < Control > Render ( frmCommandEditor editor )
124
+ {
125
+ base . Render ( editor ) ;
126
+
127
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_ListName" , this , editor ) ) ;
128
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_ItemIndex" , this , editor ) ) ;
129
+ RenderedControls . Add ( CommandControls . CreateDefaultLabelFor ( "v_UserVariableName" , this ) ) ;
130
+ var VariableNameControl = CommandControls . CreateStandardComboboxFor ( "v_UserVariableName" , this ) . AddVariableNames ( editor ) ;
131
+ RenderedControls . AddRange ( CommandControls . CreateUIHelpersFor ( "v_UserVariableName" , this , new Control [ ] { VariableNameControl } , editor ) ) ;
132
+ RenderedControls . Add ( VariableNameControl ) ;
133
+
134
+ return RenderedControls ;
135
+ }
136
+
137
+
138
+
139
+ public override string GetDisplayValue ( )
140
+ {
141
+ return base . GetDisplayValue ( ) + $ " [From '{ v_ListName } ', Store In: '{ v_UserVariableName } ']";
142
+ }
143
+ }
144
+ }
0 commit comments