Skip to content

Commit f1f0ae1

Browse files
committed
Added Selenium Options Selection
1 parent 625c803 commit f1f0ae1

File tree

4 files changed

+124
-5
lines changed

4 files changed

+124
-5
lines changed

taskt/Core/Automation/Commands/SeleniumBrowserElementActionCommand.cs

+110-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Xml.Serialization;
99
using Newtonsoft.Json;
1010
using OpenQA.Selenium;
11+
using OpenQA.Selenium.Support.UI;
1112
using taskt.UI.CustomControls;
1213
using taskt.UI.Forms;
1314

@@ -68,7 +69,9 @@ public class SeleniumBrowserElementActionCommand : ScriptCommand
6869
[Attributes.PropertyAttributes.PropertyUISelectionOption("Get Matching Elements")]
6970
[Attributes.PropertyAttributes.PropertyUISelectionOption("Wait For Element To Exist")]
7071
[Attributes.PropertyAttributes.PropertyUISelectionOption("Switch to frame")]
71-
[Attributes.PropertyAttributes.PropertyUISelectionOption("Get Count")]
72+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Get Count")]
73+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Get Options")]
74+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Select Option")]
7275
[Attributes.PropertyAttributes.InputSpecification("Select the appropriate corresponding action to take once the element has been located")]
7376
[Attributes.PropertyAttributes.SampleUsage("Select from **Invoke Click**, **Left Click**, **Right Click**, **Middle Click**, **Double Left Click**, **Clear Element**, **Set Text**, **Get Text**, **Get Attribute**, **Wait For Element To Exist**, **Get Count**")]
7477
[Attributes.PropertyAttributes.Remarks("Selecting this field changes the parameters that will be required in the next step")]
@@ -261,8 +264,87 @@ where rw.Field<string>("Parameter Name") == "Clear Element Before Setting Text"
261264
}
262265
}
263266

267+
break;
268+
case "Get Options":
269+
270+
string applyToVarName = (from rw in v_WebActionParameterTable.AsEnumerable()
271+
where rw.Field<string>("Parameter Name") == "Variable Name"
272+
select rw.Field<string>("Parameter Value")).FirstOrDefault();
273+
274+
275+
string attribName = (from rw in v_WebActionParameterTable.AsEnumerable()
276+
where rw.Field<string>("Parameter Name") == "Attribute Name"
277+
select rw.Field<string>("Parameter Value")).FirstOrDefault().ConvertToUserVariable(sender);
278+
279+
280+
var optionsItems = new List<string>();
281+
var ele = (IWebElement)element;
282+
var select = new SelectElement(ele);
283+
var options = select.Options;
284+
285+
foreach (var option in options)
286+
{
287+
var optionValue = option.GetAttribute(attribName);
288+
optionsItems.Add(optionValue);
289+
}
290+
291+
var requiredVariable = engine.VariableList.Where(x => x.VariableName == applyToVarName).FirstOrDefault();
292+
293+
if (requiredVariable == null)
294+
{
295+
engine.VariableList.Add(new Script.ScriptVariable() { VariableName = applyToVarName, CurrentPosition = 0 });
296+
requiredVariable = engine.VariableList.Where(x => x.VariableName == applyToVarName).FirstOrDefault();
297+
}
298+
299+
requiredVariable.VariableValue = optionsItems;
300+
requiredVariable.CurrentPosition = 0;
301+
302+
264303
break;
304+
case "Select Option":
305+
306+
string selectionType = (from rw in v_WebActionParameterTable.AsEnumerable()
307+
where rw.Field<string>("Parameter Name") == "Selection Type"
308+
select rw.Field<string>("Parameter Value")).FirstOrDefault();
309+
310+
string selectionParam = (from rw in v_WebActionParameterTable.AsEnumerable()
311+
where rw.Field<string>("Parameter Name") == "Selection Parameter"
312+
select rw.Field<string>("Parameter Value")).FirstOrDefault();
313+
314+
315+
seleniumInstance.SwitchTo().ActiveElement();
316+
317+
var el = (IWebElement)element;
318+
var selectionElement = new SelectElement(el);
319+
320+
switch (selectionType)
321+
{
322+
case "Select By Index":
323+
selectionElement.SelectByIndex(int.Parse(selectionParam));
324+
break;
325+
case "Select By Text":
326+
selectionElement.SelectByText(selectionParam);
327+
break;
328+
case "Select By Value":
329+
selectionElement.SelectByValue(selectionParam);
330+
break;
331+
case "Deselect By Index":
332+
selectionElement.DeselectByIndex(int.Parse(selectionParam));
333+
break;
334+
case "Deselect By Text":
335+
selectionElement.DeselectByText(selectionParam);
336+
break;
337+
case "Deselect By Value":
338+
selectionElement.DeselectByValue(selectionParam);
339+
break;
340+
case "Deselect All":
341+
selectionElement.DeselectAll();
342+
break;
343+
default:
344+
throw new NotImplementedException();
345+
}
265346

347+
break;
266348
case "Get Text":
267349
case "Get Attribute":
268350
case "Get Count":
@@ -569,7 +651,34 @@ public void seleniumAction_SelectionChangeCommitted(object sender, EventArgs e)
569651
actionParameters.Rows.Add("Variable Name");
570652
}
571653
break;
654+
case "Get Options":
655+
actionParameters.Rows.Add("Attribute Name");
656+
actionParameters.Rows.Add("Variable Name");
657+
break;
658+
case "Select Option":
659+
actionParameters.Rows.Add("Selection Type");
660+
actionParameters.Rows.Add("Selection Parameter");
661+
572662

663+
DataGridViewComboBoxCell selectionTypeBox = new DataGridViewComboBoxCell();
664+
selectionTypeBox.Items.Add("Select By Index");
665+
selectionTypeBox.Items.Add("Select By Text");
666+
selectionTypeBox.Items.Add("Select By Value");
667+
selectionTypeBox.Items.Add("Deselect By Index");
668+
selectionTypeBox.Items.Add("Deselect By Text");
669+
selectionTypeBox.Items.Add("Deselect By Value");
670+
selectionTypeBox.Items.Add("Deselect All");
671+
672+
//assign cell as a combobox
673+
if (sender != null)
674+
{
675+
ElementsGridViewHelper.Rows[0].Cells[1].Value = "Select By Text";
676+
}
677+
678+
ElementsGridViewHelper.Rows[0].Cells[1] = selectionTypeBox;
679+
680+
681+
break;
573682
case "Wait For Element To Exist":
574683
foreach (var ctrl in ElementParameterControls)
575684
{

taskt/Core/ExtensionMethods.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public static string ConvertToUserVariable(this String str, object sender)
2121
return string.Empty;
2222

2323
if (sender == null)
24-
return str;
24+
return str;
25+
26+
if (str.Length < 2)
27+
{
28+
return str;
29+
}
30+
2531

2632
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
2733

taskt/packages.config

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ limitations under the License.
2222
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
2323
<package id="OneNoteOCR" version="1.0.0.0" targetFramework="net452" />
2424
<package id="RestSharp" version="106.6.9" targetFramework="net452" />
25-
<package id="Selenium.WebDriver" version="3.7.0" targetFramework="net452" />
25+
<package id="Selenium.Support" version="3.141.0" targetFramework="net48" />
26+
<package id="Selenium.WebDriver" version="3.141.0" targetFramework="net48" />
2627
<package id="Serilog" version="2.7.1" targetFramework="net452" />
2728
<package id="Serilog.Formatting.Compact" version="1.0.0" targetFramework="net452" />
2829
<package id="Serilog.Sinks.File" version="4.0.0" targetFramework="net452" />

taskt/taskt.csproj

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@
146146
<Reference Include="UIAutomationClientsideProviders" />
147147
<Reference Include="UIAutomationProvider" />
148148
<Reference Include="UIAutomationTypes" />
149-
<Reference Include="WebDriver, Version=3.7.0.0, Culture=neutral, processorArchitecture=MSIL">
150-
<HintPath>..\packages\Selenium.WebDriver.3.7.0\lib\net45\WebDriver.dll</HintPath>
149+
<Reference Include="WebDriver, Version=3.141.0.0, Culture=neutral, processorArchitecture=MSIL">
150+
<HintPath>..\packages\Selenium.WebDriver.3.141.0\lib\net45\WebDriver.dll</HintPath>
151+
</Reference>
152+
<Reference Include="WebDriver.Support, Version=3.141.0.0, Culture=neutral, processorArchitecture=MSIL">
153+
<HintPath>..\packages\Selenium.Support.3.141.0\lib\net45\WebDriver.Support.dll</HintPath>
151154
</Reference>
152155
<Reference Include="WebSocket4Net, Version=0.15.0.9, Culture=neutral, PublicKeyToken=eb4e154b696bf72a, processorArchitecture=MSIL">
153156
<HintPath>..\packages\WebSocket4Net.0.15.0\lib\net45\WebSocket4Net.dll</HintPath>

0 commit comments

Comments
 (0)