我正在从事基于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()方法以前已被调用过!)根据接口描述,这听起来像是全局注册SelBlocks JavaScript代码的最合适的方法:
_selenium.AddScript(selBlocksScript, "selBlocks"); // throws UnsupportedOperationException
strack跟踪:
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 (这是一种不太合适的方法,因为它会在当前加载的web页面的上下文中执行脚本,所以我假设每次在使用SelBlocks扩展方法之前,我都必须一次又一次地调用它)。
调用如下所示:
((IJavaScriptExecutor)driver).ExecuteScript(selBlocksScript, null);
Chrome浏览器返回日志对象未知的消息。下面是堆栈跟踪:
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()
我应该如何继续向定制的Selenium WebDriver项目添加SelBlocks脚本(或者一般的用户扩展)?或者,在Selenium 2(又称WebDriver)中关于用户扩展的讨论(可以在here或here中找到)是否意味着我需要自己实现所有SelBlocks函数才能在WebDriver中使用它们?
发布于 2017-04-20 08:12:05
使用自定义java脚本扩展的方法在WebDriver中不再有效。因此,我在自己的应用程序中实现了SelBlocks插件所需的所有功能,可以与Selenium项目的HTML Runner HTML Runner on Github相媲美。假设您熟悉HTML Runner类和SelBlocks JavaScript代码,下面将大致介绍如何实现这一点:
我编写了一个BlockDefinition类,它包含与SelBlocks js中的blockDefs对象相同的信息(即特定块的开始和结束位置的索引)。扩展了CoreTestCase类(来自HTML runner),以实例化BlockDefinition类的对象并编译Selenium测试用例。SelBlocks js中的compileSelBlocks()函数)。BlockDefinitions被写入到TestState对象中,因此每个CoreStep对象都可以访问它们。
我为我想要的每个SelBlocks命令定义了一个CoreStep类(对于那些我不需要的命令,定义了一个空的存根类),并在NonReflectiveSteps对象中注册了它们(参见。HTML Runner)。我的NextStepDecorator被赋予了一个名为"NextBlockCommand“的getter /属性,它返回"GoToNextCommand”(按顺序)、"JumpToBlockStart“或"JumpToEndOfBlock”。在每个核心步骤执行之后,CoreTestCase对象都会计算这个返回值,并使用BlockDefinitions对象来决定下一步运行哪个核心步骤。
无可否认,这是一个非常粗略的草图。如果它太简短,而你对细节感兴趣,那就问吧。
https://stackoverflow.com/questions/42063916
复制