Some other helpful Selenium methods

Here are a few other helpful functions for use of Selenium testing scripts as you often need to click links, fill in fields, and submit forms.


import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
*
* @param driver
* @param name
* @return
*/
public static WebElement findElementByName(final WebDriver driver, final String name){
final By el = By.name(name);
final WebElement wel = driver.findElement(el);
return wel;
}
/**
*
* @param driver
* @param name
* @param value
*/
public static void sendKeysByFieldName(final WebDriver driver, final String name, final String value){
final WebElement wel = findElementByName(driver, name);
wel.sendKeys(value);
}
/**
*
* @param driver
* @param xpath
*/
public static void clickByXpath(final WebDriver driver, final String xpath){
final By el = By.xpath(xpath);
//LOGGER.info("el is {}", el);
final WebElement wel = driver.findElement(el);
wel.click();
}
/**
*
* @param driver
* @param linktext
*/
public static void waitToClickLinkText(final WebDriver driver, final String linktext){
final WebDriverWait wait = new WebDriverWait(driver, 10);
final By el = By.linkText(linktext);
wait.until(ExpectedConditions.elementToBeClickable(el));
final WebElement wel = driver.findElement(el);
wel.click();
}
/**
*
* @param driver
* @param text
* @return
*/
public boolean pageContainsText(final WebDriver driver, final String text){
final String xpathExpression = "//*[contains(text(),'" + text + "')]";
final List<WebElement> list = driver.findElements(By.xpath(xpathExpression));
return list.size() > 0;
}

Hiding Firebug Lite controls in browser

I started getting extremely tired of the FireBug Lite overlaying content in some of my legacy websites. By taking a look at the markup it was adding, I found a quick and easy way to hide it for most users.

Adding the following to one of your CSS files should do the trick…


/* Begin hide FireBug Lite */
#jsConsole,
#jsConsoleShowSourceButton,
#jsConsoleHideSourceButton,
#jsConsoleShowConsoleButton,
#jsConsoleHideConsoleButton { display:none; }
/* End hide FireBug Lite */

NOTE: I have not yet confirmed, but this approach should work in other browsers such as MSIE, Chromium and Safari that also may use FireBug Lite.

Use ARAI role=”button”

With the widespread use of Rich Web applications, links and other HTML tags are are often used to create buttons. When an element is focused, screen readers announce its tag name. That means users of screen readers will be unaware of the intention of the markup used. For links, the value of the link’s href may be announced and confuse the users. The addition of the ‘role’ attribute helps with semantics and with accessibility tools such as JAWS. In addition, you’ll also want to verify that the tabindex is set to a valid value to allow for keyboard navigation of the page.

Some examples:

<a role="button" href="#">Click Me</a>
<a role="button" href="javascript:callback();">Click Me</a>
<a role="button" tabindex="0">Click Me</a>
<img role="button" tabindex="0" src="example.gif" alt="Click Me" />

REFERENCES:

META Tag ‘MSThemeCompatible’

Okay, so this one’s a little old, and I just found it while looking at some of Microsoft Update’s HTML source, it appears to be relevant for MSIE6 and newer and may be responsible for some interesting styling and behaviour of form components.

A quick search for it turns up lots of discussions about other browers such as Firefox being effected if the value is not defined… as such it’s likely a good idea to define it in your pages to be sure.

An old MSDN entry reads…

When running on Windows XP, Internet Explorer 6 and the content displayed in it sports a look and feel that matches the Windows XP platform. You can opt to have your HTML content not take on the same look as the operating system for elements such as buttons and scroll bars, by adding the following META tag:
<meta http-equiv="MSThemeCompatible" content="no" />

Setting this will disable theme support for the document. Some background on this, Windows XP (MSIE6) allows for the use of themes for the operating system to change the general color scheme of many elements.
As such, many HTML components (such as SELECT dropdowns, BUTTONS and INPUT fields ‘MAY’ also be effected if you don’t explicitly prevent it in your code.

There was some support for this in Mozilla Firefox builds for Windows, as such, while I’d normally recommend using a conditional comment, I’m torn in this case.

Cheers

Gradient HTML

Provided that all of the ‘buttons’ in your application already use the HTML <button> tag, this is a simple matter to accomplish:
1. Modify your CSS to include the following:

button{background:#eee url(../images/button.png);}

2. Upload the gradient image that you intend to use. (in this example button.png)

3. Done.

For your convenience, the image I use is available here (Gradient Button Background Image)

NOTE: MSIE exhibits some poor caching behaviours when background images are used, look for a post on this elsewhere in my blog.