Webdriver tips

1 minute read

Webdriver is still a useful testing framework, thought phantom.js may be a better fit for javascript-intensive for SPA(Single Page Application).

The below are a few usages I encounter often.

XPath element locator

Very common in locating elements. There is a simple XPath tutorial at W3School, though I'm not a big fan of W3School.

[sourcecode language="csharp"]
public MetaData ClickConfirmButton()
var okButton = new Button("//*[contains(@id,'confirm-edit')]");
return this;

A quick syntax I ofetn come across is

[sourcecode language="xml"]
//*[contains(@data-test, 'row')][3]//*[contains(@data-test,'note')]

This means, to find an element that has "data-test" attribute with the value of "row" and go the the third element within it, and find an element with "note" value, there. (does it make sense?)

wait until a page is loaded

One big change from Selenium RC to Webdriver is waiting for loading. By default, Webdriver doesn't wait for a page or element is fully loaded. But you can use a handy wait command.

[sourcecode language="csharp"]
new WebDriverWait(_driver, TimeSpan.FromSeconds(10))
.Until(w => w.Url.Contains("workspace"));

Install the latest flash player for firefox

Once I was testing a page that renders a flash content. The page opens without any problem on Chrome, but not on Firefox. Firefox just get frozen, and the automation test fails miserably. Chrome has its own flash player, so it's updated when the browser is updated, but it's not the case for Firefox. If firefox freezes over flash content, install the latest Flash Player. Otherwise, you will be pulling your hair, condemning Firefox harshly...

Testing "Click to download a file" stuff

By default, firefox pops up the download manager when you try to download a file. Also, the download path can be different per machine. You wouldn't want manager pop-up, as you don't like alert dialogue box. You can set a silent file download on firefox profile.

[sourcecode language="csharp"]
var firefoxProfile = new FirefoxProfile

firefoxProfile.AcceptUntrustedCertificates = true

firefoxProfile.SetPreference("browser.download.dir", "c:\temp\");
firefoxProfile.SetPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.SetPreference("browser.download.panel.removeFinishedDownloads", true);
firefoxProfile.SetPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.SetPreference("browser.download.folderList", 2);
firefoxProfile.SetPreference("browser.download.manager.closeWhenDone", true);
"application/zip, application/x-zip, application/x-zip-compressed, application/pdf, text/plain, application/octet-stream, application/vnd.ms-excel, application/msword, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.presentationml.presentation"

You would need to know the mime tppe of the files you want to download silently.
If you want to know more preferences, please visit Mozilla's knowledge base.

to be continued ...