Selenium WebDriver - Browser and Navigation Commands

Browser commands are the starting point of your Selenium Webdriver script. These commands used to launch browser, maximize it, open URL to be tested and other navigation commands.

1. Browser Commands

i. Set Path of browser/driver executable:
  • This would be the first line of your webdriver script. You have to download the browser executable file for the browser you are using and set path of the driver executbale to system property.
  • Like for firefox you have to download geckodriver.exe and place in your project. Similarly for other browsers you have to download their browser/driver executables.
  • Download Driver Executables link - https://www.seleniumhq.org/download/
  • Below is an example for Firefox.
// Set Path of driver executable
String driver_executable_path = "./src/com/techlistic/utils/geckodriver.exe";

System.setProperty("webdriver.gecko.driver", driver_executable_path);


ii. Launch Broswer:
// Launch Browser - Creating a Firefox instance
WebDriver driver = new FirefoxDriver();

Code for other browsers :
// For Chrome
WebDriver driver = new ChromeDriver();

// For Safari
WebDriver driver = new SafariDriver();

// For Opera
WebDriver driver = new OperaDriver();

// For IE
WebDriver driver = new InternetExplorerDriver();


iii. Open URL
  • This command is used to open URL. You can provide a string which contains a url.
// Open URL 
driver.get("https://www.techlistic.com/");

// Or 
String url = "https://www.techlistic.com/";

driver.get(url);


iv. Get Title
  • This command is used to obtain title of the page. It returns a string.
// Get Title
driver.getTitle();

// Code implementation example
String pageTitle = driver.getTitle();
// Validate Page Title
if(pageTitle == "Techlistic - Home") {
 System.out.println("Test Passed.");
}
else {
 System.out.println("Test Failed.");
}


v. Get Current URL
  • This command is used to get url of current page. It returns a string.
// Get Current URL
driver.getCurrentUrl();

//Code implementation example
String pageURL = driver.getCurrentUrl();

// Validate Current Page URL
if(pageURL == "https://www.techlistic.com/p/selenium-tutorials.html") 
{
 System.out.println("Test Passed.");
}
else 
{
 System.out.println("Test Failed.");
}


vi. Get Page Source
  • This command is used to get the source code of the web page. It returns a string.
// Get Page Source
driver.getPageSource();

// Or
String sourceCode = driver.getPageSource();


vii. Close Browser
  • This command is used to close the current browser window. It is generally used at the end of script.
viii. Quit Browser
  • This command is used to close all the browser windows opened by Webdriver. It is used at end of the script
// Close Current Browser Window
driver.close();

// Close All Browser Windows
driver.quit();

    ix. Maximize Browser

    x. Make Browser Full Screen

    xi. Minimize Browser

    xii. Set Size of Browser

    // Maximize Browser
    driver.manage().window().maximize();
    
    // Make Browser Window Full Screen
    driver.manage().window().fullscreen();
    
    // Minimize the browser
    driver.manage().window().setPosition(new Point(0, -1000));
    
    // Set Size of browser
    driver.manage().window().setSize(new Dimension(1920, 1080));

    Program - Browser Commands
    You can refer the following example program for browser commands.




    package com.techlistic.selenium;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class A_01_BrowserCommands_Test {
    
     public static void main(String[] args) {
      
      // Expected Title
      String expectedTitle = "Techlistic";
    
      // Set Path of the Chrome driver
      System.setProperty("webdriver.chrome.driver", "./src/com/techlistic/utils/chromedriver.exe");
    
      // Launch Firefox browser
      WebDriver driver = new ChromeDriver();
    
      // Open URL of Website
      driver.get("https://www.techlistic.com");
    
      // Maximize Window
      driver.manage().window().maximize();
      
      // Get Title of current Page
      String actualTitle = driver.getTitle();
      System.out.println("Title fetched: "+ actualTitle);
      
      // Validate/Compare Page Title
      if(expectedTitle.equals(actualTitle)) {
       System.out.println("Test Case Passed.");
      }
      else {
       System.out.println("Test Case Failed.");
      }
      
      // Close Browser
      driver.close();
      
     }
     
    
    }


    2. Browser Navigation Commands

    These commands are used to move forward and back using browser history.

    /***********************************
     * Browser Navigation Commands     *
     ***********************************
     */
    
    // Navigate Forward
    driver.navigate().forward();
    
    // Navigate Back
    driver.navigate().back();
    
    // Refresh Page
    driver.navigate().refresh();
    
    // Naviagte directly to some URL (which might be external or internal in the website)
    driver.navigate().to("https://www.techlistic.com/p/java.html");




    Selenium Demo Websites  << Previous      ||      Next >>  Find Element & Locators


    Refer Selenium Webdriver Tutorials Series
    Join Telegram channel at https://t.me/techlistic
    Feel free to ask queries or share your thoughts in comments or email me.

    Comments

    1. Excellent. Content Simplified in its best way. Please post on pending areas in the index.

      ReplyDelete
    2. This is one awesome blog article. Much thanks again selenium online training

      ReplyDelete

    3. تنظيف منازل بالدمام تنظيف منازل بالدمام
      تنظيف منازل بالاحساء تنظيف منازل بالاحساء
      تنظيف منازل بمكة تنظيف منازل بمكة
      تنظيف منازل بجدة تنظيف منازل بجدة
      تنظيف منازل بالمدينة المنورة تنظيف منازل بالمدينة المنورة

      ReplyDelete

    Post a Comment

    Popular Posts

    Top 10 Demo Websites to Practice Selenium Webdriver Online

    Top 25 Must to know Selenium Webdriver Commands List

    Assignment 5: Automate "Buy Product" Functionality for E-commerce Website using Selenium

    Selenium IDE - Sample Script With Most Commonly Used Commands

    Assignment 6: Automate "User Registration" Process for E-commerce Website using Selenium