Your ultimate guide

Automation_Browser Commands 1


What is WebDriver?
  • WebDriver is an interface provided by Selenium WebDriver.
  • Interface is a collection of abstract methods(Methods without implementation) (Like close(), findElement(), get(), getTitle(), quit(), switchTo(), …….. )
  • WebDriver interface acts as a contract that each browser-specific driver implements.
    Note: By using Selenium we can only test the Web Based Applications.


How to Launch Chrome browser?
    To Launch the web browser we need to create an object for the ChromeDriver Contactor, Using the WebDriver interface we can launch the web browser.

Example: WebDriver driver = new ChromeDriver();
    Here we have created a ChromeDriver instance and stored it in a variable called driver, which is of type ‘WebDriver’ interface.

Note: 
  • For the Firefox browser "WebDriver driver = new FirefoxDriver();"
  • For the Edge browser "WebDriver driver = new EdgeDriver();"
  • For the Opera browser "WebDriver driver = new OperaDriver();"
  • For the Safari browser "WebDriver driver = new SafariDriver();"


Interview Question:

Explain this? 

WebDriver driver = new ChromeDriver();

We can create an Object of a class ChromeDriver by taking reference of an interface (WebDriver). In this case, we can call the implemented method of the webDriver interface.

******************************




Browser Commands:
    get(): get method is used to launch the website.
        Syntax: driver.get(“url”);

  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;

  3. public class GetMtd {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.    }
  8. }




    getTitle(): getTitle method is used to fetch the title of the current webpage.

        Syntax: driver.getTitle();

  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class GetTitle {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       String title = driver.getTitle();
  8.       System.out.println(title);
  9.    }
  10. }




    getCurrentUrl(): get Current Url method is used to get the URL of the active browser window.
        Syntax: driver.getCurrentUrl();
  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class GetUrl {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       String curUrl = driver.getCurrentUrl();
  8.       System.out.println(curUrl);
  9.    }
  10. }




    getPageSource(): The get Page Source method is used to get the current web page source code.
        Syntax: driver.getPageSource();
  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class GetPageSrc {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       String getSrc = driver.getPageSource();
  8.       System.out.println(getSrc);
  9.    }
  10. }




    close(): The close method is used to close the active window in the browser.
        Syntax: driver.close();
  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class closeMtd {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       driver.close();
  8.    }
  9. }




    quit(): The quit method is used to close all windows in the browser.
        Syntax: driver.quite();
  • Example Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class quitMtd {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       driver.quit();
  8.    }
  9. }

What is the difference between the close() and quit() methods in selenium?
    quit() method turning off the entire browser and all its tabs. It's like closing Chrome entirely.
    close() method closing just one tab in the browser. If you have multiple tabs open, it will close the one you're looking at, but the other tabs stay open.
    So, quit() ends everything, and close() only closes what you're looking at,





Example Program 1:

User Stories:

1.    Open Chrome Browser

2.    Navigate to the Testingcolleges blogger Homepage.

3.    Get the page Title, page URL and source code of the page

4.    Print the Title, URL and Source code of the webpage in the Console

5.    Close the browser window using quit()


  • Program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class Example1 {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();

  6.       driver.get("https://testingcolleges.blogspot.com/");
  7.       String pgTitle = driver.getTitle();
  8.       String pgUrl = driver.getCurrentUrl();
  9.       String pgSrc = driver.getPageSource();

  10.       System.out.println(pgTitle+"\n"+pgUrl);
  11.       System.out.println(pgSrc);
  12.       driver.quit();
  13.    }
  14. }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Example Program 2:
Steps Step Actions Expected Results
1 Open Chrome Browser Chrome browser should be Launch
2 Launch the URL, https://testingcolleges.blogspot.com/ Web page should be displayed
3 Verify the Current URL https://testingcolleges.blogspot.com/ should be displayed
4 Verify the Title of the page “Testing Colleges” title should be displayed
5 Close the browser window Browser should be Closed


  • Example 2 program:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class Example2Detail {
  4.    public static void main(String[] args) {
  5.       WebDriver driver = new ChromeDriver();
  6.       driver.manage().window().maximize();

  7.       //Launch the URL
  8.       String url = "https://testingcolleges.blogspot.com/";
  9.       driver.get(url);

  10.       //verify the URL of the current Web page
  11.       String actualURL = driver.getCurrentUrl();
  12.       String expectedURL = "https://testingcolleges.blogspot.com/";
  13.       if(actualURL.equals(expectedURL)) {
  14.          System.out.println("URL of the page :"+ expectedURL +" :Test is Passed");
  15.       }
  16.       else {
  17.          System.out.println("The actual URL of the page is :"+ actualURL + " :Test is Failed");
  18.       }

  19.       //Verify the Title of the current Web page
  20.       String actualTitle = driver.getTitle();
  21.       String expectedTitle = "Testing Colleges";
  22.       if(actualTitle.equals(expectedTitle)) {
  23.          System.out.println("Title of the page :"+ expectedTitle +" :Test is Passed");
  24.       }
  25.       else {
  26.          System.out.println("The actual title of the page is :"+ actualTitle +" :Test is Failed");
  27.       }

  28.       //Close the Browser
  29.       driver.close();
  30.    }
  31. }

- Next Page: Browser Commands 2

No comments:

Post a Comment