Tuesday, October 25, 2011

Sending KeyStrokes

Sending key combination using Selenium-WebDriver

public void fn_sendKeys() {
String strURL;
WebElement txtGoogle;
WebDriver firefox=new FirefoxDriver();

strURL="www.google.com"
firefox.get(strURL);
txtGoogle=firefox.findElement(By.name("q"));

txtGoogle.sendKeys(Keys.CONTROL,"G");//Sending Ctrl+G key combination
}

Monday, October 24, 2011

Checking and Unchecking Web Checkbox

We can use the below methods to check or uncheck web checkbox.


//Checking
public void CheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true){
System.out.println("Checkbox is already checked");
}
else
{
chkbx1.click();
System.out.println("Checked the checkbox");
}
}

//Unchecking
public void UnCheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true) {
chkbx1.click();
System.out.println("Checkbox is unchecked");
}
else
{
System.out.println("Checkbox is already unchecked");
}
}

Thursday, October 20, 2011

Selecting Multiple Items in Listbox

Selecting multiple HTML Listbox's options

String strURL;
strURL="URL";

WebDriver firefoxDriver=new FirefoxDriver();
firefoxDriver.get(strURL);

WebElement list1;
list1=firefoxDriver.findElement(By.name("lst"));

List<WebElement> lstOptions=list1.findElements(By.tagName("option"));
list1.sendKeys(Keys.CONTROL);
lstOptions.get(0).click();//Selects the first option.
lstOptions.get(1).click();//Selects the second option.

Tuesday, October 18, 2011

Handling Javascript Popups Using Selenium-WebDriver

We can use the below methods to handle Javascript popups.

# accept()
This method can be used to click 'OK' button in a popup

WebDriver firefoxDriver=new FirefoxDriver();
Alert alert = firefoxDriver.switchTo().alert();//Creating object for Alert class
alert.accept();//Clicking OK button

# dismiss()
Canceling popup using dismiss method

WebDriver firefoxDriver=new FirefoxDriver();
Alert alert = firefoxDriver.switchTo().alert();
alert.dismiss();//Canceling the popup

# getText()
Getting popup text

String strAlertText;
WebDriver firefoxDriver=new FirefoxDriver();
Alert alert = firefoxDriver.switchTo().alert();
strAlertText=alert.getText();//This line retrieves text from popup.
System.out.println(strAlertText);