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]"));

Saturday, March 21, 2015

When to use Selenium server with Selenium WebDriver?

There are some scenario where we have to use the Selenium-Server with Selenium-WebDriver.
  1. If we are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  2. If we want to connect to a remote machine that has a particular browser version that is not on our current machine.
  3. If we are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HTMLunit driver

How to play with excel sheet using APACHE POI and read excelsheet in Selenium WebDriver fraemwork ?

We can read excel using APACHE POI. its Java API to work with excel sheet.


HSS stands for Horrible spread sheet.

public FileInputStream instr;
public HSSFWorkbook wb = null;
public HSSFSheet ws = null;


            File file = new File(System.getProperty("user.dir"));
            DataFormatter dt = new DataFormatter();

            instr = new FileInputStream(file.getAbsolutePath() + "/Common/Config.xls");
            wb = new HSSFWorkbook(instr);
            ws = wb.getSheet("config");
        
           int row_count = ws.getLastRowNum() +1;           //to get count of total rows...
           int cell_count = ws.getLastCellNum() +1;  

           HSSFRow row = ws.getRow(0);                    //To get 1st row
           String column1text = dt.formatCellValue(row.getCell(0); //read text from first row and column




   

Friday, March 20, 2015

Difference between Selenium 1.0 and Selenium 2.0?

Selenium 1.0 also known as Selenium RC and Selenium 2.0 known as Seleniuim WebDriver.
Differences as below...

Selenium 1.0 (RC) Selenium 2.0 (WebDriver)
Need to start Remote server before execution.No need to start any server.
Support some of the browser like.. IE, FireFox, Chrome and Opera. Can not support headless HTMLunit driverSupport all the browser like.. IE, FireFox, Chrome ,Opera, headless HTMLunit, Android
Core engine is Javascript basedInteracts natively with browser application
Very complex architectureSimple and easy architecture
It calls Selenium Core to execute and control browserDirectly interact with browser
API are more matured but contain redundancy and often confusingAPI's are simple and no redundancey.
Built in test result generator and automatically generate HTML fileNo Built in result file generator
It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browserSelenium-WebDriver makes direct calls to the browser using each browser’s native support for automation

Thursday, March 19, 2015

What is Selenium 2.0?


Selenium 2.0 is latest version of selenium open source tool. Selenium 2.0 also known as Selenium WebDriver. Selenium is automation testing tool for Web based application. WebDriver is a name of API. We cannot automate desktop application using Selenium open source tool. older version of Selenium is Selenium 1.0 also known as Selenium RC.