Monday 23 January 2017

How to create Instance of Selenium Webdriver

  Selenium WebDriver is a tool to automate manual testcases. Selenium is open source tool for automating web applications. The latest version of selenium is Selenium3.0 . Selenium tests can be run against most modern browsers such as Firefox, Chrome and Internet Explorer. Selenium can be deployed on Windows, Linux and Mac.

Working with Browsers :

1. Open  Firefox Browser :
                                             Webdriver has  FirefoxDriver class to open a new session of firefox browser.
Syntax :
               // Need to import following packages

              import org.openqa.selenium.WebDriver;
              import org.openqa.selenium.firefox.FirefoxDriver;

              //Instance of FirefoxDriver              
               WebDriver ffDriver = new FirefoxDriver();
         
             //Open google.com site
               ffDriver.get("http://www.google.com") ;
  
2. Open Chrome Browser :
                                             Webdriver has ChromeDriver class to open a new session of chrome browser. To use chrome driver we need to download and provide path of ChromeDriver.exe. We can set the path using System.setProperty();
Syntax :
               // Need to import following packages
               import org.openqa.selenium.WebDriver;
               import org.openqa.selenium.chrome.ChromeDriver;

              
              //Instance of ChromeDriver
               System.setProperty("webdriver.chrome.driver", "Path  Of ChromeDriver.exe");
                WebDriver chDriver = new ChromeDriver();
         
             //Open google.com site
               chDriver.get("http://www.google.com") ;

3. Open InternetExplorer Browser :
                                             Webdriver has InternetExplorer class to open a new session of InternetExplorer browser. To use InternetExplorer driver we need to download and provide path of InternetExplorerDriver.exe. We can set the path using System.setProperty();
Syntax :
               // Need to import following packages
               import org.openqa.selenium.WebDriver;
               import org.openqa.selenium.internetexplorer.InternetExplorerDriver;

              
              //Instance of InternetExplorerDriver
               System.setProperty("webdriver.ie.driver", "Path  Of InternetExplorerDriver.exe");
                WebDriver ieDriver = new InternetExplorerDriver();
         
             //Open google.com site
               ieDriver.get("http://www.google.com") ;

Tuesday 17 January 2017

Webdriver Methods for Identifying elements

             A web application can contain many elements such as textbox, links, radio buttons, buttons, links, checkbox and drop down list. To locate element on web page webdriver provided findElement and findElements method.
             
             Page Elements : 
                                    1. Drop Down List.
                                    2. List Box.
                                    3. Web Table.
                                    4. Frame.
                                    5. Radio Button.
                                    6. Check Box.
                                    7. Image
                                    8. Link.
                                    9. Button.
                                   10. Text Box.
   
            Selenium Webdriver provides methods to uniquely identified an element in a HTML document during runtime.
            The various Webdriver methods which can be used to identify an element in GUI are as follows :

                 i. Id.
                ii. className.
               iii. tagName.
                iv. name.
                 v. linkText.
                vi. partialLinkText.
               vii. cssLocator.
              viii. Xpath.

1. Id :
         The id method uses the 'id' attribute to identify an HTML element.

        
   
         HTML Code of this element is :

         <input id="UserName" type="text" name="Uname" maxlength = "30"/>

         The Webdriver code to identify this element is :
     
          driver.findElement(By.id("UserName"));

2.  ClassName : 
          The className method uses the 'class' attribute to identify an HTML element.

        
   
         HTML Code of this element is :

         <input id="UserName" type="text" class="name" name="Uname" maxlength = "30"/>

         The Webdriver code to identify this element is :
     
          driver.findElement(By.className("name"));

3.  TagName : 
            The tagName method uses the 'tagName' attribute to identify an HTML element.
     
                                         


           User Name : <input id="UserName" type="text" name="Uname" maxlength = "30"/>
           <a href="www.google.com"> Know More </a>

          The Webdriver code to identify this element is :
     
          driver.findElement(By.tagName("a"));

       
4. Name :
           The Name method uses the 'name' attribute to identify an HTML element.

        
   
         HTML Code of this element is :

         <input id="UserName" type="text" class="name" name="Uname" maxlength = "30"/>

         The Webdriver code to identify this element is :
     
          driver.findElement(By.name("Uname"));
          

Saturday 14 January 2017

Selenium Webdriver Methods

                        Selenium provides a variety of methods to perform actions on a browser. Some of those are described below:

1. get() :
           It is used to load a new page in the current browser. This method blocks the WebDriver test execution until the page load is complete.

              // Need to import following packages
              import org.openqa.selenium.WebDriver;
              import org.openqa.selenium.firefox.FirefoxDriver;

              //Instance of FirefoxDriver              
               WebDriver ffDriver = new FirefoxDriver();
         
             //Open google.com site
               ffDriver.get("http://www.google.com") ;

2. getCurrentUrl() : 
                              It is used to get current url. Return type of this method is string.
          
               //Instance of FirefoxDriver              
               WebDriver ffDriver = new FirefoxDriver();
         
               //To get Current Url
               String strCurrentUrl = ffDriver.getCurrentUrl() ;

3. getTitle() :
                     It returns the title of current web page. Return type of this method is string.
               //Instance of FirefoxDriver              
               WebDriver ffDriver = new FirefoxDriver();
         
               //To get Current Url
               String strGetTitle = ffDriver.getTitle() ;

4. findElement() :
                         Finds matched element in the current page. If no matched element is found till     
               timeout time. then it returns an exception.
   
                       WebElement btnElement = driver.findElement(By.id("ButtonId"));

5. findElements():
                         Finds all the matched elements in the current page. If element does not found then it will return empty list.

                       List<WebElement>  lstElement = driver.findElement(By.tagName("a"));

6. navigate() : 
                       This method is used to back, forward, refresh and navigate.
                       i. driver.navigate().back()
                      ii. driver.navigate().forward()
                     iii. driver.navigate().refresh()
                      iv. driver.navigate().to("URL")