As you probably know I am developing a series posts called- Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writing automation tests with WebDriver. Also, contain a lot of more advanced topics such as automation strategies, benchmarks and researches. In the next couple of publications, I am going to share with you some Advanced WebDriver usages in tests. Without further ado, here are the today's advanced WebDriver Automation tips and trips.

1. Taking a Screenshot

You can use the following method to take a full-screen screenshot of the browser.

Sometimes you may need to take a screenshot of a single element.

First we make a full-screen screenshot then we locate the specified element by its location and size attributes. After that, the found rectangle chunk is saved as a bitmap.

Here is how you use both methods in tests.

We get a temp file name through the special Path . NET class. By default temp files are generated with ".tmp" extension because of that we replace it with ".png".

2. Get HTML Source of WebElement

You can use the GetAttribute method of the IWebElement interface to get the inner HTML of a specific element.

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research and afterward we used many of the tricks in lots of the features of our solution.

3. Execute JavaScript

You can use the interface IJavaScriptExecutor to execute JavaScript through WebDriver.

This test is going to take the current window's title via JavaScript and print it to the Debug Output Window.

4. Set Page Load Timeout

There are at least three methods that you can use for the job. First you can set the default driver's page load timeout through the WebDriver's options class.

You can wait until the page is completely loaded via JavaScript.

Your third option is to wait for a specific element(s) to be visible on the page.

5. Execute Tests in a Headless Browser

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. In order to be able to use the PhantomJSDriver in your code, you first need to download its binaries.

This test is executed under 3 seconds through the PhantomJSDriver. Almost three times faster than with the FirefoxDriver.

6. Check If an Element Is Visible You can use the Displayed property of the IWebElement interface.

7. Use Specific Profile By default WebDriver always assigns a new 'clean' profile if you use the FirefoxDriver's default constructor. However, sometimes you may want to fine-tune the profile e.g. add extensions, turn off the JavaScript, etc.

You can do some similar configurations for the ChromeDriver.

8. Turn Off JavaScript You can use the code from point 7 to set up a new Firefox profile. Then you need to set the 'javascript.enabled' attribute to false.

9. Manage Cookies Before you can work with the cookies of a site, you need to navigate to some of its pages. Add a New Cookie

Get All Cookies

Delete a Cookie by Name

Delete All Cookies

Get a Cookie by Name