/ testing

Selenium Best Practices

1) Use Robust object identification methods:

Preferred selector order: id > name >css>xpath

To locate an element we can use
the element’s ID

e.g- driver.findElement(By.id("LinkId")).click();

the element’s name attribute

e.g- driver.findElement(By.name("LinkName")).click();

by CSS statement

e.g- driver.findElement(By.cssSelector(“a#LinkId”)).click();

the element’s CSS statement

e.g- driver.findElement(By.css("LinkName")).click();

an XPath statement

e.g- driver.findElement(By.xpath([//div 2/a[text()=’LinkText’]")).click();

by a links text
e.g- driver.findElement(By.linkText("LinkText")).click();

document object model (DOM)
e.g- driver.findElement(By.xpath("div/div/a")).click();

In practice, you will discover

• id and name are often the easiest and sure way.

• xpath are often brittle. For example you have 3 tables displayed but sometimes there are no data and the table isn’t rendered, your xpath locating the second table will not always return the intented one.

• css are the way to goin conjunction of id and name !

• Locating links in an internationalized application is sometimes hard… try to use partial href.

2. Avoid Thread.sleep prefer WebDriverWait

Instead of sleep:

driver.findElement(By.id(“contacttypelink”)).click();

Thread.sleep(500);

Use Wait:

WebDriverWait wait=newWebDriverWait(driver, 30);

wait.until(ExpectedConditions.elementToBeClickable(By.id("LinkId")));

3. Keep your WebDriver up to Date.

Keep your WebDriver jar file up to date to support latest versions of browsers.
You can find the browser support for a particular version of WebDriver in its version change log details.

https://code.google.com/p/selenium/source/browse/java/CHANGELOG(https://code.google.com/p/selenium/source/browse/java/CHANGELOG)

4. Turn browser auto update OFF.

Make sure you turn off your browser auto update.
Because if your browser gets updated to a version which is not supported by WebDriver, then it might cause unconditional failures during test executions.
Always update your browser as per the WebDriver version change logs.

5. How to close Firebug startpage

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference(“extensions.firebug.currentVersion”, “25.0”);

driver = new FirefoxDriver(profile);

Note that you may need to adapt the version.

6. IE Not Working

Check:-

• The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.

• On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings,choose “Internet Options…” from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled “Enable Protected Mode”.

Note that you may need to adapt the version.

Possible workaround for Protected Mode

DesiredCapabilitiesieCapabilities = DesiredCapabilities.internetExplorer();

_ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY DOMAILS,true);

ieCapabilities.setCapability(“ensureCleanSession”, true);

driver = new InternetExplorerDriver(ieCapabilities);

7. Use RunTime object for initial clean up:

If you want to close all the Excel and browser instances before starting your Test Execution then you can use Runtime object to kill all the unwanted processes.
For E.g. In order to kill all the Excel instances. You can execute-

Runtime rt = Runtime.getRuntime();

rt.exec("taskkill /F /IM EXCEL.exe") ;

Similarly, to close all Firefox instances-

Runtime rt = Runtime.getRuntime();

rt.exec("taskkill /F /IM Firefox.exe") ;

8. Use Selenium IDE for Object Identification:

We can you Selenium IDE for finding the object location and also to test custom paths. After creating the custom css or xpaths you can use the IDE’s find button to validate the path

http://i.imgur.com/gPjq4PL.png