WebDriver Download

Submitted by admin on 2011-11-15 22:58:56

The following URL is for downloading WebDriver libs:

http://code.google.com/p/selenium/downloads/list

Usually I download selenium-java-XX.jar.

You can download dotnet version: selenium-dotnet-XX.jar

The one with name "selenium-server-XX.jar" is the package with selenium RC.

You can see more information in that page.

WebDriver and Select

Submitted by admin on 2011-10-30 21:16:07

WebDriver provides simple and useful API for Select. With them you can easily deal with html Select with WebDriver. Please see the following sample code:

WebElement e = driver.findElements(By.id("select_id"));
Select select = new Select(e);
select.selectByValue("value"); // select by option value
select.selectByIndex(specific_index); // select by option index, start from "0"
select.selectByVisibleText("label_text"); // select by the visible option text

 

The above code show you how to select an option with WebDriver while the following is to deselect options:

// undo the selection for all options
select.deselectAll(); 

// undo selection by option index,  start from "0"
select.deselectByIndex(index);

// undo selection by option value of Select
select.deselectByValue("value");

// undo selection by option text of Select
select.deselectByVisibleText("text");

 

If you want to go through all options then this will help you a lot:

List options = select.getOptions();

 

Sometimes, you will operate on multi-selection Selects, below is also useful:

// check if the Select can be multiple selected
boolean isMultiple = select.isMultiple();

// return all the selected options
List selectedOptions = select.getAllSelectedOptions();

// only return the first one of all the selected options
WebElement option = select.getFirstSelectedOption();

 

WebDriver Select api makes your work efficient, right?

Use WebDriverWait to wait for AJAX/Javascript asynchronized refresh

Submitted by admin on 2011-10-30 20:22:32

Web Driver use Jna to drive local browser to test web application, it's really useful that we don't need to start Selenium Remote Control.

The advantage of browser driven testing is that it can test Ajax/Javascript.

In Selenium IDE we can use "waitForXXX" to wait until what we expected, but Web Driver is different from that, we need to write some additional code to handle that.

The following code is writen in Java.

	Function() {
	            public WebElement apply(WebDriver driver) {
	                return driver.findElement(locator);
	            }
	        };
	}

When complete the above code, you can use the following code to wait for your condition.

	WebDriverWait wait = new WebDriverWait(driver, 60);
	wait.until(presenceOfElementLocated(locator));

Related links: One Useful Class for WebDriver - A Package Class for WebDriverWait

HtmlUnit Http Headers Handling

Submitted by admin on 2011-10-26 13:02:35

How can I handle http headers with HtmlUnit? Easy!

1. Get http headers with Htmlunit.

List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();

String headerValue = webResponse.getResponseHeaderValue(headerName);

2. Add http headers to request with HtmlUnit.

WebRequest req = new WebRequest(new URL("http://sss.com"));
req.setAdditionalHeader(name, value); // or req.setAdditionalHeaders(Map<String, String> additionalHeaders); 
WebResponse webResponse = webClient.loadWebResponse(req);

3. Remove http headers from request with HtmlUnit.

webRequest.removeAdditionalHeader(name);

Look, easy?

Please post me if you have any questions about http headers handling in HtmlUnit.

How to: HtmlUnit Cookies Handling

Submitted by admin on 2011-10-24 20:12:18

HtmlUnit has its mechanism to handle Cookies which is useful for our Web Application testing.

1. Enable/disable Cookies with HtmlUnit.

webClient.getCookieManager().setCookiesEnabled(true);//enable cookies

webClient.getCookieManager().setCookiesEnabled(false);//disable cookies

To see if Cookies is enabled:

webClient.getCookieManager().isCookiesEnabled();

 

2. Get Cookies values with HtmlUnit.

public String getCookieValue(String cookieName) {
		Cookie cookie = webClient.getCookieManager().getCookie(cookieName);
		if(cookie != null){
			return cookie.getValue();
		}
		return "";
	}

Two other methods:

webClient.getCookieManager().getCookies(); //Returns the currently configured cookies, in an unmodifiable set.

webClient.getCookieManager().getCookies(URL url); //Returns the currently configured cookies applicable to the specified URL, in an unmodifiable set.

3. Set cookies values with HtmlUnit.

public void setCookieValue(String cookieName, String value){
		Cookie cookie = new Cookie(cookieName, value);
		this.webClient.getCookieManager().addCookie(cookie);
	}

4. Remove Cookies with HtmlUnit.

public void removeCookies(String cookieName, String value){
		Cookie cookie = new Cookie(cookieName, value);
		webClient.getCookieManager().removeCookie(cookie);
	}

To clear all cookies with HtmlUnit:

public void clearCookies(){
		webClient.getCookieManager().clearCookies();
	}

 

See all code above, you can build up your own class for HtmlUnit Cookies handling.

How To: WebDriver Wait for Element Present

Submitted by admin on 2011-10-23 12:56:21

How to wait for element present with WebDriver?

In the web world, there are more and more web sites utilize AJAX technology to refresh the partial page without reloading the whole page.

Therefore, in the testing world, we often need to type code to wait for some elements' presence to process the next testing steps.

How can we do that? The answer is WebDriverWait Class.

Related Topics: WebDriver Ajax, WebDriver Wait

I've a post in this blog talking about this, please see in this link: Use WebDriverWait to wait for AJAX/Javascript asynchronized refresh

Web Server Security Technical Implementation Guide

Submitted by admin on 2011-10-22 22:00:17

This document is for web masters or security specialists. Though it's an old doc (pressed in 2006), it still worth a read.

Click download here: Web Server Security Technical Implementation Guide

referral: http://whitepapers.hackerjournals.com/?p=4832

Javascript - Conversion between ASC and String

Submitted by admin on 2011-10-22 11:46:19

In Javascript we often convert ASC to Char or Char to ASC.

Here shows you the useful functions for both of them:


 

Get Current URL with PHP

Submitted by admin on 2011-10-19 14:44:59

In most of the cases when you are coding with PHP, you don't care about the page URL.

But when you are doing SEO for your site, you will take the page URL seriously. Because page URL is very important for ranking in Search Engine results.

Here is my story of how I introduce Get Current URL in PHP.

In order to filter out some bad links, like "http://ksblog.org/index.php?q=&id=", which hurt SEO on my site, I decided to redirect it to my home page.

At the beginning, I detected if the value of "q" and "id" parameters is empty string but the code seems a little redundant.

if(isset($_GET['q']) && strlen($_GET['q'])==0 && isset($_GET['id']) && strlen($_GET['id'])==0){
	redirect_301("http://ksblog.org");
	return;
}

To keep the code short I chenged the code to identify if the URL is exactly "/index.php?q=&id=", see the code below code with the PHP to get current page URL:

if($_SERVER['REQUEST_URI']=="/index.php?q=&id="){
	redirect_301("http://ksblog.org");
	return;
}

The code looks a little simpler but the first snippet looks more flexable.

Another useful PHP system variable to get you current server name(domain name) is $_SERVER['SERVER_NAME']

 

LPAD and RPAD functions for Javascript

Submitted by admin on 2011-10-18 22:57:38

If you use Oracle you may know it has two useful functions -- LPAD and RPAD.

Let me instruct how are these two functions useful.

A valid U.S. zip code contains 5 chars, but some times we store the zip code as integer in the data table or in the Excel without special treament.

Take zip code "02011" for example.

If we store it in Excel without any special treatment we will get "2011" from it, but we cannot use it because it is invalid. So we need to pad the first "0" back in the left of the string. How can you do that? Go through the following.

function lpad(originalstr, length, strToPad) {
    while (originalstr.length < length)
        originalstr = strToPad + originalstr;
    return originalstr;
}

function rpad(originalstr, length, strToPad) {
    while (originalstr.length < length)
        originalstr = originalstr + strToPad;
    return originalstr;
}