Selenium WebDriver- Commands

Last updated on Nov 29 2021
Manikaran Reddy

Table of Contents

Selenium WebDriver- Commands

As we’ve discussed earlier within the IDE section, Selenium commands are the set of commands that are wont to run our Selenium tests.
In Selenium WebDriver, we’ve a completely different set of commands for performing different operations. Since we are using Selenium WebDriver with Java, commands are simply methods written in Java language.
Note: A java method may be a collection of statements that are grouped together to perform a selected operation.
Before stepping into the small print of commands provided by Selenium WebDriver, we insist you to travel through the Java OOPs (Object-Oriented Programming) concepts in java programing language . you’ll also ask our Java OOPs concepts section provided within the Java Tutorial.
Now, the question arises is that how can we access methods provided by WebDriver.
Till now, we’ve successfully created our first test script in Selenium WebDriver. Therefore, one possible thanks to view the methods provided by WebDriver is to open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver and press the dot key. it’ll show you all of the possible methods provided by WebDriver.

Selenium Web Driver
Selenium Web Driver

Let us consider an example form the suggestions displayed by Eclipse to know the syntax of the methods provided by WebDriver.

Selenium Web Driver
Selenium Web Driver

Method Name
To access any method of any class, we’d like to make an object of sophistication then all the general public methods will appear for the thing .
Parameter
A parameter is an argument which is passed to a way to perform some specific operation.
Return type
Methods can return a worth or returning nothing (void). If the void is mentioned after the tactic , it means, the tactic is returning no value. If it’s returning a worth , then it must display the sort of the worth for e.g. getTitle(): String.
Now, we’ll discuss the varied commands provided by WebDriver. The commands provided by Selenium WebDriver are often broadly classified in following categories:
1. Browser Commands
2. Navigation Commands
3. WebElement Commands
Given below are a number of the foremost commonly used Selenium commands in WebDriver:
1. Fetching an internet page
There are two methods to fetch an internet page:
• Using Get method

1. driver.get("www.tecklearn.com")

• Using Navigate method

1. driver.navigate().to("https://tecklearn.com/selenium-tutorial");
2. Locating forms and sending user inputs
1. driver.findElement(By.id("lst-ib")).sendKeys("tecklearn tutorials");

3. Clearing User inputs
The clear() method is employed to clear the user inputs from the text box.

1. driver.findElement(By.name("q")).clear();

4. Fetching data over any web element
Sometimes we’d like to fetch the text written over an internet element for performing some assertions and debugging. We use getText() method to fetch data written over any web element.

1. driver.findElement(By.id("element567")).getText();
5. Performing Click event
The click() method is employed to perform click operation on any web element.
1. driver.findElement(By.id("btnK")).click();
6. Navigating backward in browser history
1. driver.navigate().back();
7. Navigating forward in browser history
1. driver.navigate().forward();
8. Refresh/ Reload an internet page
1. driver.navigate().refresh();
9. Closing Browser
1. driver.close();
10. Closing Browser and other all other windows related to the driving force
1. driver.quit();
11. Moving between Windows
1. driver.switchTo().window("windowName");
13. Moving between Frames
1. driver.switchTo().frame("frameName");
14. Drag and Drop
Drag and Drop operation is performed using the Action class.
1. WebElement element = driver.findElement(By.name("source"));
2. WebElement target = driver.findElement(By.name("target"));
3.
4. (new Actions(driver)).dragAndDrop(element, target).perform();

Let us consider a sample test script which can cover most of the commonly used WebDriver commands.
For our test purpose, we are employing a dummy website under the URL:
https://www.testandquiz.com/selenium/testing.html
The default interface of the online page seems like this:

Selenium Web Driver
Selenium Web Driver

You can also use this dummy website for your Selenium Testing practice.
First you would like to download the browser driver for the browser on which you’re willing to automate your test scenarios. we’ve already discussed execution of Selenium test scripts on different browsers within the previous sections of this tutorial.
For this test, we are using Firefox Gecko driver to automate our test scenarios on Firefox browser.
Below is that the sample test script with embedded comments.

1. import org.openqa.selenium.By;
2. import org.openqa.selenium.WebDriver;
3. import org.openqa.selenium.firefox.FirefoxDriver;
4. import org.openqa.selenium.remote.DesiredCapabilities;
5. import org.openqa.selenium.support.ui.Select;
6.
7. public class Second {
8.
9. public static void main(String[] args) {
10.
11. // System Property for Gecko Driver
12. System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
13.
14. // Initialize Gecko Driver using Desired Capabilities Class
15. DesiredCapabilities capabilities = DesiredCapabilities.firefox();
16. capabilities.setCapability("marionette",true);
17. WebDriver driver= new FirefoxDriver(capabilities);
18.
19. // Launch Website
20. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
21.
22. // Fetch the text "This is sample text." and print it on console
23. // Use the category name of the div to locate it then fetch text using getText() method
24. String sampleText = driver.findElement(By.className("col-md-12")).getText();
25. System.out.println(sampleText);
26.
27. // Use the linkText locator method to seek out the link and perform click using click() method
28. driver.findElement(By.linkText("This may be a link")).click();
29.
30. // Click on the textbox and send value
31. driver.findElement(By.id("fname")).sendKeys("Tecklearn");
32.
33. // Clear the text written within the textbox
34. driver.findElement(By.id("fname")).clear();
35.
36. // Click on the Submit button using click() command
37. driver.findElement(By.id("idOfButton")).click();
38.
39. // Locate the radio button by id and check it using click() function
40. driver.findElement(By.id("male")).click();
41.
42. // Locate the checkbox by cssSelector and check it using click() function
43. driver.findElement(By.cssSelector("input.Automation")).click();
44.
45. // Use Select class for choosing value from dropdown
46. Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));
47. dropdown.selectByVisibleText("Automation Testing");
48.
49. // Close the Browser
50. driver.close();
51.
52. }
53.
54. }

So, this brings us to the end of blog. This Tecklearn ‘Selenium WebDriver – 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- Commands"

Leave a Message

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