Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / automation

Selenium WebDriver Browser Commands

4.96/5 (13 votes)
20 Oct 2014CPOL3 min read 57.8K  
Here I describe all the commands that are required for the Selenium to perform specific tasks.

Introduction

In this part of my tip, we are going to discuss about various browser commands that we would be using in our day to day life in testing. Opening and closing a browser is the very first thing in Selenium, a tester would like to do. The commands refer to what Selenium has to do. Just like a master commands a slave in early days!! Sorry even today. OK!! Let's not get social now.
So here, we discuss regarding the commands we give to the Selenium in order to perform some specific task.

Below are different basic commands, which we will encounter while writing the scripts.

Get Commands

Get commands are used to collect various information about the page that we deal with. Here are some important "get" commands you must be familiar with:

  1. Get Command - " get() ": get() command is used to open a new browser window and it will find/fetch the page that you have given.
  2. Get Title Command - " getTitle() " : This command is used to get the title of the current page.
  3. Get Current URL Command - " getCurrentUrl() " : This command is used to get the current URL of the browser.
  4. Get Page Source Command - " getPageSource()" : This command is used to get the source code of the page.
  5. Get Text - " getText() ": This command is used to fetch the text of the element.

Navigate Commands

  1. Navigate To Command - " navigate().to() " : This command is like Get() command. It opens a new browser window and it will find/fetch the page that you have given.
  2. Navigate Refresh Command - " navigate().refresh() " : This command is used to refresh the current page.
  3. Navigate to back page - " navigate().back() " : This command is used to go back by one page on the browser's history.
  4. Navigate to forward page - " navigate().forward() " : Takes you forward by one page on the browser's history.
  5. Refresh page - " navigate().refresh() " : It refreshes the current page.

Other Useful Commands

  1. SwitchTo command - " driver.switchTo().window("windowName") " : This command is used to switch from one window to another.
  2. Switching from one iframe to another - " driver.switchTo().frame("frameName") " : This command is used to switch from a one iframe to another iframe.
  3. Handling Alerts - " driver.switchTo().alert() " : This command is used to handle alerts.
  4. Close Command - " close() ": This command closes the current window of the browser.
  5. Quit Command - " quit() " : This command is used to quit the browser and all the opened windows in the browser. These are the basic commands that are needed for writing simple script.

Now by using these commands, I will automate a browser by writing scripts so that we can have a clear idea of how to use these commands.

Test Scenario

  • Open a Firefox Browser.
  • Put an Implicit wait.
  • Navigate to the URL.
  • Maximize the window.
  • Store the Title Name.
  • Print the Title Name.
  • Store Title Length.
  • Print Title Length.
  • Store the URL Name.
  • Print the URL Name.
  • Store URL Length.
  • Print URL Length.
  • Print the name of Tab which is going to be clicked suppose [NEWS TAB].
  • Click on NEWS Tab.
  • Store the Text of an element of the new opened tab.
  • Print the Text Name of an element of the new opened tab.
  • Now Navigate to the previous page.
  • Print the Title of the previous page.
  • Now Navigate to the next page.
  • Print the Text of the next page.
  • Refresh the current page.
  • Store the Page Source in String variable.
  • Print the Page Source Name.
  • Store Page Source Length in Integer Variable.
  • Print Page Source Length.
  • Close the browser or page currently which is opened.

The code/snippet goes as below:

C#
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Commands {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();

// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

// Go to URL
driver.get("http://www.bbc.com/");

// Storing Title name in String variable so that we can get the title name
String btitle = driver.getTitle();

// Printing the Title name in 2 ways
// 1st way
System.out.println(btitle);
// 2nd way
System.out.println("Title is : " + btitle);

// Storing Title length in Int variable because we are dealing with Integers that's why int is used.
int bTitleLength = driver.getTitle().length();

// Printing Title l0ength
System.out.println("Length of the Title is : " + bTitleLength);
// Storing URL in String variable
String b1 = driver.getCurrentUrl();

// Printing URL on Console Window

System.out.println("URL is : " + b1);

// Storing URL length
int b2 = driver.getCurrentUrl().length();

// Printing URL length on console Window

System.out.println("URL length is : " + b2);

// COMMAND FOR NAVIGATION THROUGH PAGES.

//Open the NEWS Page.
String b3 = driver.findElement(By.xpath("//*[@id='blq-nav-news']/a")).getText();

// Print the Tab name which is being clicked.
System.out.println("Name of the Tab which is being clicked is : " + b3);

// Click on the Tab
driver.findElement(By.xpath("//*[@id='blq-nav-news']/a")).click();

//Get the text where Xpath is pointing.
String b4 = driver.findElement(By.xpath("//*[@id='header']")).getText();

// Print the text of the current page
System.out.println("Text of current page is : " + b4);

//Navigate to previous Page.

driver.navigate().back();
Thread.sleep(1000);
String b5 = driver.getTitle();

// Print the text of the previous page
System.out.println("Text of the previous page is : " + b5);

//Navigate to Next page.
driver.navigate().forward();
Thread.sleep(1000);
// Print the text of the NEXT page
String b6 = driver.getTitle();
System.out.println("Text of the NEXT page is : " + b6);
// COMMAND FOR REFRESHING THE CURRENT PAGE

// driver.get(driver.getCurrentUrl());
driver.navigate().refresh();

// PAGE SOURCE COMMAND
// Storing Page Source in String variable.
String b7 = driver.getPageSource();
Thread.sleep(1000);

// Printing the Page Source
System.out.println("Page source : " + b7);

// Storing Page Source length in Int variable
int b8 = driver.getPageSource().length();

// Printing the Page Source length
System.out.println("Length of the page source is : " + b8);

//close the browser or page currently which is having the focus.
driver.close();

//Close all the windows.
//driver.quit();
}
}

Below is the output of the commands executed as seen in the console window.

Image 1
  • SwitchTo command
  • Switching from one iframe to another
  • Handling Alerts

The above three useful commands are not covered in this part of the tip, I will be penning these details in my upcoming article.

Thanks for reading. Hope this helps beginners in testing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)