Selenium WebDriver – Browser Commands

Last updated on Dec 02 2021
Manikaran Reddy

Table of Contents

Selenium WebDriver – Browser Commands

The very basic browser operations of WebDriver include opening a browser; perform few tasks then closing the browser.
Given are a number of the foremost commonly used Browser commands for Selenium WebDriver.
1. Get Command
Method:

1. get(String arg0) : void

In WebDriver, this method loads a replacement website within the existing browser window. It accepts String as parameter and returns void.
The respective command to load a replacement website are often written as:

1. driver.get(URL);
2.
3. // Or are often written as
4.
5. String URL = "URL";
6. driver.get(URL);

Example: as an example , the command to load the official website of tecklearn are often written as:

1. driver.get("www.tecklearn.com")
2. Get Title Command

Method:

1. getTitle(): String

In WebDriver, this method fetches the title of the present website . It accepts no parameter and returns a String.
The respective command to fetch the title of the present page are often written as:

1. driver.getTitle();
2.
3. // Or are often written as
4.
5. String Title = driver.getTitle();
3. Get Current URL Command

Method:

1. getCurrentUrl(): String

In WebDriver, this method fetches the string representing the present URL of the present website . It accepts nothing as parameter and returns a String value.
The respective command to fetch the string representing the present URL are often written as:

1. driver.getCurrentUrl();
2.
3. //Or are often written as
4.
5. String CurrentUrl = driver.getCurrentUrl();
4. Get Page Source Command

Method:

1. getPageSource(): String

In WebDriver, this method returns the ASCII text file of the present website loaded on the present browser. It accepts nothing as parameter and returns a String value.
The respective command to urge the ASCII text file of the present website are often written as:

1. driver.getPageSource();
2.
3. //Or are often written as
4. String PageSource = driver.getPageSource();
5. Close Command

Method:

1. close(): void

This method terminates the present browser window operating by WebDriver at the present time. If the present window is that the only window operating by WebDriver, it terminates the browser also . This method accepts nothing as parameter and returns void.
The respective command to terminate the browser window are often written as:

1. driver.close();
6. Quit Command

Method:

1. quit(): void

This method terminates all windows operating by WebDriver. It terminates all tabs also because the browser itself. It accepts nothing as parameter and returns void.
The respective command to terminate all windows are often written as:

1. driver.quit();

Let us consider a sample test script during which will cover most of the Browser Commands provided by WebDriver.
In this sample test, we’ll automate the subsequent test scenarios:
• Invoke Chrome Browser
• Open URL: https://www.google.co.in/
• Get Page Title name and Title length
• Print Page Title and Title length on the Eclipse Console
• Get page URL and verify whether it’s the specified page or not
• Get page Source and Page Source length
• Print page Length on Eclipse Console.
• Close the Browser
For our test purpose, we are using the house page of “Google” program .
We will create our test suit step by step to offer you an entire understanding on the way to use Browser Commands in WebDriver.
• Step1. Launch Eclipse IDE and open the prevailing test suite “Demo_Test” which we’ve created in WebDriver Installation section of WebDriver tutorial.
• Step2. Right click on the “src” folder and make a replacement Class File from New > Class.

Selenium Web Driver
Selenium Web Driver

Give your Class name as “Navigation_command” and click on on “Finish” button.

Selenium Web Driver
Selenium Web Driver

Step3. Let’s get to the coding ground.
To automate our test scenarios, first you would like to understand “How to invoke/launch web browsers in WebDriver?”
Note: To invoke a browser in Selenium, we’ve to download an executable file specific thereto browser. for instance , Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. These executable files start a server on your system which successively is liable for running your test scripts in Selenium.
We have stated the procedures and methods to run our tests on different browser in later sections of this tutorial. For reference you’ll undergo all of them before proceeding with the particular coding.
1. Running test on Firefox
2. Running test on Chrome
3. Running test on Internet Explorer
4. Running test on Safari
• To invoke Google Chrome browser, we’d like to download the ChromeDriver.exe file and set the system property to the trail of your ChromeDriver.exe file. we’ve already discussed this in earlier sessions of this tutorial. you’ll also ask “Running test on Chrome Browser” to find out the way to download and set System property for Chrome driver.
Here is that the sample code to line system property for Chrome driver:

1. // System Property for Chrome Driver
2. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");

After that we’ve to initialize Chrome driver using ChromeDriver class.
Here is that the sample code to initialize Chrome driver using ChromeDriver class:

1. // Instantiate a ChromeDriver class.
2. WebDriver driver=new ChromeDriver();

Combining both of the above code blocks, we’ll get the code snippet to launch Google Chrome browser.

1. // System Property for Chrome Driver
2. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
3.
4. // Instantiate a ChromeDriver class.
5. WebDriver driver=new ChromeDriver();

• To automate our second test scenario i.e. “Get Page Title name and Title length”, we’ve to store the title name and length in string and int variable respectively.
Here is that the sample code to try to to that:

1. // Storing Title name within the String variable
2. String title = driver.getTitle();
3. // Storing Title length within the Int variable
4. int titleLength = driver.getTitle().length();

To print page Title name and Title length within the Console window, follow the given code snippet:

1. // Printing Title & Title length within the Console window
2. System.out.println("Title of the page is : " + title);
3. System.out.println("Length of the title is : "+ titleLength);

• The next test scenario requires fetching the URL and verifying it against the particular URL.
First, we’ll store the present URL during a String variable:

1. // Storing URL in String variable
2. String actualUrl = driver.getCurrentUrl();

Verifying the present URL because the actual URL:

1. if (actualUrl.equals("https://www.google.co.in"))
2. {
3. System.out.println("Verification Successful - the right Url is opened.");
4. }
5. Else
6. {
7. System.out.println("Verification Failed - An incorrect Url is opened.");
8. }

• To automate our 6th test scenario (Get page Source and Page Source length), we’ll store the page source and page source length within the string and int variable respectively.

1. // Storing Page Source in String variable
2. String pageSource = driver.getPageSource();
3.
4. // Storing Page Source length in Int variable
5. int pageSourceLength = pageSource.length();

To print the length of the Page source on console window, follow the given code snippet:

1. // Printing length of the Page Source on console
2. System.out.println("Total length of the Page Source is : " + pageSourceLength);

• Finally, the given code snippet will terminate the method and shut the browser.

1. driver.close();

Combining all of the above code blocks together, we’ll get the specified ASCII text file to execute our test script “Web_command”.
The final test script will appear something like this:
(We have embedded comment in each section to elucidate the steps clearly)

1. import org.openqa.selenium.WebDriver;
2. import org.openqa.selenium.chrome.ChromeDriver;
3.
4. public class Web_command {
5.
6. public static void main(String[] args) {
7.
8. // System Property for Chrome Driver
9. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
10.
11. // Instantiate a ChromeDriver class.
12. WebDriver driver=new ChromeDriver();
13.
14. // Storing the appliance Url within the String variable
15. String url = ("https://www.google.co.in/");
16.
17. //Launch the ToolsQA WebSite
18. driver.get(url);
19.
20. // Storing Title name within the String variable
21. String title = driver.getTitle();
22.
23. // Storing Title length within the Int variable
24. int titleLength = driver.getTitle().length();
25.
26. // Printing Title & Title length within the Console window
27. System.out.println("Title of the page is : " + title);
28. System.out.println("Length of the title is : "+ titleLength);
29.
30. // Storing URL in String variable
31. String actualUrl = driver.getCurrentUrl();
32.
33. if (actualUrl.equals("https://www.google.co.in/")){
34. System.out.println("Verification Successful - the right Url is opened.");
35. }
36. else{
37.
38. System.out.println("Verification Failed - An incorrect Url is opened.");
39. }
40.
41. // Storing Page Source in String variable
42. String pageSource = driver.getPageSource();
43.
44. // Storing Page Source length in Int variable
45. int pageSourceLength = pageSource.length();
46.
47. // Printing length of the Page Source on console
48. System.out.println("Total length of the Pgae Source is : " + pageSourceLength);
49.
50.
51. //Closing browser
52. driver.close();
53. }
54. }

To run the test script on Eclipse window, right click on the screen and click on
Run as → Java application

Selenium Web Driver
Selenium Web Driver

After execution, the test script will launch the chrome browser and automate all the test scenarios. The console window will show the results for print commands.

Selenium Web Driver
Selenium Web Driver

So, this brings us to the end of blog. This Tecklearn ‘Selenium WebDriver – Browser Commands’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/selenium-training-certification/

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium
• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse
Setting the environment in Eclipse for Selenium
• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On
Advantages of Selenium automation testing
• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On
Selenium Web driver Automation
• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On
Deploying Web Drivers for scripting
• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On
Deep dive into Selenium Web Driver
• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On
Switching Operations in WebDriver using Window
• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On
Introduction to TestNG Framework
• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report
JUnit Operations and Test Framework
• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project
Object Repository
• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory
JavaScript Functions
• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Selenium WebDriver - Browser Commands"

Leave a Message

Your email address will not be published. Required fields are marked *