Selenium Tutorial Selenium
Selenium Tutorial Selenium
Selenium Introduction
Hello friends! this is our first tutorial of the Selenium tutorial series. In this tutorial, we will study the
basics of the Selenium suite, its different components, features, advantages, and limitations.
Content
What is Selenium?
Components of Selenium Suite
Advantages of Selenium
Limitations of Selenium
What is Selenium WebDriver?
Conclusion
What is Selenium?
Selenium is a free and open-source test automation suite used for automating web-based applications.
It supports automation across different browsers, platforms, and programming languages.
Using Selenium, we can automate the functional tests and easily integrate them with Maven, Jenkins,
and other build automation and continuous integration tools.
Advantages of Selenium
Let’s now see some of the advantages of Selenium-
1. Selenium is open source, there is no licensing cost for its usage.
2. Scripting can be done in most of the widely used programming languages like Java, C#,
Ruby, and Python.
3. It provides platform support for all the major operating systems – Windows, Linux, Mac
OS, and Solaris.
4. It supports most of the popular browsers like Chrome, Firefox, Edge, Internet Explorer,
Opera, and Safari.
5. The Selenium IDE component of the Selenium suite provides record and playback
features using which non-programmers can also write automation scripts.
6. Selenium Grid helps in parallel and distributed testing.
Limitations of Selenium
Some of the limitations of Selenium are-
1. Selenium does not provide desktop application automation support.
2. Web Services – REST or SOAP cannot be automated using Selenium.
3. Selenium WebDriver requires programming language requirements for script creation.
4. For performing common tasks required in automation like logging, reading-writing to
external files, we have to rely on external libraries.
Let’s check the Selenium WebDriver component in a little more detail now.
Apart from this, Selenium WebDriver can also handle scenarios like alerts, pop-ups, ajax requests,
keyboard, and mouse actions easily.
Since Webdriver directly calls the methods of different browsers hence we have a separate driver for
each browser.
Conclusion
This completes our tutorial on Introduction to Selenium and its most important component Selenium
WebDriver.
Launching Browsers in Selenium
Content
Understanding the browser launching command
Launching Firefox Browser
Launching Chrome Browser
Launching Internet Explorer Browser
Launching Safari Browser
This is the java implementation of launching a browser in Selenium. Here, ‘WebDriver’ is an interface
and we are creating a reference variable ‘driver’ of type WebDriver, instantiated using ‘FireFoxDriver’
class.
For those who are not very proficient in Java, an interface is like a contract that classes implementing it
must follow. An interface contains a set of variables and methods without anybody(no implementation,
only method name, and signature). We cannot instantiate objects from interfaces. Hence, the below line
of code is incorrect and throws a compile-time error saying “Cannot instantiate the type WebDriver”.
WebDriver driver = new WebDriver();
For instantiation of driver objects, we need classes like FirefoxDriver or ChromeDriver which have
implemented the WebDriver interface. In other words, these driver classes have followed the contract
of WebDriver by implementing all the methods of the WebDriver interface. Thus making all the different
types of driver classes uniform, following the same protocol.
Please note that we can also create a reference variable of type FirefoxDriver like this-
FirefoxDriver driver = new FirefoxDriver();
But having a WebDriver reference object helps in multi-browser testing as the same driver object can be
used to assign to any of the desired browser-specific drivers.
Now, let’s see a sample code snippet implementing the above process. We will be studying each line of
code in the coming tutorials. For now, just consider it as a black-box and focus on the operations
performed(specified in the comments).
//Launching Firefox browser
WebDriver driver = new FirefoxDriver();
//Opening google.com
driver.get("http://www.google.com");
//Initializing webelement searchBox
WebElement searchBox = driver.findElement(By.name("q"));
//Writing a text "ArtOfTesting" in the search box
searchBox.sendKeys("ArtOfTesting");
So, here we see that in order to perform an operation on web element – searchBox, we first need to
locate it. Here, By.name(“q”) is a locator which when passed to findElement method returns a
searchBox web element.
Before going further with the different types of locators available in Selenium, let’s first see, how to get
the different attributes of an element that are used in the locators.
Content
Using Developer Tool
Locators in Selenium
Opening a URL
Using Get method-
The driver.get() method is used to navigate to a web page by passing the string URL as parameter.
Syntax-
driver.get("https://artoftesting.com");
Clicking on WebElements
The click() method in Selenium is used to perform the click operation on web elements. In our previous
tutorial, Locators in Selenium WebDriver, we studied about locating the webElements in Selenium. The
click() method is applied on the webElements identified, to perform the click operation.
Writing in a Textbox
The sendKeys() method can be used for writing in a textbox or any element of text input type.
//Creating a textbox webElement
WebElement element = driver.findElement(By.name("q"));
//Using sendKeys to write in the textbox
element.sendKeys("ArtOfTesting!");
Closing browser
Selenium provides two commands to close browsers close() and quit(). The driver.close() command is
used to close the browser having focus. Whereas, the driver.quite() command is used to close all the
browser instances open.
//To close the current browser instance
driver.close();
//To close all the open browser instances
driver.quit();
Sample Script
You can use the sample script below to automate the dummy webpage. The comments mentioned
throughout the script will guide you through the whole automation process. Also, note that the
Thread.sleep() used in the sample script is to pause the automation in between events. This
Thread.sleep() is not required and is only included to provide you some time to see the automation
events.
Download this java file here – seleniumBasicCommands.java.
package myTestPackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;
public class seleniumBasicCommands {
//Launch sampleSiteForSelenium
driver.get("https://artoftesting.com/sampleSiteForSelenium");
//Fetch the text "This is sample text!" and print it on the console
//Use the id of the div to locate it and then fecth text using the getText() method
String sampleText = driver.findElement(By.id("idOfDiv")).getText();
System.out.println(sampleText);
//Waiting for 3 seconds just for the user to efficiently check automation
//Its not mandatory though
Thread.sleep(3000);
//Using linkText locator to find the link and then using click() to click on it
driver.findElement(By.linkText("This is a link")).click();
Thread.sleep(3000);
//Finding textbox using id locator and then using send keys to write in it
driver.findElement(By.id("fname")).sendKeys("Kuldeep Rana");
Thread.sleep(3000);
Thread.sleep(3000);
Thread.sleep(3000);
Thread.sleep(3000);
Thread.sleep(50000);
}
Implicit & Explicit Waits in Selenium
Content
Why are waits required in Selenium?
Implicit Waits
Explicit Waits
Why are waits required in Selenium?
In UI automation, waits are required because certain elements get loaded on the page asynchronously,
so after triggering an event a page may get loaded successfully but some of its elements may still not get
loaded.
This causes elementNotFound exception while locating the element. In such cases, we are left with
using Thread.sleep() i.e. a static wait that will halt the test execution for some specified time and then
perform the next step.
As Thread.sleep() will wait for the specified time no matter if the elements get visible before that time.
So, using Thread.sleep() is never advisable in UI automation.
To avoid this Selenium provides different types of waits, out of which Implicit and Explicit waits are most
commonly used.
Implicit Waits
An implicit wait when used is set to the WebDriver instance and is applied to all the web elements. In
implicit wait the webdriver polls the DOM to check the availability of the webElement and waits till the
maximum time specified before throwing NoSuchElementException.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
In the above code snippet, the value 20 specified in the implicit wait method is the maximum time in
seconds till which WebDriver will wait before throwing NoSuchElementException while locating a
WebElement.
Explicit Waits
Unlike implicit waits, the explicit waits are applied to each and every web element. In explicit wait,
certain conditions are defined for which the WebDriver instance waits before locating web elements or
performing actions on them. Some of the most common conditions specified in explicit waits are-
elementToBeClickable, presenceOfElementLocated etc.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));