如何将 Selenium SelBlocks 插件合并到自定义的基于 Selenium 的 C# 项目中?
How to incorporate Selenium SelBlocks plugin into a custom Selenium-based C# project?
我正在从事基于 Selenium 的浏览器自动化项目,其中一些非编程用户在 Selenium IDE 中定义测试用例,然后我们通过我们的自定义添加一些自定义逻辑和 运行应用。我们的应用程序是用 C# 编写的,但基本上与 Selenium HTML Runner java 应用程序的工作方式类似。
现在我们到了测试用例中需要循环的地步,因此我们在 Selenium IDE 中安装了 SelBlocks 插件。为了让它在我们的自定义应用程序中也能正常工作,我希望我们可以使用 SelBlocks 用户扩展 JS 文件(从这里 https://raw.githubusercontent.com/refactoror/SelBlocks/master/user-extensions.js)。
但是,我无法使用 WebDriverBackedSelenium 和我们正在使用的 ChromeDriver 进行此操作。
如果我在我的 WebDriverBackedSelenium 对象上使用 ExecuteScript(),我会收到 UnsupportedOperationException。 (之前调用过对象上的 Start() 方法!)根据 API 的描述,这听起来像是全局注册 SelBlocks 最合适的方法 JavaScript 代码:
_selenium.AddScript(selBlocksScript, "selBlocks"); // throws UnsupportedOperationException
轨迹轨迹:
bei Selenium.WebDriverCommandProcessor.Execute(String commandName, String[] args)
bei Selenium.WebDriverCommandProcessor.DoCommand(String command, String[] args)
bei RHoDOS.CoreTestCase.Run(TestState state, Results results, IWebDriver driver) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\CoreTestCase.cs:Zeile 86.
bei RHoDOS.HtmlLauncher.LaunchHtmlTest(String testTitle, String filepath, WebDriverBackedSelenium selenium, IWebDriver driver, Results results) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 71.
bei RHoDOS.HtmlLauncher.RunHtmlSuite(String filepath) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 110.
bei RHoDOS.Program.Main(String[] args) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\Program.cs:Zeile 36.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
然后我尝试在我的 ChromeDriver 对象上使用 ExecuteScript(这是一种不太合适的方法,因为它会在当前加载的网页的上下文中执行脚本,所以我假设我会一次又一次地调用它,每次在我使用 SelBlocks 扩展方法之前)。
调用如下所示:
((IJavaScriptExecutor)driver).ExecuteScript(selBlocksScript, null);
Chrome 浏览器 returns LOG 对象未知的消息。这是堆栈跟踪:
bei OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1271.
bei OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1070.
bei OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1154.
bei OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 520.
bei RHoDOS.CoreTestCase.Run(TestState state, Results results, IWebDriver driver) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\CoreTestCase.cs:Zeile 87.
bei RHoDOS.HtmlLauncher.LaunchHtmlTest(String testTitle, String filepath, WebDriverBackedSelenium selenium, IWebDriver driver, Results results) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 71.
bei RHoDOS.HtmlLauncher.RunHtmlSuite(String filepath) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 110.
bei RHoDOS.Program.Main(String[] args) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\Program.cs:Zeile 36.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
我应该如何继续将 SelBlocks 脚本(或一般的用户扩展)添加到自定义的 Selenium WebDriver 项目中?或者关于 Selenium 2(又名 WebDriver)中的用户扩展的讨论,如可以发现的那样 here or here,是否意味着我需要自己实现所有 SelBlocks 函数才能使它们在 WebDriver 中可用?
使用自定义 java 脚本扩展的方法在 WebDriver 中不再有效。所以我确实在我自己的应用程序中实现了SelBlocks插件所需的所有功能,这与Selenium项目的HTML RunnerHTML Runner on Github不相上下。
这是一个关于如何实现的粗略草图,假设熟悉 HTML Runner classes 以及 SelBlocks JavaScript 代码:
我写了一个 BlockDefinition class,它包含与 SelBlocks js 中的 blockDefs 对象相同的信息(即特定块的开始和结束位置的索引)。 CoreTestCase class(来自 HTML 运行ner)已扩展为实例化 BlockDefinition class 的对象并编译 Selenium 测试用例 HTML(cf .SelBlocks js 中的 compileSelBlocks() 函数)。 BlockDefinitions 被写入 TestState 对象,因此每个 CoreStep 对象都可以访问它们。
我为我想要的每个 SelBlocks 命令定义了一个 CoreStep class(为我不需要的命令定义了空存根 classes)并将它们注册到 NonReflectiveSteps 对象中(cf. HTML亚军)。
我的 NextStepDecorator 已获得一个名为 "NextBlockCommand" 的 getter / 属性,其中 return 为 "GoToNextCommand"(按顺序)、"JumpToBlockStart" 或 "JumpToEndOfBlock"。 CoreTestCase 对象在每个核心步骤执行后评估此 return 值,并使用 BlockDefinitions 对象来决定接下来 运行 哪个核心步骤。
诚然,这是一个非常粗略的草图。如果太简短而您对细节感兴趣,请询问。
我正在从事基于 Selenium 的浏览器自动化项目,其中一些非编程用户在 Selenium IDE 中定义测试用例,然后我们通过我们的自定义添加一些自定义逻辑和 运行应用。我们的应用程序是用 C# 编写的,但基本上与 Selenium HTML Runner java 应用程序的工作方式类似。
现在我们到了测试用例中需要循环的地步,因此我们在 Selenium IDE 中安装了 SelBlocks 插件。为了让它在我们的自定义应用程序中也能正常工作,我希望我们可以使用 SelBlocks 用户扩展 JS 文件(从这里 https://raw.githubusercontent.com/refactoror/SelBlocks/master/user-extensions.js)。
但是,我无法使用 WebDriverBackedSelenium 和我们正在使用的 ChromeDriver 进行此操作。 如果我在我的 WebDriverBackedSelenium 对象上使用 ExecuteScript(),我会收到 UnsupportedOperationException。 (之前调用过对象上的 Start() 方法!)根据 API 的描述,这听起来像是全局注册 SelBlocks 最合适的方法 JavaScript 代码:
_selenium.AddScript(selBlocksScript, "selBlocks"); // throws UnsupportedOperationException
轨迹轨迹:
bei Selenium.WebDriverCommandProcessor.Execute(String commandName, String[] args)
bei Selenium.WebDriverCommandProcessor.DoCommand(String command, String[] args)
bei RHoDOS.CoreTestCase.Run(TestState state, Results results, IWebDriver driver) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\CoreTestCase.cs:Zeile 86.
bei RHoDOS.HtmlLauncher.LaunchHtmlTest(String testTitle, String filepath, WebDriverBackedSelenium selenium, IWebDriver driver, Results results) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 71.
bei RHoDOS.HtmlLauncher.RunHtmlSuite(String filepath) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 110.
bei RHoDOS.Program.Main(String[] args) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\Program.cs:Zeile 36.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
然后我尝试在我的 ChromeDriver 对象上使用 ExecuteScript(这是一种不太合适的方法,因为它会在当前加载的网页的上下文中执行脚本,所以我假设我会一次又一次地调用它,每次在我使用 SelBlocks 扩展方法之前)。
调用如下所示:
((IJavaScriptExecutor)driver).ExecuteScript(selBlocksScript, null);
Chrome 浏览器 returns LOG 对象未知的消息。这是堆栈跟踪:
bei OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1271.
bei OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1070.
bei OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 1154.
bei OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args) in C:\Users\U0558\Downloads\selenium-master\selenium-master\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:Zeile 520.
bei RHoDOS.CoreTestCase.Run(TestState state, Results results, IWebDriver driver) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\CoreTestCase.cs:Zeile 87.
bei RHoDOS.HtmlLauncher.LaunchHtmlTest(String testTitle, String filepath, WebDriverBackedSelenium selenium, IWebDriver driver, Results results) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 71.
bei RHoDOS.HtmlLauncher.RunHtmlSuite(String filepath) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\HtmlLauncher.cs:Zeile 110.
bei RHoDOS.Program.Main(String[] args) in D:\NB00382\Documents\Visual Studio 2015\Projects\RHoDOS-Dev1\RHoDOS\Program.cs:Zeile 36.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
我应该如何继续将 SelBlocks 脚本(或一般的用户扩展)添加到自定义的 Selenium WebDriver 项目中?或者关于 Selenium 2(又名 WebDriver)中的用户扩展的讨论,如可以发现的那样 here or here,是否意味着我需要自己实现所有 SelBlocks 函数才能使它们在 WebDriver 中可用?
使用自定义 java 脚本扩展的方法在 WebDriver 中不再有效。所以我确实在我自己的应用程序中实现了SelBlocks插件所需的所有功能,这与Selenium项目的HTML RunnerHTML Runner on Github不相上下。 这是一个关于如何实现的粗略草图,假设熟悉 HTML Runner classes 以及 SelBlocks JavaScript 代码:
我写了一个 BlockDefinition class,它包含与 SelBlocks js 中的 blockDefs 对象相同的信息(即特定块的开始和结束位置的索引)。 CoreTestCase class(来自 HTML 运行ner)已扩展为实例化 BlockDefinition class 的对象并编译 Selenium 测试用例 HTML(cf .SelBlocks js 中的 compileSelBlocks() 函数)。 BlockDefinitions 被写入 TestState 对象,因此每个 CoreStep 对象都可以访问它们。
我为我想要的每个 SelBlocks 命令定义了一个 CoreStep class(为我不需要的命令定义了空存根 classes)并将它们注册到 NonReflectiveSteps 对象中(cf. HTML亚军)。 我的 NextStepDecorator 已获得一个名为 "NextBlockCommand" 的 getter / 属性,其中 return 为 "GoToNextCommand"(按顺序)、"JumpToBlockStart" 或 "JumpToEndOfBlock"。 CoreTestCase 对象在每个核心步骤执行后评估此 return 值,并使用 BlockDefinitions 对象来决定接下来 运行 哪个核心步骤。
诚然,这是一个非常粗略的草图。如果太简短而您对细节感兴趣,请询问。