From 21002e47ca7018aab57c29a267c3c8920630ad38 Mon Sep 17 00:00:00 2001 From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com> Date: Mon, 5 Aug 2024 20:48:58 +0800 Subject: [PATCH] =?UTF-8?q?WTS=EF=BC=88=E7=BD=91=E9=A1=B5=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8C=96=E6=B5=8B=E8=AF=95=EF=BC=89=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=9B=B4=E5=A4=9A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SereinWAT/SereinWAT.cs | 101 ++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 22 deletions(-) diff --git a/SereinWAT/SereinWAT.cs b/SereinWAT/SereinWAT.cs index 69a375e..72da155 100644 --- a/SereinWAT/SereinWAT.cs +++ b/SereinWAT/SereinWAT.cs @@ -17,6 +17,29 @@ namespace Serein.Module IE, Firefox, } + public enum ByType + { + XPath, + Id, + Class, + Name, + CssSelector, + PartialLinkText, + } + public enum ScriptOp + { + Add, + Modify, + Delete, + } + public enum ActionType + { + Click, + DoubleClick, + RightClick, + SendKeys + } + /// /// 网页自动化测试 @@ -45,9 +68,14 @@ namespace Serein.Module } #endregion + [MethodDetail(DynamicNodeType.Action,"等待")] + public void Wait([Explicit]int time = 1000) + { + Thread.Sleep(time); + } + [MethodDetail(DynamicNodeType.Action,"启动浏览器")] - public WebDriver OpenDriver([Explicit] bool isVisible = true, - [Explicit] DriverType driverType = DriverType.Chrome) + public WebDriver OpenDriver([Explicit] bool isVisible = true,[Explicit] DriverType driverType = DriverType.Chrome) { @@ -102,12 +130,6 @@ namespace Serein.Module } - [MethodDetail(DynamicNodeType.Action,"等待")] - public void Wait([Explicit]int time = 1000) - { - Thread.Sleep(time); - } - [MethodDetail(DynamicNodeType.Action,"进入网页")] public void ToPage([Explicit] string url) { @@ -121,29 +143,64 @@ namespace Serein.Module } } - [MethodDetail(DynamicNodeType.Action,"XPath定位元素")] - public IWebElement XPathFind(string xpath, - [Explicit] int index = 0) + + [MethodDetail(DynamicNodeType.Action,"定位元素")] + public IWebElement FindElement(string key, [Explicit] ByType byType = ByType.Id, [Explicit] int index = 0) { - var element = WebDriver.FindElements(By.XPath(xpath))[0]; + By by = byType switch + { + ByType.Id => By.Id(key), + ByType.XPath => By.XPath(key), + ByType.Class => By.ClassName(key), + ByType.Name => By.Name(key), + ByType.CssSelector => By.CssSelector(key), + ByType.PartialLinkText => By.PartialLinkText(key), + }; + var element = WebDriver.FindElements(by)[index]; return element; } - - [MethodDetail(DynamicNodeType.Action,"Js添加元素内部属性")] - public void AddAttribute(IWebElement element, [Explicit] string attributeName = "", [Explicit] string value = "") + [MethodDetail(DynamicNodeType.Action, "操作元素")] + public void PerformAction(IWebElement element, [Explicit] ActionType actionType = ActionType.Click, [Explicit] string text = "") { - //IJavaScriptExecutor js = (IJavaScriptExecutor)Driver; - WebDriver.ExecuteScript($"arguments[0].{attributeName} = arguments[1];", element, value); + var actions = new OpenQA.Selenium.Interactions.Actions(WebDriver); + + switch (actionType) + { + case ActionType.Click: + actions.Click(element).Perform(); + break; + case ActionType.DoubleClick: + actions.DoubleClick(element).Perform(); + break; + case ActionType.RightClick: + actions.ContextClick(element).Perform(); + break; + case ActionType.SendKeys: + element.SendKeys(text); + break; + } } - [MethodDetail(DynamicNodeType.Action, "Js设置元素内部属性")] - public void SetAttribute(IWebElement element, [Explicit] string attributeName = "", [Explicit] string value = "") + + [MethodDetail(DynamicNodeType.Action,"Js操作元素属性")] + public void AddAttribute(IWebElement element, [Explicit] ScriptOp scriptOp = ScriptOp.Modify,[Explicit] string attributeName = "", [Explicit] string value = "") { - //IJavaScriptExecutor js = (IJavaScriptExecutor)Driver; - WebDriver.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", element, attributeName, value); + if(scriptOp == ScriptOp.Add) + { + WebDriver.ExecuteScript($"arguments[0].{attributeName} = arguments[1];", element, value); + } + else if (scriptOp == ScriptOp.Modify) + { + WebDriver.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", element, attributeName, value); + } + else if (scriptOp == ScriptOp.Delete) + { + WebDriver.ExecuteScript("arguments[0].removeAttribute(arguments[1]);", element, attributeName); + } } - [MethodDetail(DynamicNodeType.Action, "Js获取元素内部属性")] + + [MethodDetail(DynamicNodeType.Action, "Js获取元素属性")] public string GetAttribute(IWebElement element, [Explicit] string attributeName = "") { return element.GetAttribute(attributeName);