0

I am working on Selenium-based browser automation project, where some non-programming users define test cases in Selenium IDE, which we then add some custom logic to and run through our custom application. Our Application is written in C#, but basically works in a similar way as does the Selenium HTML Runner java application.

Now we hit the point where we needed loops in our test cases, so we installed the SelBlocks plugins in Selenium IDE. To make that also work in our custom application, I hope we could use the SelBlocks user extension JS file (from here https://raw.githubusercontent.com/refactoror/SelBlocks/master/user-extensions.js).

However, I couldn't get this working with WebDriverBackedSelenium and the ChromeDriver we are using. If I use ExecuteScript() on my WebDriverBackedSelenium object, I receive an UnsupportedOperationException. (The Start() method on the object has been called before!) According to the API description this sounded like the most suitable approach for globally registering the SelBlocks JavaScript code:

_selenium.AddScript(selBlocksScript, "selBlocks"); // throws UnsupportedOperationException

The strack trace:

   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()

I then tried to use ExecuteScript on my ChromeDriver object (which is a less suitable approach, because it would execute the script in the context of the currently loaded web page, so I assume I would have to call it again and again, everytime before I use a SelBlocks extension method).

The call looks like this:

 ((IJavaScriptExecutor)driver).ExecuteScript(selBlocksScript, null);

The Chrome browser returns the message that the LOG object is unknown. Here's the stack trace:

   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()

How should I proceed to add the SelBlocks script (or user extensions in general) to a customized Selenium WebDriver project? Or does the discussion on user extensions in Selenium 2 (aka WebDriver), as can be found here or here, mean that I need to implement all SelBlocks functions by myself to make them availale in WebDriver?

Community
  • 1
  • 1
Kai Weber
  • 388
  • 4
  • 14

1 Answers1

0

The approach with custom java script extensions does not work in WebDriver anymore. So I did implement all the required functionality of the SelBlocks plugin in my own application, which is comparable with the Selenium project's HTML Runner HTML Runner on Github. Here's a rough sketch on how this could be accomplished, assuming familiarity with the HTML Runner classes as well as the SelBlocks JavaScript code:

I wrote a BlockDefinition class, which holds the same information as the blockDefs object in the SelBlocks js (i.e. indexes for starting and ending positions of specific blocks). The CoreTestCase class (from HTML runner) has been expanded to instantiate an object of the BlockDefinition class and to compile the Selenium test case HTML (cf. compileSelBlocks() function in the SelBlocks js). The BlockDefinitions are written into the TestState object, so they are accessible to the each CoreStep object.

I defined a CoreStep class for every SelBlocks command that I wanted (and empty stub classes for those that I don't need) and registered them in the NonReflectiveSteps object (cf. HTML Runner). My NextStepDecorator has been given a getter / property called "NextBlockCommand", which returns either "GoToNextCommand" (in sequence), "JumpToBlockStart" or "JumpToEndOfBlock". The CoreTestCase object evaluates this return value after every core step execution and uses the BlockDefinitions object to decide which core step to run next.

This is admittedly a very rough sketch. If it is too brief and you are interested in the details, just ask.

Kai Weber
  • 388
  • 4
  • 14