Monday, March 18, 2013

How to choose Ext JS Combo values using Selenium WebDriver

When we see Ext JS Combo box, it looks like a ordinary combo box and when we try

Select oSel=
new Select(oCategoryItems);
List<WebElement> oListItems=oSel.getOptions();

It throws an error message saying,
Element should have been "select" but was "input"

The reason being Ext JS combo box are not just combo boxes, they combination of controls like,
<Input> and <Image> or <em>
<input> and <Select>

You can see the sample combo box object in Naukri.com website, which is attached


So inorder to select these items, first we need to click on the Input object and then select the value.

Following is the code for printing out all the values, you can use whatever function you want after clicking the object.


 
import
java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.ui.Select;
 
public
class PrintListBoxValues
{
public static void main(String[] args)
{
WebDriver driver=
new FirefoxDriver();
driver.get(
"http://www.naukri.com/");
WebElement oCategory=driver.findElement(By.id(
"farea"));
oCategory.click();
WebElement oCategoryItems=driver.findElement(By.id(
"fareaSL"));
Select oSel=
new Select(oCategoryItems);
List<WebElement> oListItems=oSel.getOptions();
for(int i=1;i<=oListItems.size()-1;i++)
{
System.
out.println(oListItems.get(i).getText());
}
}
}

Sunday, March 17, 2013

How to switch between different windows using Selenium WebDriver

Inorder to switch between Windows we should be knowing the window handlers and traverse between windows.

For that i am opening the link in a new window using clicking down button, after that moving to the specified window.

Here is the code:
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class MoveBetweenTabs
{
    public static void main(String[] args)
    {
        WebDriver driver=new FirefoxDriver();
        driver.navigate().to("http://www.google.com");
       
        driver.manage().window().maximize();
       
        WebElement oWE=driver.findElement(By.linkText("About Google"));
       
        Actions oAction=new Actions(driver);
        oAction.moveToElement(oWE);
        oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
       
        Set<String> sHandlers= driver.getWindowHandles();
        for(String sHandler:sHandlers)
        {
       
            if(driver.switchTo().window(sHandler).getTitle().equals("Google"))
            {
                driver.switchTo().window(sHandler);
                WebElement oWE1=driver.findElement(By.linkText("+Google"));
                oWE1.click();
            }
        }
    }
}

How to right click and choose an option using Selenium WebDriver

There is no direct way to choose an option after right clicking using Selenium WebDriver.

For Ex: what i mean here is say you open google.com
and then right click on "About Google" and have to choose "Open Link in new Tab"

In Selenium WebDriver there is no direct way to do this.

The work around is clicking {DOWN} button. But there is a disadvantage in this approach, suppose if your options dynamically change then this approach wont work.

Here is the sample code for the above approach:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class RightClickAndChooseAnOption
{
    public static void main(String[] args)
    {
        WebDriver driver=new FirefoxDriver();
        driver.navigate().to("http://www.google.com");
       
        driver.manage().window().maximize();
       
        WebElement oWE=driver.findElement(By.linkText("About Google"));
       
        Actions oAction=new Actions(driver);
        oAction.moveToElement(oWE);
        oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
       
    }

}