In this article I’ll share with you 6 usefult tips that may help you when you work with Selenium WebDriver in Java.

1. How To Highlight an Element

While debugging, sometimes it is very helpful if the element we are going to interact with can be highlighted. The following code will create a border around the element we are going to interact with:

2. How To Verify Text Presence

WebDriver is far better in many ways than Selenium RC, however if you have migrated away from Selenium RC, you will miss a few functions, and one of the functions is isTextPresent. It is not complicated to implement, even though it is not available in WebDriver:

3. Never Hard Code Waiting for an Element

I have seen many testers use hard coded sleep (Thread.sleep()) to wait till some element appears on the page. It isn’t a bad solution, as it works most of the time if you have given a high enough wait time; however it affects the performance of the test. For example, the below code will wait for 3 minutes, even if the element appears within 1 minute:

However, if we use the explicit wait provided by WebDriver, we can avoid this performance bottleneck. The following code will wait a maximum of 3 minutes However, if within this time the element appears, the following code will move ahead:

4. If Click is not working – use JavaScript

There may be times when the click API provided by WebDriver is not working, or it is not returning the control . You can use JavaScript to click on the element at this time:

5. How To Bypass File Download Dialog Box in Firefox

One thing which always creates trouble is WebDrivers inability to handle dialog boxes. File Download Dialog box is one of them. The following trick will help you to handle the file download dialog box for Firefox:

6. How To Change the User Agent for Firefox

Sometimes you need to check how your website is going to look on android devices. For this, rather than running your test cases on an actual android device, you can change Firefox on Windows to give you the same look and feel as for an andriod. You can achieve this by changing the Firefox user agent. The following is the way you can do this: