Saturday, September 14, 2013

Wednesday, April 17, 2013

What is the difference between WebDriver.close() and WebDriver.quit()

WebDriver.close() method closes the current window.

See the below example, here it opens the new link in a different window and upon executing the close() method, it closes the parent window and leaving the new window open.

WebDriver driver=new FirefoxDriver();
driver.get("http://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();
driver.close();

Whereas, WebDriver.quit() method quits the driver, and closing every associated window.
In the above example, if you use driver.quit() in place of close , then first opens "google" page and then open the "about google" in new window and closes both the windows.

Tuesday, April 16, 2013

How to check the object existance

Here in the below code, i am checking for an object existence.

    //Max_TimeOut variable holds the time in seconds for the maximum time to wait before the control flows to NoSuchElementException block.
    int Max_TimeOut=60;
    public boolean isObjExists(WebDriver driver,By locator)
    {
        //I am putting the code in try catch because if the object does not exist, it throws exception.
        try
        {
            //Before throwing exception, it will wait for the Max_timeout specified
            WebDriverWait wait=new WebDriverWait(driver,Max_TimeOut);
            wait.until(ExpectedConditions.elementToBeClickable(locator));
            //If the element found, then it returns true
            return true;
        }
        catch(NoSuchElementException exception)
        {
            //If the element is not found, then it returns false
            return false;
        }
       
    }

This function can be invoked in like below:
        By locator=By.name("Email");
        if(obj.isObjExists(driver, locator))
        {
            Reporter.log("Object exists");
            WebElement uNameElement=driver.findElement(locator);
            uNameElement.sendKeys("abcd");
        }
        else
        {
            Reporter.log("Object does not exist");
        }

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();
       
    }

}

Wednesday, February 13, 2013

How to execute JavaScript using Selenium WebDriver


With Java Script we can access the DOM Properties. By doing so we can get the properties values of those objects.

Say i have a Div or a Span control like below:
<Div id="SampleDiv1">I am in Div</Div> or
<Span id="SampleSpan1">I am in Span</Div>

Here we have two solutions to get the value of the objects.

Solution 1: Use the getAttribute method.
This method is described in below location:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute%28java.lang.String%29

So for ex above, we can use like:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getAttribute("innerHTML");
System.out.println(sReturnText);


Because in the above example we are trying to get text from the control we can directly use getText method like below:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getText();
System.out.println(sReturnText);

Solution 2: We can get the DOM properties be JavaScript
There might be situations you will be needing to run the JavaScript in your automation. To do this/cover the example follow below steps:
String sJScript = "return document.getElementById('SampleSpan1').innerHTML;";
String sReturnText = (String) ((JavascriptExecutor) driver).executeScript(sJScript);
 System.out.println(sReturnText);