0% found this document useful (0 votes)
53 views13 pages

Techlistic: Selenium Webdriver - Browser and Navigation Commands

This document provides an overview of browser commands and navigation commands in Selenium WebDriver for automation testing. It discusses launching the browser, opening URLs, getting page titles and URLs, maximizing/minimizing the browser window, navigating forward and backward, and refreshing pages. Code examples are given for each command along with a sample program to demonstrate various browser commands. Popular demo websites are also listed for practicing Selenium automation.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views13 pages

Techlistic: Selenium Webdriver - Browser and Navigation Commands

This document provides an overview of browser commands and navigation commands in Selenium WebDriver for automation testing. It discusses launching the browser, opening URLs, getting page titles and URLs, maximizing/minimizing the browser window, navigating forward and backward, and refreshing pages. Code examples are given for each command along with a sample program to demonstrate various browser commands. Popular demo websites are also listed for practicing Selenium automation.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Techlistic

Selenium | Java | Rest API | Postman | JMeter | Automation Tutorials & Tools | Top
Selenium Blogs | Software Testing | Python | Hacking | Guest Blogs | Webinars | Coding
Interview Q&A
 HOME
 
 SELENIUM
 
 JAVA
 
 DEMO SITES
 
 ASSIGNMENTS
 
 INTERVIEWS
 
 REST API
 
 TESTNG
 
 CONTACT
 
 JMETER
 












Selenium WebDriver - Browser and Navigation


Commands
- January 20, 2020
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

Author: Vaneesh Behl


Subscribe FB Group Techlistic.com
Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Selenium Webdriver

Comments

1.

AnonymousOctober 11, 2019 at 5:04 PM

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

REPLY

2.

S VEERESH KUMARFebruary 9, 2020 at 3:56 PM

very helpful to practise these Assignments

REPLY

Post a Comment

Popular Posts
Top 10 Demo Websites to Practice Selenium Webdriver Online
- February 27, 2020
In this post you will find links of top demo websites/pages which Automation
Professionals can use for practice purpose. Here is the list of demo websites:
1. AUTOMATION PRACTICE FORM Level - BeginnerAbout - This form contains all the
important form elements which we come across daily like text box, radio button, check
box, select drop downs, multi-select box, button, links, File Upload, Download link.

2. AUTOMATION PRACTICE TABLE Level - IntermediateAbout - This web page


contains table data. You can test your Selenium and Java skills. It would require
Selenium commands and java loops, conditions to read table data. 

3. PRACTICE WINDOWS & ALERTS Level - IntermediateAbout - This web page


contains buttons/ links which produces new web windows, javascript alerts etc. You can
practice selenium commands to handle windows/alerts on this page.

4. E-Commerce Practice Website Level - Intermediate/ProAbout - It’s a full featured e-


commerce website simulator and my favorite. Here you can test anythi…
READ MORE
How to Take Screenshot and Partial Screenshot in Selenium
WebDriver
- July 30, 2019
In this tutorial we will learn to take full screenshot and partial screenshot. In automation
we capture the screenshots for reference. To capture screenshot in Selenium
'TakeScreenshot' interface is used.

Take Screenshot Command:


/************************************ * Capture Screenshot *
************************************ *//* Create object of File class of Java * And capture
screenshot using getScreenshotAs() method of Webdriver * Set Output type of
screenshot taken as 'File'*/File screenshot =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); /* Use FileUtils class
of Java * Call it's copyFile method * Copy screenshot file to a location with some
name and extension you want*/FileUtils.copyFile(screenshot,new
File("D:\\screenshot.jpg"));

How to Take Partial Screenshot:

Sometimes we don't want to take screenshot of the full screen. Reasons might be, full
size images would last in huge memory storage for image directory or…
READ MORE
Automation Framework Building 1st step - Implementing Code Re-
usablility
- July 23, 2019
In this post, you will learn kind of coding pattern which is very helpful in maintaining our
automation code. This post is written insight to help beginners. We'll learn that instead of
writing a linear script, we should create page (action) methods which in general contain
actions which we are going to perform on our web software. 

Let's say, we have a login functionality in our software and we have to automate it. In
that case we'll create a method named login and write the commands like, entering
username, password and click login in that method. Now we can use that method
wherever we need it in other test cases as well. Benefits of using action methods are:

Code Re-usabilityBetter maintainability of codeSharing below a sample script, in which


I'm entering data in a web form using action methods:

You can also place these action methods in some other class like Common.java. And if
you want to re-use any of the methods in your Selenium script, just create the object
Comm…
READ MORE
Top 25 Must to know Selenium Webdriver Commands List
- August 20, 2019
It covers almost all selenium webdriver commands with syntax. This tutorial acts as your
guide to all important Selenium Webdriver commands.

1. Browser CommandsBrowser commands are the basic commands in Selenium. These


commands are the starting point of your selenium script. Browser commands are used to
launch browser, maximize it, open url and close browser etc. 

Syntax of all browser commands:


/************************** * Browser Commands * ************************** */// Set Path of
driver executable String driver_executable_path =
"./src/com/techlistic/utils/geckodriver.exe"; System.setProperty("webdriver.gecko.driver",
driver_executable_path); // Launch Browser - Creating a Firefox instance WebDriver
driver = new FirefoxDriver(); // For Chrome WebDriver driver = new ChromeDriver(); //
For Safari WebDriver driver = new SafariDriver(); // For Opera WebDriver driver = new
OperaDriver(); // For IE WebDriver driver = new InternetExplorerDriver(); // Op…
READ MORE
 Powered by Blogger
Theme images by mammuth

Followers
Selenium Links

 Selenium Tutorials
 TestNG Integration
 Blogs/Articles
 Practice Assignments
 Demo Websites
 Interview Q/A
 Online Training

Website Links

 Tech Trends
 Java Blogs
 Python
 Hacking
 Selenium IDE
 Guest Posts

Tutorials List

 Software Testing
 Java Tutorial
 Selenium Tutorial
 TestNG Tutorial

Labels

You might also like