Saturday 3 June 2017

Working with Frames

  
                                                     Working with Frames


                        A frame is part of the  web page or browser window which leads ans display content independent of its container. Frames allow developers to present HTML pages in multiple views.
                     
Frame example :
                        
                       


HTML Syntax :

<html>
      <frameset cols= "33%,33%,33%">
               <frame name=frame1 id=frame1 src="http://www.flipkart.com"> Frame A
               <frame name=frame2 src="http://www.amazon.com"> Frame B
               <frame name=frame3 src="http://www.jabong.com"> Frame C
      </frameset>
</html>

Locating Frame :
Selenium Webdriver provides three ways of locating a frame element
      1. Index.
      2. Name.
      3. Id.
 
1. Locating Frame using Name attribute :
    Attribute name of the frame element can be used to locate the frame element.   

HTML Sysntax:    
<html>
      <frameset cols= "33%,33%,33%">
               <frame name=frame1 id=frame1 src="http://www.flipkart.com"> Frame A
      </frameset>
</html>

Selenium Code:

WebDriver driver = new FirefoxDriver();
driver.get("file://C:/User/frameset.html");
WebElement frame1= driver.findElement(By.name("frame1"));
driver.switchTo().frame(frame1); // To move on frame
driver.switchTo().defaulltContent(); // To move on main window


    
2. Locating Frame using Id attribute :
    Attribute Id of the frame element can be used to locate the frame element.  

 HTML Sysntax:   
<html>
      <frameset cols= "33%,33%,33%">
               <frame name=frame1 id=frame1 src="http://www.flipkart.com"> Frame A
      </frameset>
</html>

Selenium Code:

WebDriver driver = new FirefoxDriver();
driver.get("file://C:/User/frameset.html");
WebElement frame1= driver.findElement(By.Id("frame1"));
driver.switchTo().frame(frame1); // To move on frame
driver.switchTo().defaulltContent(); // To move on main window

3.  Locating Frame using Index attribute :
     Index value of the frame can be used to locate the frame element. The index  value starts with 0.
     
Selenium Code:

WebDriver driver = new FirefoxDriver();
driver.get("file://C:/User/frameset.html");
driver.switchTo().frame(0); // To move on frame using index
driver.switchTo().defaulltContent(); // To move on main window





Sunday 23 April 2017

How to priorities Testcases in Selenium Webdriver using TestNG

How to priorities Testcases in Selenium Webdriver using TestNG
        To priorities testcase we need to set priority to every testcase. In below example we have prioritized Login Testcase first and Location Testcase second. We have used "priority" keyword in Test annotation. Test execution starts with 0 priority.

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LoginLinked {
   
    FirefoxDriver driver = new FirefoxDriver();
   
    @BeforeClass
    public void BrowserIntiate()
   {       
          driver.get("https://www.linkedin.com");
          driver.manage().window().maximize();
    }
   
    @Test(priority=0) // It will execute first as its priority is zero
    public void Login()
    {
         WebElement txtUserName = driver.findElementById("login-email");
          txtUserName.sendKeys("Username");
         
          WebElement txtPassword = driver.findElementById("login-password");
          txtPassword.sendKeys("Password");
         
          WebElement btnLogIn = driver.findElementByName("submit");
          btnLogIn.click();      
         
    }
   
    @Test(priority=1)
    public void LocationTest()
   {
          WebElement lnkAdvance = driver.findElementByLinkText("Advanced");
          lnkAdvance.click();
         
          WebElement lnkClose = driver.findElementByXPath("//button[@class='text-button close-advs']");
          lnkClose.click();
         
          WebElement chkCountryIN = driver.findElementById("in:0-G-ffs");
          chkCountryIN.click();         
         
         Assert.assertEquals("INDI", chkCountryIN.getText());
    }
   
@AfterClass
    public void BrowserQuit()
   {
        driver.quit();
    }
   
}

Saturday 22 April 2017

How to Maximize window using selenium Web Driver

How to Maximize browser window using selenium Web Driver


import org.openqa.selenium.firefox.FirefoxDriver; // If you are using Firefox Browser
import org.openqa.selenium.chrome.ChromeDriver;// If you are using Chrome Browser
import org.openqa.selenium.ie.InternetExplorerDriver; // If you are using IE
import java.util.concurrent.TimeUnit;


public class MaximizeWindow {

    public static void main(String[] args)
   {
         FirefoxDriver driver = new FirefoxDriver();
         driver.get("https://www.linkedin.com");
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.manage().window().maximize();        // for maximize window           
    }

}

Wednesday 12 April 2017

Working With Popup Windows

                                                
                                             Working With Popup Windows

                                There are different type of windows in selenium
                                        
                                     1. Javascript Alert popups.
                                     2. Windows Alert popups.


 1. Javascript Alert Popup :
                                            Selenium provides an Alert class for working with javascript alerts. Method switchTo() to be used to switch driver context to the alert box before any user action can be taken it.

                   Alert alert = driver.switchTo().alert()

    Methods of Alert class :
                 
                   a. accept() :  This method clicks on the 'OK' button of the alert box.
                                       
                                        Syntax:                                                   
                                        driver.switchTo().alert().accept();

                     
                                       


                   b. dismiss() : This method clickes on the 'Cancel' button of the alert box.

                                         Syntax :
                                         driver.switchTo().alert().dismiss();
                                        
                                       
                     c. getText() : This method retrives the text displayed on the alertbox.             
                                         
                                           Syntax :
                                           driver.switchTo().alert().getText();



                    d. sendKeys() : This method will write the specified text string on the alert
                                               prompt box.
                                      
                                             Syntax :
                                             driver.switchTo().alert().sendKeys("Your Text Here");
                                           


2. Windows Alert Popups :
                                            We can handle these popus using two ways.
                                             1. Using third party Auto IT tool.
                                             2. Using Robot Class.


 

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