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