Commit 367e3f11 by 施晓雨

更新示例代码

parent a4846853
......@@ -36,6 +36,10 @@
this.label3 = new System.Windows.Forms.Label();
this.cbxRemote = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cbBrowserCssless = new System.Windows.Forms.CheckBox();
this.cbBrowserImageless = new System.Windows.Forms.CheckBox();
this.cbBrowserUndetected = new System.Windows.Forms.CheckBox();
this.cbBrowserHeadless = new System.Windows.Forms.CheckBox();
this.cbxHidenLocalCommandWindow = new System.Windows.Forms.CheckBox();
this.btnSwitchWindow = new System.Windows.Forms.Button();
this.panel5 = new System.Windows.Forms.Panel();
......@@ -76,11 +80,12 @@
this.tpActionJson = new System.Windows.Forms.TabPage();
this.rtbActionsJson = new System.Windows.Forms.RichTextBox();
this.panel4 = new System.Windows.Forms.Panel();
this.btnMofcomTest = new System.Windows.Forms.Button();
this.btnShowActionsTip = new System.Windows.Forms.Button();
this.btnMouseDemo = new System.Windows.Forms.Button();
this.btnActionsDemo = new System.Windows.Forms.Button();
this.btnActionsJson = new System.Windows.Forms.Button();
this.btnMofcomTest = new System.Windows.Forms.Button();
this.btnImportActions = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.panel5.SuspendLayout();
this.panel2.SuspendLayout();
......@@ -99,7 +104,7 @@
//
// btnInitialize
//
this.btnInitialize.Location = new System.Drawing.Point(6, 79);
this.btnInitialize.Location = new System.Drawing.Point(6, 133);
this.btnInitialize.Name = "btnInitialize";
this.btnInitialize.Size = new System.Drawing.Size(53, 23);
this.btnInitialize.TabIndex = 0;
......@@ -109,7 +114,7 @@
//
// btnDispose
//
this.btnDispose.Location = new System.Drawing.Point(73, 79);
this.btnDispose.Location = new System.Drawing.Point(73, 133);
this.btnDispose.Name = "btnDispose";
this.btnDispose.Size = new System.Drawing.Size(54, 23);
this.btnDispose.TabIndex = 0;
......@@ -142,7 +147,7 @@
//
// btnScreenshot
//
this.btnScreenshot.Location = new System.Drawing.Point(133, 79);
this.btnScreenshot.Location = new System.Drawing.Point(133, 133);
this.btnScreenshot.Name = "btnScreenshot";
this.btnScreenshot.Size = new System.Drawing.Size(55, 23);
this.btnScreenshot.TabIndex = 0;
......@@ -172,6 +177,10 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.cbBrowserCssless);
this.groupBox1.Controls.Add(this.cbBrowserImageless);
this.groupBox1.Controls.Add(this.cbBrowserUndetected);
this.groupBox1.Controls.Add(this.cbBrowserHeadless);
this.groupBox1.Controls.Add(this.cbxHidenLocalCommandWindow);
this.groupBox1.Controls.Add(this.btnSwitchWindow);
this.groupBox1.Controls.Add(this.panel5);
......@@ -189,24 +198,64 @@
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(293, 184);
this.groupBox1.Size = new System.Drawing.Size(293, 224);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "基础操作";
//
// cbBrowserCssless
//
this.cbBrowserCssless.AutoSize = true;
this.cbBrowserCssless.Location = new System.Drawing.Point(95, 103);
this.cbBrowserCssless.Name = "cbBrowserCssless";
this.cbBrowserCssless.Size = new System.Drawing.Size(72, 16);
this.cbBrowserCssless.TabIndex = 19;
this.cbBrowserCssless.Text = "禁用样式";
this.cbBrowserCssless.UseVisualStyleBackColor = true;
//
// cbBrowserImageless
//
this.cbBrowserImageless.AutoSize = true;
this.cbBrowserImageless.Location = new System.Drawing.Point(6, 103);
this.cbBrowserImageless.Name = "cbBrowserImageless";
this.cbBrowserImageless.Size = new System.Drawing.Size(72, 16);
this.cbBrowserImageless.TabIndex = 18;
this.cbBrowserImageless.Text = "禁用图片";
this.cbBrowserImageless.UseVisualStyleBackColor = true;
//
// cbBrowserUndetected
//
this.cbBrowserUndetected.AutoSize = true;
this.cbBrowserUndetected.Location = new System.Drawing.Point(178, 78);
this.cbBrowserUndetected.Name = "cbBrowserUndetected";
this.cbBrowserUndetected.Size = new System.Drawing.Size(72, 16);
this.cbBrowserUndetected.TabIndex = 17;
this.cbBrowserUndetected.Text = "隐藏特征";
this.cbBrowserUndetected.UseVisualStyleBackColor = true;
//
// cbBrowserHeadless
//
this.cbBrowserHeadless.AutoSize = true;
this.cbBrowserHeadless.Location = new System.Drawing.Point(95, 78);
this.cbBrowserHeadless.Name = "cbBrowserHeadless";
this.cbBrowserHeadless.Size = new System.Drawing.Size(72, 16);
this.cbBrowserHeadless.TabIndex = 16;
this.cbBrowserHeadless.Text = "无头模式";
this.cbBrowserHeadless.UseVisualStyleBackColor = true;
//
// cbxHidenLocalCommandWindow
//
this.cbxHidenLocalCommandWindow.AutoSize = true;
this.cbxHidenLocalCommandWindow.Location = new System.Drawing.Point(163, 53);
this.cbxHidenLocalCommandWindow.Location = new System.Drawing.Point(5, 81);
this.cbxHidenLocalCommandWindow.Name = "cbxHidenLocalCommandWindow";
this.cbxHidenLocalCommandWindow.Size = new System.Drawing.Size(84, 16);
this.cbxHidenLocalCommandWindow.Size = new System.Drawing.Size(72, 16);
this.cbxHidenLocalCommandWindow.TabIndex = 15;
this.cbxHidenLocalCommandWindow.Text = "隐藏命令行";
this.cbxHidenLocalCommandWindow.Text = "隐藏服务";
this.cbxHidenLocalCommandWindow.UseVisualStyleBackColor = true;
//
// btnSwitchWindow
//
this.btnSwitchWindow.Location = new System.Drawing.Point(238, 105);
this.btnSwitchWindow.Location = new System.Drawing.Point(238, 159);
this.btnSwitchWindow.Name = "btnSwitchWindow";
this.btnSwitchWindow.Size = new System.Drawing.Size(49, 23);
this.btnSwitchWindow.TabIndex = 14;
......@@ -219,7 +268,7 @@
this.panel5.Controls.Add(this.lblCurrenWindow);
this.panel5.Controls.Add(this.btnCloseCurrentWindow);
this.panel5.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel5.Location = new System.Drawing.Point(3, 143);
this.panel5.Location = new System.Drawing.Point(3, 183);
this.panel5.Name = "panel5";
this.panel5.Size = new System.Drawing.Size(287, 38);
this.panel5.TabIndex = 13;
......@@ -247,7 +296,7 @@
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(1, 113);
this.label7.Location = new System.Drawing.Point(1, 167);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(59, 12);
this.label7.TabIndex = 10;
......@@ -257,7 +306,7 @@
//
this.cbWindows.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbWindows.FormattingEnabled = true;
this.cbWindows.Location = new System.Drawing.Point(72, 107);
this.cbWindows.Location = new System.Drawing.Point(72, 161);
this.cbWindows.Name = "cbWindows";
this.cbWindows.Size = new System.Drawing.Size(163, 20);
this.cbWindows.TabIndex = 11;
......@@ -299,7 +348,7 @@
// groupBox3
//
this.groupBox3.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox3.Location = new System.Drawing.Point(0, 331);
this.groupBox3.Location = new System.Drawing.Point(0, 371);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(293, 136);
this.groupBox3.TabIndex = 12;
......@@ -318,7 +367,7 @@
this.groupBox2.Controls.Add(this.tbxVarName);
this.groupBox2.Controls.Add(this.tbxScriptPath);
this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox2.Location = new System.Drawing.Point(0, 184);
this.groupBox2.Location = new System.Drawing.Point(0, 224);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(293, 147);
this.groupBox2.TabIndex = 10;
......@@ -631,6 +680,7 @@
//
// panel4
//
this.panel4.Controls.Add(this.btnImportActions);
this.panel4.Controls.Add(this.btnMofcomTest);
this.panel4.Controls.Add(this.btnShowActionsTip);
this.panel4.Controls.Add(this.btnMouseDemo);
......@@ -642,6 +692,17 @@
this.panel4.Size = new System.Drawing.Size(820, 20);
this.panel4.TabIndex = 12;
//
// btnMofcomTest
//
this.btnMofcomTest.Dock = System.Windows.Forms.DockStyle.Left;
this.btnMofcomTest.Location = new System.Drawing.Point(216, 0);
this.btnMofcomTest.Name = "btnMofcomTest";
this.btnMofcomTest.Size = new System.Drawing.Size(72, 20);
this.btnMofcomTest.TabIndex = 17;
this.btnMofcomTest.Text = "Mofcom";
this.btnMofcomTest.UseVisualStyleBackColor = true;
this.btnMofcomTest.Click += new System.EventHandler(this.btnMofcomTest_Click);
//
// btnShowActionsTip
//
this.btnShowActionsTip.Dock = System.Windows.Forms.DockStyle.Left;
......@@ -686,16 +747,16 @@
this.btnActionsJson.UseVisualStyleBackColor = true;
this.btnActionsJson.Click += new System.EventHandler(this.btnActionsJson_Click);
//
// btnMofcomTest
// btnImportActions
//
this.btnMofcomTest.Dock = System.Windows.Forms.DockStyle.Left;
this.btnMofcomTest.Location = new System.Drawing.Point(216, 0);
this.btnMofcomTest.Name = "btnMofcomTest";
this.btnMofcomTest.Size = new System.Drawing.Size(72, 20);
this.btnMofcomTest.TabIndex = 17;
this.btnMofcomTest.Text = "Mofcom";
this.btnMofcomTest.UseVisualStyleBackColor = true;
this.btnMofcomTest.Click += new System.EventHandler(this.btnMofcomTest_Click);
this.btnImportActions.Dock = System.Windows.Forms.DockStyle.Right;
this.btnImportActions.Location = new System.Drawing.Point(670, 0);
this.btnImportActions.Name = "btnImportActions";
this.btnImportActions.Size = new System.Drawing.Size(75, 20);
this.btnImportActions.TabIndex = 18;
this.btnImportActions.Text = "导入脚本";
this.btnImportActions.UseVisualStyleBackColor = true;
this.btnImportActions.Click += new System.EventHandler(this.btnImportActions_Click);
//
// FrmMain
//
......@@ -705,7 +766,7 @@
this.Controls.Add(this.panel3);
this.Controls.Add(this.panel2);
this.Name = "FrmMain";
this.Text = "代码测试";
this.Text = "网页自动化工具-功能演示";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.panel5.ResumeLayout(false);
......@@ -781,6 +842,11 @@
private System.Windows.Forms.Button btnSwitchWindow;
private System.Windows.Forms.CheckBox cbxHidenLocalCommandWindow;
private System.Windows.Forms.Button btnMofcomTest;
private System.Windows.Forms.CheckBox cbBrowserHeadless;
private System.Windows.Forms.CheckBox cbBrowserUndetected;
private System.Windows.Forms.CheckBox cbBrowserImageless;
private System.Windows.Forms.CheckBox cbBrowserCssless;
private System.Windows.Forms.Button btnImportActions;
}
}
......@@ -49,6 +49,8 @@ namespace Kivii.Test.WebDriver
_manager.OnException += manager_OnException;
_manager.OnCurrentWindowChanged += manager_OnWindowChanged;
_manager.OnActionExecuted += manager_OnActionExecuted;
_manager.OnActionException += manager_OnActionException;
//以下是WebDriver的事件
_manager.ElementClicked += EventDriver_ElementClicked;
_manager.ElementClicking += EventDriver_ElementClicking;
......@@ -69,6 +71,16 @@ namespace Kivii.Test.WebDriver
_manager.ScriptExecuting += EventDriver_ScriptExecuting;
}
private void manager_OnActionException(Window sender, Exception e)
{
appendMsg($"动作执行异常:{DateTime.Now}{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}");
}
private void manager_OnActionExecuted(Window sender, string e)
{
appendMsg($"动作执行成功:{DateTime.Now}{Environment.NewLine}{e}");
}
private void manager_OnWindowChanged(object sender, Window e)
{
lblCurrenWindow.Text = $"当前窗口:{(_manager?.StartWindow==_manager.CurrentWindow?"主窗口":"子窗口")}{Environment.NewLine}{e.Handle}{Environment.NewLine}标题:{e.Title}";
......@@ -235,7 +247,12 @@ namespace Kivii.Test.WebDriver
if (_manager != null) return;
rtbMsg.Clear();
_manager = new Manager();
_manager.HideCommandPromptWindow = cbxHidenLocalCommandWindow.Checked;//如果是本地运行,可选择隐藏本地服务的命令行
if (cbBrowserUndetected.Checked) _manager.Options |= BrowserOption.Undetected;//不显示浏览器
if (cbBrowserHeadless.Checked) _manager.Options |= BrowserOption.Headless;//不显示浏览器
if (cbBrowserImageless.Checked) _manager.Options |= BrowserOption.Imageless;//不显示浏览器
if (cbBrowserCssless.Checked) _manager.Options |= BrowserOption.Cssless;//不显示浏览器
_manager.HideLocalService = cbxHidenLocalCommandWindow.Checked;//如果是本地运行,可选择隐藏本地服务的命令行
addListener();//管理器的2个事件是直接可以监听的,WebDriver的事件是需要等初始化完才能监听的
var serviceUrl = string.Empty;
if (cbxRemote.Checked)
......@@ -446,11 +463,19 @@ namespace Kivii.Test.WebDriver
var json = autoPage.ExportActions(true);
rtbActionsJson.Text = json;
}
/// <summary>
/// 导入JSON文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImportActions_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Filter = "脚本文件|*.json";
if (ofd.ShowDialog() != DialogResult.OK) return;
var json = System.IO.File.ReadAllText(ofd.FileName);
rtbActionsJson.Text = json;
}
#endregion
}
}
......@@ -35,8 +35,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Kivii.WebDriver.V4.7, Version=4.15.2023.11050, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Kivii.WebDriver.4.15.2023.11050\lib\net47\Kivii.WebDriver.V4.7.dll</HintPath>
<Reference Include="Kivii.WebDriver.V4.7, Version=4.15.2023.11060, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Kivii.WebDriver.4.15.2023.11060\lib\net47\Kivii.WebDriver.V4.7.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
......
......@@ -26,6 +26,7 @@ namespace Kivii.Test.WebDriver.Tests
#region 暂停2
{
var action = new SleepAction();
action.Name = "暂停2秒";
action.PauseMilliseconds = 2000;
rtns.Actions.Add(action);
}
......@@ -34,6 +35,7 @@ namespace Kivii.Test.WebDriver.Tests
#region 移动鼠标至第1个元素上点击
{
var action = new ClickAction();
action.Name = "移至第一个标签";
action.Target = new ElementFinder("//*[@id=\"zhtbomrasff\"]");
rtns.Actions.Add(action);
}
......@@ -42,7 +44,7 @@ namespace Kivii.Test.WebDriver.Tests
#region 暂停2
{
var action = new SleepAction();
action.Name = "准备抓取网页";
action.Name = "等待抓取网页";
action.PauseMilliseconds = 2000;
rtns.Actions.Add(action);
}
......@@ -51,6 +53,7 @@ namespace Kivii.Test.WebDriver.Tests
#region 抓取内容
{
var action = new JavascriptAction();
action.Name = "执行脚本";
action.Script="return '抓取当前页内容'";
rtns.Actions.Add(action);
}
......@@ -59,6 +62,7 @@ namespace Kivii.Test.WebDriver.Tests
#region 点击下一页
{
var action = new ClickAction();
action.Name = "点击下一页";
action.Target = new ElementFinder()
{
Type = ElementFindType.LinkText,
......@@ -72,7 +76,7 @@ namespace Kivii.Test.WebDriver.Tests
{
var action = new LoopAction();
action.LoopTimes =10;
action.FromName = "准备抓取网页";
action.FromName = "等待抓取网页";
rtns.Actions.Add(action);
}
#endregion
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Kivii.WebDriver" version="4.15.2023.11050" targetFramework="net472" />
<package id="Kivii.WebDriver" version="4.15.2023.11060" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="Selenium.Support" version="4.15.0" targetFramework="net472" />
<package id="Selenium.WebDriver" version="4.15.0" targetFramework="net472" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment