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


    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

    Popular Posts

    Top 10 Demo Websites to Practice Selenium Webdriver Online

    Future of Selenium Automation Testing as a Career Option

    How to learn Selenium Webdriver on your own online

    Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

    Selenium Webdriver - Overview & Features