Webdriver tips

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.

public MetaData ClickConfirmButton()
{
	var okButton = new Button("//*[contains(@id,'confirm-edit')]");
	okButton.Click();
	return this;
}

A quick syntax I ofetn come across is

//*[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.

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.

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);
firefoxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", 
    "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 …

Webdriver tips

Remove the annoying certificate error page when running selenium tests

Selenium and webdriver tests are everywhere these days. They can be often slow and flaky, but you need to run them to make sure you didn’t break anything.

cf.For more information on selenium webdriver, go to selenium webdriver documentation.

Often what I find is I run selenium tests and start doing something as they take time to run. Then the tests are not running. Firefox stopped with certificate error! You have to add the certificate again to run those tests. It happens again and again, and as you are not a machine, you get tired mentally and emotionally. This is my story with selenium tests, so far.

There are a few ways to get around this certificate error. (I can’t say it’s annoying, as it warns you against the fraudulent websites.)

cert_override.txt

This is a text file gerated in the user profile to store SSL certificate expections, if a user specifies it.
Here is an example.

Fields

Files are separated by a tab character, and each line is finished by a line feed character.

1. domainname:port : port 443 for HTTPS (SSL)
2. hash algorithm OID

  • SHA1-256: OID.2.16.840.1.101.3.4.2.1 (most used)
  • SHA-384: OID.2.16.840.1.101.3.4.2.2
  • SHA-512: OID.2.16.840.1.101.3.4.2.3

3. Certificate fingerprint using previous hash algorithm

4. One or more characters for override type:

  • M : allow mismatches in the hostname
  • U : allow untrusted certs (whether it’s self signed cert or a missing or invalid issuer cert)
  • T : allow errors in the validity time, for example, for expired or not yet valid certs

5. Certificate’s serial number and the issuer name as a base64 encoded string

Firefox profile

In order to edit the file, you would have to find the file first, and it was tricky to locate Firefox profile.
If you are on windows 7 or windows server 2008 like me, click on Windows Start and type %APPDATA%\Mozilla\Firefox\Profiles\ in the search box.
If you on a earlier version of windows, please go to a mozilla support page for the profile location.

Remove the annoying certificate error page when running selenium tests