This post shows how I used the O2 Platform's FluentSharp REPL script_Me capabilities to debug the problem and find a solution.
In the TeamMentor's UserTests here is one that was failing:
Source code of test shown above, with the error occurring on the selected line (with clickButton.Click() )
To debug this issue, lets open up a C# Repl on the beginning of the test (using FluentSharp’s script_Me Extension Method)
Running this UnitTest
... will open up the browser and the REPL gui
Note: the error shown above is a known bug in the O2 Platform REPL generator, and it is due to the fact that the unitTest variable (passed as a parameter to the REPL GUI) is a collection. This is a minor bug in, which can be dealt with by making the parameter a generic value:
... and adding a couple references and using statements:
With this set-up we can now control the Chome WebDriver from this REPL Scripts.
1) Get a reference to the teamMentor object and call the homePage method:
2) get a strongly typed reference to the ChromeDriver object and invoke Navigate and Refresh
3) Get XPath using private field value
4) get element reference (in this case the ‘OWASP Top 10’ folder from the OWASP library)
5) Click on element and the same error we had when executing the UnitTest normally (first screenshot).
Basically the problem is that if that folder is currently not visible, the Click event cannot be invoked.
And just to confirm, if we manually expand that folder, we will get no errors:
To fix this we need to open up that folder programmatically.
In these cases I always like to look at jQuery and see if I can use it to address the issue.
So in another Chome window, I opened up the developer tools and used the same GUID as used in the UnitTest to find the folder:
Back into C# code, here are a couple Extension Methods that will allow us to do this programmatically:
… which can be used like this:
… and the script that opens the folder:
Next we create a generic lambda method to open the node, and loop it 5 times (for fun):
And now, the code from the original UnitTest (i.e the Click event) doesn’t fail anymore:
… and we can add the rest of the UnitTest (just to make sure it all works)
Note that I had to comment out the .Click event since it was closing down the OWASP Top 10 folder (which meant that nodes.first().Text would return an empty value)
Now that we have a fix, the final step is to close the REPL window and update the UnitTest with the new jQuery based commands
So back in the UnitTest file (the one that was failing before), I removed the clickButton code and added the code created in the REPL C# environment:
And the test now passes :)
To fix the other UnitTests that were also failing (on this class), I transformed the Lambda method into a normal C# function:
... which was used in this unit test:
… and this one
And finally, with these fixes, all unit tests now executed ok :)
- Coding Firefox in C# in real-time using Selenium's Firefox driver
- PoC - Selenium - Gui with 3 Hijacked Browser Windows.h2
- Using TeamCity and NUnit to Start WebServer, Run Selenium Tests and Stop WebServer
- OpenQA.Selenium.DriverServiceNotFoundException on Chrome
- Using Selenium to Login using Multiple Browsers
Scripts used in this post:
a) calling teamMentor.HomePage()
b) strongly-typed reference to ChromeDriver object
) Get JsTree using XPath
) get element and click on link
) Hello from C#
) loop 5 times
) final REPL code that contained the original UnitTest’s Asserts