Thursday, February 24, 2011

WebDriver - Handling listboxes

1. Select value from a listbox

public void selectValue(WebElement listbox, String value) {
        if (listbox != null && !value.isEmpty()) {
            List listValues = listbox.findElements(By.tagName("option"));
            for (WebElement option : listValues) {
                if (option.getText().equals(value)) {
                    option.setSelected();
                    break;
                }
            }
        }
    }


2. Get selected value from listbox

public String getSelectedValue(WebElement listbox) throws MyException {
        if (listbox == null) {
            throw new MyException("listboxValues = " + listbox);
        }
        List options = listbox.findElements(By.tagName("option"));
        for (WebElement option : options) {
            if (option.isSelected())
                return option.getText();
        }
        return null;
    }


3. Verfiy for a value in listbox
public boolean verifyListboxValue(WebElement listbox,String value) throws IllegalArgumentException {
        if (listbox == null) {
            throw new IllegalArgumentException("listboxValues = " + listbox);
        }
        List options = listbox.findElements(By.tagName("option"));
        for (WebElement option : options) {
            if (option.getText().equals(value))
                return true;
        }
        return false;
    }


 class MyException extends Exception{
.....
}

Thursday, February 17, 2011

Selenium RC supporting for Https certificate issue

Download latest selenium1.0.x and include the following code while starting selenium server from code.

RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setTrustAllSSLCertificates(true);
seleniumServer = new SeleniumServer(rcc);
seleniumServer.start();

Wednesday, February 16, 2011

Accepting Untrusted Certificates - Https Webdriver firefox

Code for resolving https issue :
1. Create a firefox new profile
Refer http://kb.mozillazine.org/Creating_a_new_Firefox_profile_on_Windows

Open firefox , open your application and accept the cerificate manually.
Now add below code in your script (assuming the newly created profile name is WebDriver2).

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver2");
profile.setAcceptUntrustedCertificates(false);
WebDriver driver = new FirefoxDriver(profile);


Wednesday, February 9, 2011

Selenium Tutorial

This tutorial explains how to use write selenium tests using frameworks like Junit , TestNG with Ant. Hope by the end of tutorial you will get a base framework to start your tests for your application under test.
First of all take your own time and go through the entire document available at http://seleniumhq.org/docs/index.html.
Now starts with Installation of required artifacts.



  1. Installing eclipse
    Your computer should have a working Java Runtime Environment (JRE) v6 to run Eclipse. If you do not already have it, follow the instructions for installing Java 6 before installing Eclipse. Regardless of whether or not you had an existing, prior version of Eclipse, click to open the local zip archive of the Eclipse program so that you can view the folder. Once downloaded zip file , extract the zip contents into a folder (eg: c:\eclipse ). Open eclipse editor by doubleclick eclipse.exe.