Selenium WebDriver Browser Commands

Introduction

In this part of my article, 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 refers what Selenium has to do. Justlike a master commands a slave in early days!! sorry even today. OK!! Lets 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 witting 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() “

getTitle() 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 open  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 froward 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 close the current window of the browser.

5. Quit Command – ” quit()

This command is use to quit the browser and all the opened windows in the browser.

These are the basic commands that are needed for witting 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 a 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:-

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

SwitchTo command
Switching  from one iframe to another
Handling Alerts
The above three Useful commands are not covered in this part of the Article, I will be penning these details in my upcoming article.
Hope this helps anyway to beginners in testing.
Thanks.
🙂

Leave a comment