I’m no longer involved with the Watir project, but to answer all the emails/tweets about what happened to watirwebdriver.com, it seems to have been moved by the project to watir.github.io and it looks like they won’t be renewing/redirecting the old url.
Butch Mayhew asks…
I have noticed you blogging more about JS frameworks. How do these compare to Watir/Ruby? Would you recommend one over the other?
Whilst I see merit in both views: I still think having your automated acceptance tests in the same language as your application leads to better maintainability and adoptability.
But generally with the direction ES is going, writing page objects as classes is much nicer than using functions for everything as in ES5.
GhostDriver has been released which means it is now easy to run reliable headless WebDriver tests on Mac OSX.
Steps to get working on OSX
- First make sure you have homebrew installed
brew install phantomjs
which should install PhantomJS 1.8.1 or newer
- Run irb and start using GhostDriver!
require 'watir-webdriver' b = Watir::Browser.new :phantomjs b.goto "www.google.com" b.url #"http://www.google.com.au/" b.title #"Google"
And yes, in case you’re wondering, it does screenshots!
In watir-webdriver it is really easy to access both the webdriver driver and the watir browser objects from an element:
require 'watir-webdriver' b = Watir::Browser.new b.goto 'www.google.com' button = b.button(name: 'btnK') button.driver #webdriver button.browser #watir browser
I never knew how to do this in C# until someone named Robert left a comment yesterday on an old blog post with instructions on how to do so.
You can get the webdriver by using.
var driver = ((IWrapsDriver)webElement).WrappedDriver;
Neat. This means the C# check image present extension method I wrote about previously can be implemented directly from the element itself:
This means it can be simply called like:
instead of the cumbersome:
I didn’t realize it’s actually a little tricky to check that an image is loaded when using WebDriver. WebDriver will only complain if the image tag you’re looking for isn’t in the DOM, not if the image link is broken and not actually visible.
For example, in watir-webdriver (ruby), this doesn’t really work as I would expect as the image isn’t actually visible on the ‘brokenimage’ page.
require 'watir-webdriver' b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.image(id: 'watermelon').visible? #true but is not visible
b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.execute_script("return (typeof arguments.naturalWidth!=\"undefined\" && arguments.naturalWidth>0)", b.image(id: 'watermelon'))
b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.execute_script("return arguments.complete", b.image(id: 'watermelon'))
In C#, you can wrap this up into a WebDriver extension method so you can this directly from Driver passing in the image element.
If it’s important that images load correctly in your application, you should probably start putting some of these in your WebDriver page objects. It’s simple to write a verify images method on a page that iterates through each image in the DOM and checks that it’s visible using the techniques above. Have fun.
Update: 30 November
I wrote about a slightly more elegant C# approach to do this directly from the element.
My webdriver-user-agent gem now supports random user agents. This idea belonged to Christoph Pilka who released the webdriver-user-agent-randomizer gem and suggested that we merge this feature back into the orginal gem.
Well, I have done it and now you can access this functionality like so:
require 'selenium-webdriver' require 'webdriver-user-agent' driver = UserAgent.driver(:agent => :random) driver.execute_script('return navigator.userAgent') # random agent like "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.2) Gecko/20010726 Netscape6/6.1"
See README for full details.