Tuesday, March 24, 2015

How to play with findelements method in selenium webdriver ?

findElements method is used to locate multiple web element with same attribute.like all the element with type button on the page. It returns list of web element. if no element found then a empty list will be return. Like findelement it will not return null or raise any exception if no element found.

Code for play with findelements as below..

WebDriver driver = new InternetExplorerDriver();
List <webElement> elements = driver.findElements(By.className("button"));

for (WebElement element : elements )
   {
      String elmtext = element.getText();  / to get text from web element.
      boolean dis = element.isDisplayed(); //to check if web element is displayed.
    } 
 

 

Monday, March 23, 2015

How to play with WebDriver API and Open URL of the application in Selenium ?

First we need to set internet explorer exe path and initiate Webdriver using below code.

public class BrowserStart {
  
    public static WebDriver driver;
  
    public static void loadbrowser () throws InterruptedException{
      
        //Set Internetexplorer exe path..
      
        File file = new File(System.getProperty("user.dir") + "/libs/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
      
         driver = new InternetExplorerDriver();
        driver.get("http://www.google.com");

      }
}

How to play with Web Element on webpage in Selenium ?

1. Edit Box:


Enter "Hello" word in edit box...

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("txtbx"));
element.clear();    \\ Clear text box 
element.sendKeys("Hello");

2. Drop Down :

Select "India" from country drop down.

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("dcountry"));
new Select(element).selectByVisibleText("India");

Also we can select by index.
new Select(element).selectByIndex(1);
or
 new Select(element).selectByValue("India");

we can also deselect value from drop down using below code.
new Select(element).deselectByValue("India");

3. Radio button:

Select one of the type radio button.
WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("type"));
element.click();
element.isSelected; // To check if radio button is clicked.

in case of multiple radio button and if we want to select one of the radio button then we can use findByElements to locate list of the radio button.

List <WebElement> radiolist = driver.findElements(By.ID("type"));

4. Checkbox :

Select one of the option checkbo.
WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("option"));
element.click();
element.isSelected; // To check if check box is clicked.

in case of multiple checkboxes and if we want to select one of the checkbox then we can use findByElements to locate list of the checkbox.

List <WebElement> radiolist = driver.findElements(By.ID("option"));

5. WebButton:

To click on web Button or submit form details .

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("submit"));
element.click();
or
element.submit();

element.isEnabled();        //  To check in button is enable or dissable..
element.isDisplayed();    // To check if button is visible ot not..
 

Sunday, March 22, 2015

Difference between findelement () and findelements ()..???

findElement :

 findElement method is used to locate single element .findelement method returns first matching element of given locator found on the web page . Return type is WebElement object. If no element found then it throw nosuchelement exception.

WebDriver driver = new internetExplorerDriver();
WebElement element = driver.findElement(by.ID("txtbox"));

findElements:

findelements method used to locate multiple element on web page with given attribute. like all the check boxes with same ID. Return type is list. if no element found then return empty list of webElement object.

List<WebElement> types= driver.findElements(By.xpath("//type")); 
int size = types.size();
 

Different types of Web element locator in Selenium WebDriver


Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement.we have to use finelement method. it returns WebElement object if element found else returns nosuchelementfound exception.
Different types of locator as below.

1.  By.ID: 

Locating web element by ID is simplest and easy way. Use this when we know id attribute of an element

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.ID("txtboxid"));

If web element ID not defined by UI developer or element ID is not unique or its generated at run time then we can not use By.ID locator.

2. By.Name : 

Like ID we can also use Name attribute of web element to locate the same.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.Name("txtboxname"));

3. By.className:

We can also class attribute to locate web element . In case of multiple element with same class name than it returns first element with classname.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.className("txtclassname"));

4. By.linkText

 We can use link text to locate a link element.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.linkText("txtlink"));

5. By.partialLinkText

Like linkText we can also locate link element by partial link text.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.partialLinkText("txtlinktext"));

6.  By.Xpath:

Web element can be located using xpath. At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible.  Xpath is useful when we don't know ID or Name attribute.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.Xpath("//html/body/form[1]"));

7. By.CSS Selector:

Like Xpath we can also use CSS selector. CSS is cascade style sheet.

   WebDriver driver = new internetExplorerDriver();
   WebElement elm = driver.findElement(By.cssSelector("//html/body/form[1]"));