Skip to content

Commit bd1b01d

Browse files
committed
Added Encryption for Send Keys, Web Browser Element Set Text and Encryption Commands #172
1 parent 077011d commit bd1b01d

File tree

7 files changed

+264
-5
lines changed

7 files changed

+264
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Xml.Serialization;
5+
using System.Data;
6+
using System.Windows.Forms;
7+
using taskt.UI.Forms;
8+
using taskt.UI.CustomControls;
9+
10+
namespace taskt.Core.Automation.Commands
11+
{
12+
[Serializable]
13+
[Attributes.ClassAttributes.Group("Misc Commands")]
14+
[Attributes.ClassAttributes.Description("This command handles text encryption")]
15+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to store some data encrypted")]
16+
[Attributes.ClassAttributes.ImplementationDescription("")]
17+
public class EncryptionCommand : ScriptCommand
18+
{
19+
[XmlElement]
20+
[Attributes.PropertyAttributes.PropertyDescription("Select Encryption Action")]
21+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Encrypt")]
22+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Decrypt")]
23+
[Attributes.PropertyAttributes.InputSpecification("Select an action to take")]
24+
[Attributes.PropertyAttributes.SampleUsage("Select from **Encrypt**, **Decrypt**")]
25+
[Attributes.PropertyAttributes.Remarks("")]
26+
public string v_EncryptionType { get; set; }
27+
28+
[XmlAttribute]
29+
[Attributes.PropertyAttributes.PropertyDescription("Supply the data or variable (ex. {someVariable})")]
30+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
31+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable or json array value")]
32+
[Attributes.PropertyAttributes.SampleUsage("**Test** or **{var}**")]
33+
[Attributes.PropertyAttributes.Remarks("")]
34+
public string v_InputValue { get; set; }
35+
36+
[XmlAttribute]
37+
[Attributes.PropertyAttributes.PropertyDescription("Provide a Pass Phrase")]
38+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
39+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable or json array value")]
40+
[Attributes.PropertyAttributes.SampleUsage("**Test** or **{var}**")]
41+
[Attributes.PropertyAttributes.Remarks("")]
42+
public string v_PassPhrase { get; set; }
43+
44+
[XmlAttribute]
45+
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the encrypted data")]
46+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
47+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
48+
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
49+
[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.")]
50+
public string v_applyToVariableName { get; set; }
51+
52+
public EncryptionCommand()
53+
{
54+
this.CommandName = "EncryptionCommand";
55+
this.SelectionName = "Encryption Command";
56+
this.CommandEnabled = true;
57+
this.CustomRendering = true;
58+
this.v_EncryptionType = "Encrypt";
59+
this.v_PassPhrase = "TASKT";
60+
61+
62+
}
63+
64+
public override void RunCommand(object sender)
65+
{
66+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
67+
68+
//get variablized input
69+
var variableInput = v_InputValue.ConvertToUserVariable(sender);
70+
var passphrase = v_PassPhrase.ConvertToUserVariable(sender);
71+
72+
string resultData = "";
73+
if (v_EncryptionType.ConvertToUserVariable(sender) == "Encrypt")
74+
{
75+
//encrypt data
76+
resultData = Core.EncryptionServices.EncryptString(variableInput, passphrase);
77+
}
78+
else if (v_EncryptionType.ConvertToUserVariable(sender) == "Decrypt")
79+
{
80+
//encrypt data
81+
resultData = Core.EncryptionServices.DecryptString(variableInput, passphrase);
82+
}
83+
else
84+
{
85+
throw new NotImplementedException($"Encryption Service Requested '{v_EncryptionType.ConvertToUserVariable(sender)}' has not been implemented");
86+
}
87+
88+
//get variable
89+
var requiredComplexVariable = engine.VariableList.Where(x => x.VariableName == v_applyToVariableName).FirstOrDefault();
90+
91+
//create if var does not exist
92+
if (requiredComplexVariable == null)
93+
{
94+
engine.VariableList.Add(new Script.ScriptVariable() { VariableName = v_applyToVariableName, CurrentPosition = 0 });
95+
requiredComplexVariable = engine.VariableList.Where(x => x.VariableName == v_applyToVariableName).FirstOrDefault();
96+
}
97+
98+
//assign value to variable
99+
requiredComplexVariable.VariableValue = resultData;
100+
101+
}
102+
public override List<Control> Render(frmCommandEditor editor)
103+
{
104+
base.Render(editor);
105+
106+
//create standard group controls
107+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_EncryptionType", this, editor));
108+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InputValue", this, editor));
109+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_PassPhrase", this, editor));
110+
111+
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
112+
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
113+
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
114+
RenderedControls.Add(VariableNameControl);
115+
116+
return RenderedControls;
117+
118+
}
119+
120+
121+
122+
public override string GetDisplayValue()
123+
{
124+
return base.GetDisplayValue() + $" [{v_EncryptionType} Data, apply to '{v_applyToVariableName}']";
125+
}
126+
}
127+
}

taskt/Core/Automation/Commands/ScriptCommand.cs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ namespace taskt.Core.Automation.Commands
130130
[XmlInclude(typeof(RemoteAPICommand))]
131131
[XmlInclude(typeof(SeleniumBrowserSwitchFrameCommand))]
132132
[XmlInclude(typeof(ParseJsonModelCommand))]
133+
[XmlInclude(typeof(EncryptionCommand))]
133134
public abstract class ScriptCommand
134135
{
135136
[XmlAttribute]

taskt/Core/Automation/Commands/SeleniumBrowserElementActionCommand.cs

+52-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@ where rw.Field<string>("Parameter Name") == "Text To Set"
225225

226226
string clearElement = (from rw in v_WebActionParameterTable.AsEnumerable()
227227
where rw.Field<string>("Parameter Name") == "Clear Element Before Setting Text"
228-
select rw.Field<string>("Parameter Value")).FirstOrDefault();
228+
select rw.Field<string>("Parameter Value")).FirstOrDefault();
229+
230+
string encryptedData = (from rw in v_WebActionParameterTable.AsEnumerable()
231+
where rw.Field<string>("Parameter Name") == "Encrypted Text"
232+
select rw.Field<string>("Parameter Value")).FirstOrDefault();
233+
234+
229235

230236
if (clearElement == null)
231237
{
@@ -238,6 +244,11 @@ where rw.Field<string>("Parameter Name") == "Clear Element Before Setting Text"
238244
}
239245

240246

247+
if (encryptedData == "Encrypted")
248+
{
249+
textToSet = Core.EncryptionServices.DecryptString(textToSet, "TASKT");
250+
}
251+
241252
string[] potentialKeyPresses = textToSet.Split('{', '}');
242253

243254
Type seleniumKeys = typeof(OpenQA.Selenium.Keys);
@@ -611,6 +622,20 @@ public void seleniumAction_SelectionChangeCommitted(object sender, EventArgs e)
611622
{
612623
actionParameters.Rows.Add("Text To Set");
613624
actionParameters.Rows.Add("Clear Element Before Setting Text");
625+
actionParameters.Rows.Add("Encrypted Text");
626+
actionParameters.Rows.Add("Optional - Click to Encrypt 'Text To Set'");
627+
628+
DataGridViewComboBoxCell encryptedBox = new DataGridViewComboBoxCell();
629+
encryptedBox.Items.Add("Not Encrypted");
630+
encryptedBox.Items.Add("Encrypted");
631+
ElementsGridViewHelper.Rows[2].Cells[1] = encryptedBox;
632+
ElementsGridViewHelper.Rows[2].Cells[1].Value = "Not Encrypted";
633+
634+
var buttonCell = new DataGridViewButtonCell();
635+
ElementsGridViewHelper.Rows[3].Cells[1] = buttonCell;
636+
ElementsGridViewHelper.Rows[3].Cells[1].Value = "Encrypt Text";
637+
ElementsGridViewHelper.CellContentClick += ElementsGridViewHelper_CellContentClick;
638+
614639
}
615640

616641
DataGridViewComboBoxCell comparisonComboBox = new DataGridViewComboBoxCell();
@@ -702,8 +727,32 @@ public void seleniumAction_SelectionChangeCommitted(object sender, EventArgs e)
702727
}
703728

704729
ElementsGridViewHelper.DataSource = v_WebActionParameterTable;
705-
}
706-
730+
}
731+
732+
private void ElementsGridViewHelper_CellContentClick(object sender, DataGridViewCellEventArgs e)
733+
{
734+
var targetCell = ElementsGridViewHelper.Rows[e.RowIndex].Cells[e.ColumnIndex];
735+
736+
if (targetCell is DataGridViewButtonCell && targetCell.Value.ToString() == "Encrypt Text")
737+
{
738+
var targetElement = ElementsGridViewHelper.Rows[0].Cells[1];
739+
740+
if (string.IsNullOrEmpty(targetElement.Value.ToString()))
741+
return;
742+
743+
var warning = MessageBox.Show($"Warning! Text should only be encrypted one time and is not reversible in the builder. Would you like to proceed and convert '{targetElement.Value.ToString()}' to an encrypted value?", "Encryption Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
744+
745+
if (warning == DialogResult.Yes)
746+
{
747+
targetElement.Value = EncryptionServices.EncryptString(targetElement.Value.ToString(), "TASKT");
748+
ElementsGridViewHelper.Rows[2].Cells[1].Value = "Encrypted";
749+
}
750+
751+
}
752+
753+
754+
}
755+
707756
public override string GetDisplayValue()
708757
{
709758
return base.GetDisplayValue() + " [" + v_SeleniumSearchType + " and " + v_SeleniumElementAction + ", Instance Name: '" + v_InstanceName + "']";

taskt/Core/Automation/Commands/SendKeysCommand.cs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Drawing;
34
using System.Windows.Forms;
45
using System.Xml.Serialization;
56
using taskt.Core.Automation.User32;
@@ -29,12 +30,26 @@ public class SendKeysCommand : ScriptCommand
2930
[Attributes.PropertyAttributes.Remarks("This command supports sending variables within brackets [vVariable]")]
3031
public string v_TextToSend { get; set; }
3132

33+
[XmlAttribute]
34+
[Attributes.PropertyAttributes.PropertyDescription("Please Indicate if Text is Encrypted")]
35+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Not Encrypted")]
36+
[Attributes.PropertyAttributes.PropertyUISelectionOption("Encrypted")]
37+
[Attributes.PropertyAttributes.InputSpecification("Indicate if the text in 'TextToSend' is Encrypted.")]
38+
[Attributes.PropertyAttributes.SampleUsage("")]
39+
[Attributes.PropertyAttributes.Remarks("")]
40+
public string v_EncryptionOption { get; set; }
41+
42+
[XmlIgnore]
43+
[NonSerialized]
44+
private TextBox InputText;
45+
3246
public SendKeysCommand()
3347
{
3448
this.CommandName = "SendKeysCommand";
3549
this.SelectionName = "Send Keystrokes";
3650
this.CommandEnabled = true;
3751
this.CustomRendering = true;
52+
this.v_EncryptionOption = "Not Encrypted";
3853
}
3954

4055
public override void RunCommand(object sender)
@@ -50,6 +65,10 @@ public override void RunCommand(object sender)
5065

5166
string textToSend = v_TextToSend.ConvertToUserVariable(sender);
5267

68+
if (v_EncryptionOption == "Encrypted")
69+
{
70+
textToSend = Core.EncryptionServices.DecryptString(textToSend, "TASKT");
71+
}
5372

5473
if (textToSend == "{WIN_KEY}")
5574
{
@@ -96,13 +115,37 @@ public override List<Control> Render(frmCommandEditor editor)
96115
RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
97116
RenderedControls.Add(WindowNameControl);
98117

99-
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_TextToSend", this, editor));
118+
taskt.UI.CustomControls.CommandItemControl helperControl = new taskt.UI.CustomControls.CommandItemControl();
119+
120+
var textInputGroup = CommandControls.CreateDefaultInputGroupFor("v_TextToSend", this, editor);
121+
RenderedControls.AddRange(textInputGroup);
122+
123+
InputText = (TextBox)textInputGroup[2];
100124

125+
helperControl.ForeColor = Color.White;
126+
helperControl.CommandDisplay = "Encrypt Text";
127+
helperControl.Click += HelperControl_Click;
128+
RenderedControls.Add(helperControl);
129+
130+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_EncryptionOption", this, editor));
101131

102132
return RenderedControls;
103133

104134
}
105135

136+
private void HelperControl_Click(object sender, EventArgs e)
137+
{
138+
139+
if (string.IsNullOrEmpty(InputText.Text))
140+
return;
141+
142+
var encrypted = EncryptionServices.EncryptString(InputText.Text, "TASKT");
143+
this.v_EncryptionOption = "Encrypted";
144+
145+
InputText.Text = encrypted;
146+
147+
}
148+
106149
public override string GetDisplayValue()
107150
{
108151
return base.GetDisplayValue() + " [Send '" + v_TextToSend + "' to '" + v_WindowName + "']";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<Commands>
4+
<ScriptAction>
5+
<ScriptCommand xsi:type="CommentCommand" CommandID="dc59be51-604a-4799-a070-5b7d190b7e63" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="Set Unencrypted Value" CommandEnabled="true" />
6+
</ScriptAction>
7+
<ScriptAction>
8+
<ScriptCommand xsi:type="VariableCommand" CommandID="c37b3da5-18f2-49ef-a63f-b9c3de6933ea" CommandName="VariableCommand" IsCommented="false" SelectionName="Set Variable" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_userVariableName="vData" v_Input="Hello World!" />
9+
</ScriptAction>
10+
<ScriptAction>
11+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="24e8a8f5-6d0d-4f67-a567-f79dc237171c" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vData}" v_AutoCloseAfter="0" />
12+
</ScriptAction>
13+
<ScriptAction>
14+
<ScriptCommand xsi:type="CommentCommand" CommandID="00353095-45ad-4bbb-9791-bd8f40f368a9" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" v_Comment="Encrypt and Show Data" CommandEnabled="true" />
15+
</ScriptAction>
16+
<ScriptAction>
17+
<ScriptCommand xsi:type="EncryptionCommand" CommandID="87cf100b-d992-4fdd-85be-133fec892010" CommandName="EncryptionCommand" IsCommented="false" SelectionName="Encryption Command" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" CommandEnabled="true" v_InputValue="{vData}" v_PassPhrase="TASKT" v_applyToVariableName="vData">
18+
<v_EncryptionType>Encrypt</v_EncryptionType>
19+
</ScriptCommand>
20+
</ScriptAction>
21+
<ScriptAction>
22+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="3a201ecb-b33e-4834-8c64-3734c7e671e5" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vData}" v_AutoCloseAfter="0" />
23+
</ScriptAction>
24+
<ScriptAction>
25+
<ScriptCommand xsi:type="CommentCommand" CommandID="68f14e05-26bb-4e19-be9e-a5d41730930e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" v_Comment="Decrypt and Show Data" CommandEnabled="true" />
26+
</ScriptAction>
27+
<ScriptAction>
28+
<ScriptCommand xsi:type="EncryptionCommand" CommandID="df1c7fa8-4dc8-4947-bd5c-548ee62d2350" CommandName="EncryptionCommand" IsCommented="false" SelectionName="Encryption Command" DefaultPause="0" LineNumber="8" PauseBeforeExeucution="false" CommandEnabled="true" v_InputValue="{vData}" v_PassPhrase="TASKT" v_applyToVariableName="vData">
29+
<v_EncryptionType>Decrypt</v_EncryptionType>
30+
</ScriptCommand>
31+
</ScriptAction>
32+
<ScriptAction>
33+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="a46e52d2-3e0b-436a-9f5f-4595ad8e4dab" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="9" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vData}" v_AutoCloseAfter="0" />
34+
</ScriptAction>
35+
</Commands>
36+
<Variables />
37+
</Script>

taskt/UI/CustomControls/CustomControls.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ public static Dictionary<string, Image> UIImageDictionary()
461461
uiImages.Add("IEBrowserCloseCommand", taskt.Properties.Resources.command_window_close);
462462
uiImages.Add("IEBrowserElementCommand", taskt.Properties.Resources.command_web);
463463
uiImages.Add("SendKeysCommand", taskt.Properties.Resources.command_input);
464-
uiImages.Add("SendAdvancedKeyStrokesCommand", taskt.Properties.Resources.command_input);
464+
uiImages.Add("SendAdvancedKeyStrokesCommand", taskt.Properties.Resources.command_input);
465+
uiImages.Add("EncryptionCommand", taskt.Properties.Resources.command_input);
465466
uiImages.Add("SendMouseMoveCommand", taskt.Properties.Resources.command_input);
466467
uiImages.Add("SendMouseClickCommand", taskt.Properties.Resources.command_input);
467468
uiImages.Add("Setcommand_windowtateCommand", taskt.Properties.Resources.command_window);

taskt/taskt.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<Compile Include="Core\Automation\Commands\CatchExceptionCommand.cs" />
176176
<Compile Include="Core\Automation\Commands\EndTryCommand.cs" />
177177
<Compile Include="Core\Automation\Commands\FinallyCommand.cs" />
178+
<Compile Include="Core\Automation\Commands\EncryptionCommand.cs" />
178179
<Compile Include="Core\Automation\Commands\ParseJsonModelCommand.cs" />
179180
<Compile Include="Core\Automation\Commands\RemoteAPICommand.cs" />
180181
<Compile Include="Core\Automation\Commands\RemoteTaskCommand.cs" />

0 commit comments

Comments
 (0)