Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Common WebDriver Commands
2.1.
Fetching Web Page
2.2.
Locating Forms and Sending User Inputs
2.3.
Clear User Input
2.4.
Fetching Data from Text Element
2.5.
Perform Click Event
2.6.
Navigation, Close, Refresh, and Quit operation.
2.7.
Switching Frames and Windows
2.8.
Drag and Drop
3.
Program
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

WebDriver Commands

Author Aman Thakur
0 upvote

Introduction

This article will extensively cover topic related to Selenium test commands. The Selenium tests are executed using a set of instructions known as Selenium commands.

We have a completely distinct set of instructions for executing different actions in Selenium WebDriver. Because we're working with Java and Selenium WebDriver, commands are just Java methods. The article assumes that you are familiar with setting up and installation of web drivers and configuring your respective IDE accordingly and are familiar with Object Oriented Design patterns in Java.

Now we'll go through the different commands that WebDriver has to offer. The Selenium WebDriver commands may be categorised into the following categories:

1. Browser Commands

2. Navigation Commands

3. Web Element Commands

Common WebDriver Commands

This section will discuss the common web driver commands used in Selenium and are frequently used.

Fetching Web Page

There are primarily two ways to fetch the webpage:

1. Using Get method: webdriver.get(“www.codingninjas.com”);
2. Using navigate method: webdriver.navigate.to(“www.codingninjas.com”);

Locating Forms and Sending User Inputs

webdriver.findElement(By.id(“input-field”)).sendKeys(“coding ninjas”);  

Clear User Input

Clear user inputs from the text box

webdrtiver.findElement(By.name(“input-field”)).clear();

Fetching Data from Text Element

We occasionally need to retrieve the text placed over a web element in order to do assertions and debugging. To access data written over any web element, we utilise the getText() function.

webdriver.findElement(By.id(“input”)).getText();

Perform Click Event

To perform click operation: webdriver.findElement(By.id(“btn”)).click();

Navigation, Close, Refresh, and Quit operation.

1. Navigate forward: webdriver.navigate().forward();

2. Navigate Back: webdriver.navigate().back();

3. Refresh page: webdriver.navigate().refresh();

4. Close Page: webdriver.close();

5. Quit window: webdriver.quit();

Switching Frames and Windows

1. Switching Frames: webdriver.switchTo().frame(“frame-name”);

2. Switching Windows: webdriver.switchTo().window(“window-name”);

Drag and Drop

The Action class is used to conduct drag and drop operations.

WebElement element = webdriver.findElement(By.name(“source”));
WebElement target = webdriver.findElement(By.name(“target”));
(new Actions(driver)).dragAndDrop(element, target).perform();

Program

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class WebDriverCommands {
   public static void main(String[] args) {
       // system property for chrome driver
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");


       // initialize driver
       WebDriver webDriver = new ChromeDriver();


       // text
       webDriver.navigate().to("https://www.codingninjas.com/");
       String sampleText = webDriver.findElement(By.className("box-head zen-heading-3")).getText();
       System.out.println(sampleText);


       // click event
       webDriver.findElement(By.className("zen-base-button")).click();


       // navigation
       webDriver.navigate().back();
       webDriver.navigate().forward();


       // quit
       webDriver.quit();
   }
}

Frequently Asked Questions

1. What is the difference between click and submit in Selenium?
Ans: The click() method is only relevant to submit buttons in a form. The submit() method will wait for the page to load, but the click() function will only wait if there is an explicit wait condition. The submit() function cannot be used on a form with a submit of type button.

2. What's the fastest locator in Selenium?
Ans: The most recommended and fastest approach to identify needed WebElements on the page is to use Selenium's ID locator. Each element in the DOM has its own ID Selenium locator. Because each element on the page has its own ID, it is thought to be the quickest and safest way to locate them.

3. What is Page factory in Page object model?
Ans: Page Factory is a Selenium WebDriver class that implements the Page Object Model. The Page Factory idea is used to segregate the Page Object Repository from the Test Methods. You may use it to either initialise or directly instantiate Page Objects.

4. What is the parent class of WebDriver?
Ans: The most important interface in Selenium is SearchContext, which is extended by another interface called WebDriver. The RemoteWebDriver class implements all of the abstract methods of the SearchContext and WebDriver interfaces.

Conclusion

If you have reached till here that means, you really enjoyed this article, This article covers the important webDriver commands which are commonly used in under selenium, code snippets and their use cases. And a program which automated the java program.

Check out this problem - Smallest Distinct Window .

There are few articles that you might be interested in, such as installation of selenium Interview QuestionsDifference between RTL vs Enzyme . Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning !

Live masterclass