Wednesday, April 15, 2015

Properties File Reading For SeleniumWebDriver

For testing purpose we need some data from user at run time.It can be provided from external file.There is a way a to do that using properties file.

Basically properties file is holding data in key value pair. Properties file may be in .xml format or in .properties format.

Simple Properties File Looks Like below

User.Name="TestUser"
User.Password="TestPassword"


Here User.Name is as Key And "TestUser" is as value.

Xml Properties file looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="User.Password">"TestPassword"</entry>
<entry key="User.Name">"TestUser"</entry>
</properties>


Here <entry key="User.Password"> key is "User.Password" and Value is "TestPassword"

Reading a properties file we need to initialize first 
        Properties prop=new Properties();

Then we need to load properties file according to the properties file type if it is normal properties then use this :
        prop.load(new FileInputStream("./propertiesFile.properties"));

or if properties file is xml then use
        properties.loadFromXML(new FileInputStream("./propertiesFile"));


Now we get the value from properties file using this code.

        System.out.println(properties.get("User.Name"));
        System.out.println(properties.get("User.Password"));


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package propertiesfilehandling;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesFileHandling {

    public static void main(String[] args) throws IOException {
        // Reading XML Properties File
        Properties properties=new Properties();
        properties.loadFromXML(new FileInputStream("./propertiesFile"));
        System.out.println(properties.get("User.Name"));
        System.out.println(properties.get("User.Password"));
       
        // Reading Properties File
        Properties prop=new Properties();
        prop.load(new FileInputStream("./propertiesFile.properties"));
        System.out.println(prop.get("User.Name"));
        System.out.println(prop.get("User.Password"));       
    }   
}




Friday, April 3, 2015

Different Type Of Annotation In Page Facory

Previously we use some annotation in Page Factory model to identify webelement.Here we learn some more different type of annotation in Selenium WebDriver for Page Factory pattern.

1.@FindBy

We can either use this annotation by specifying both "how" and "using" or by specifying one of the location strategies (eg: "id") with an appropriate value to use. Both options will delegate down to the matching By methods in By class. For example, these two annotations point to the same element :

                      @ FindBy(name = "q") 
                           WebElement search; 

                      @FindBy(how = How.Name, using = "q") 
                            WebElement search;

and these two annotations point to the same list of elements:

                      @FindBy(tagName = "a") 
                            List<WebElement> links; 
                      
                       @FindBy(how = How.TAG_NAME, using = "a") 
                             List<WebElement>
2.@FindAll
              @FindAll can contain multiple @FindBy and will return all the elements which matches any @FindBy in a single list.

3.@FindBys
             

                It is use to indicate that lookup should use a series of @FindBy tags in a chain. Mechanism used to locate elements within a document using a series of other lookups. This class will find all DOM elements that matches each of the locators in sequence, e.g.

                         driver.findElements(new ByChained(by1, by2))

will find all elements that match by2 and appear under an element that matches by1.Syntax to use FindBys is like that 
                               @FindBys({@FindBy(id = "gb119"),
                                                   @FindBy(name = "q")})


                                     
                                           WebElement search;
 In @FindBys it can return multiple WebElement it depends upon our use of @FindBy tag if It return multiple element then syntax should be looks like below

                               @FindBys({@FindBy(id = "gb119"),
                                                   @FindBy(name = "q")})

                                     
                                           List<WebElement> search;



4.@CacheLookup:
             We may use same WebElement repetitively on the same page and whenever we try to access this element, Selenium WebDriver again find this element on the page newly. But it does not require to find every time if this element is not an Ajax element or it is not used in any other page. If it is used in different page then name of this element may be same but element is different from previous one. So if we use this annotation for any particular WebElement then once Selenium WebDriver find this element again it will not try to find this element into the page.
                             
                             @FindBy(name = "q")})
                             @CacheLookup

                                         List<WebElement> search;



We can understand better in the below code :


//*************************************** 
// GOOGLEPAGE Class
//***************************************
 
package testng;

import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.PageFactory;

public class Google {

    /**
     * *****************************************************************
     * Here we use @FindBy(name="q") to identify the search WebElement We can
     * use this syntax also @FindBy(how = How.NAME, using= "q")
     *
     * @CacheLookup is used to cached the webelement.Using this WebDriver not
     * try to find everytime this element,Once it find it will store for future
     * reference. @CacheLookup can be used with @FindBys or @FindAll
     */

    @FindBy(name= "q")
            @CacheLookup
        WebElement searchUsingFindBy;


    /**
     * ****************************************************************
     * For FindBys it is not necessary that to find element We always follow the
     * full DOM Path.So name="q" may be the next under of the other element but
     * we can use any of the ancestor.
     */

    @FindBys({
        @FindBy(id = "tsf"),
        @FindBy(name = "q")})

    WebElement searchUsingFindBys;

    /**
     * *****************************************************************
     * For FindAll it return all matching Element but it does not give any
     * guaranty to maintain any order to return element.So may be @FindBy(name =
     * "q") store as first WebElement into the list and
     * @FindBy(id = "gb119") is last one.
     */

    @FindAll({
        @FindBy(id = "gb119"),
        @FindBy(name = "q")})
    List<WebElement> searchUsingFindAll;

    public Google sendTxtFindBys(WebDriver driver) {
        searchUsingFindBys.clear();
        searchUsingFindBys.sendKeys("Test By FindBy");
        return PageFactory.initElements(driver, Google.class);

    }

    public Google sendTxtFindBy(WebDriver driver) {
        searchUsingFindBy.clear();
        searchUsingFindBy.sendKeys("Test By FindBys");
        return PageFactory.initElements(driver, Google.class);

    }

    public Google sendTxtFindAll(WebDriver driver) {
        for (WebElement search : searchUsingFindAll) {

        // searchUsingFindAll holds other WebElement also.This checking
        // is required to send text for search field only.

           

            if (!search.getAttribute("name").trim().isEmpty()) {
                search.clear();
                search.sendKeys("Test By FindAll");
            }
        }
        return PageFactory.initElements(driver, Google.class);

    }

}


//***************************************
// Test Class
//***************************************

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import testng.Google;

public class GooglePageTest {

    WebDriver driver = null;

    @BeforeTest
    public void testSetUp() {
        driver = new FirefoxDriver(new FirefoxBinary(new File("G:/Program Files/Mozilla Firefox/firefox.exe")), new FirefoxProfile());
        driver.get("http://www.google.com");
    }

    // Test SearchField for Google Page Using @FindBy
    @Test
    public void testFindBy() {

        Google google = PageFactory.initElements(driver, Google.class);
        google.sendTxtFindBy(driver);

    }

    // Test SearchField for Google Page Using @FindBys
    @Test
    public void testFindBys() {

        Google google = PageFactory.initElements(driver, Google.class);
        google.sendTxtFindBys(driver);

    }

    // Test SearchField for Google Page Using @FindAll
    @Test

    public void testFindAll() {

        Google google = PageFactory.initElements(driver, Google.class);
        google.sendTxtFindAll(driver);

    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

}