Selenium Interview Questions for Beginners
1. What is Selenium?
Selenium is an open-source (free) automated testing framework for validating web applications across multiple browsers and platforms. Selenium Test Scripts can be written in a variety of programming languages, including Java, C#, Python, and others. Selenium Testing is the term for testing done using the Selenium testing tool.
Selenium Software is a collection of tools, each of which caters to a specific organization's Selenium QA testing requirements. The following is a list of tools incorporated within Selenium:
- Selenium Integrated Development Environment (IDE).
- Selenium Remote Control (RC).
- Selenium WebDriver.
- Selenium Grid.
2. What are the Selenium suite components?
Selenium is a package of several testing tools. It is therefore often referred to as a Selenium Suite with each of these tools designed to cater to a different testing requirement.
The following are the different components of Selenium Suite:
- Selenium Integrated Development Environment (IDE): It is a Firefox/Chrome plug-in that is developed to speed up the creation of automation scripts by recording the user actions on the web browser and exporting them as a reusable script.
- Selenium Remote Control (RC): It is a server that enables users to generate test scripts in their preferred programming language. It accepts commands from the test scripts and sends them to the browser as Selenium core JavaScript commands, for the browser to behave accordingly.
- Selenium WebDriver: It is a programming interface that helps create and run test cases by directly communicating with the web browser and using its native compatibility to automate. Unlike RC, it doesn’t require an additional server to create and run test cases.
- Selenium Grid: It allows parallel execution of tests on different browsers and operating systems by distributing commands to other machines simultaneously.
3. What are the advantages of using Selenium as an automation tool?
The following are the advantages of using Selenium for automated testing :
- Open-Source: Selenium's greatest strength, as previously said, is that it is a freeware and portable tool. There are no out-of-pocket expenses. The utility can be downloaded for free, and community-based help is also accessible.
- Language assistance: Java, Perl, Python, C#, Ruby, Groovy, JavaScript, and other languages are supported by Selenium. It has its own script, yet it is not constrained by it. It can work with a variety of languages, depending on the developers' and testers' preferences.
- Compatible with a variety of operating systems: Selenium may run on a variety of operating systems, including Windows, Mac OS X, Linux, and UNIX. A customized testing suite can be constructed on any platform and then executed on another using the Selenium suite of products. For example, you may write test cases in Windows and run them on a Linux system with ease.
- Browser compatibility: Selenium is compatible with a variety of web browsers, including Internet Explorer, Chrome, Firefox, Opera, and Safari. When running tests and testing them across multiple browsers at the same time, this becomes really useful.
- Programming languages and framework support: Selenium works with a variety of programming languages and frameworks. For source code compilation, it can, for example, integrate with ANT or Maven frameworks. It may also be used to test apps and generate reports using the TestNG framework. Continuous Integration (CI), can integrate with Jenkins or Hudson, and it can also integrate with other open-source tools to offer other functionalities.
- Tests on a variety of devices: On Android, iPhone, and Blackberry, Selenium Test Automation can be used to automate mobile web application testing. This can aid in the generation of necessary results and the ongoing resolution of bugs present in the application.
- Regular updates: Selenium support is based on a community, which allows for frequent updates and upgrades. These upgrades are simple to install and don't require any special training. Selenium is thus both resourceful and cost-effective.
- Selenium suites with a lot of content: Selenium is more than just a single tool or utility; it's a full set of numerous testing tools that's why it's called a Suite. Each tool is tailored to specific testing needs and test environment constraints. Selenium also includes features such as Selenium IDE, Selenium Grid, and Selenium Remote Control (RC).
- Ease with which it can be implemented: Selenium has a user-friendly interface that makes it simple to develop and perform tests. Its open-source capabilities allow users to script their own extensions, making them simple to create, alter actions, and manipulate at a high level. Selenium's reporting features are also one of the reasons for its popularity, as it allows testers to extract results and take action based on them.
4. What are the disadvantages of using Selenium as a testing tool?
The following are the disadvantages of using Selenium as a testing tool:
- Tests web applications only: Selenium supports the testing of only web-based applications. Mobile applications, Captcha, and Barcode readers cannot be tested using Selenium unless integrated with third-party tools like Appium and TestNG.
- No built-in reporting and test management facility: Selenium can generate reports only using third-party tools like TestNG or JUnit.
- Unavailability of reliable tech support: Since Selenium is an open-source tool, no dedicated support for user issues is available.
- May require the knowledge of programming languages: Some prior programming knowledge is required to use Selenium.
5. What is automation testing, and what are its advantages?
Automation Testing or Test Automation is a process of automating the manual testing process of an application or a system by using testing tools that allow you to create scripts that can be executed repeatedly, generating detailed test reports of the application or system under test.
The advantages of Automated Testing are:
- It supports both the performance and functional testing of an application or system.
- It facilitates the execution of repeated test cases.
- It allows the parallel execution of the test cases.
- It improves the accuracy and efficiency of the system by reducing the manual intervention of humans to generate test cases.
- It helps in testing a large-scale test matrix.
- It saves valuable time and money for the testing team involved in the project.
6. Why should Selenium be selected as a testing tool for web applications or systems?
Selenium provides the following advantages, which make it an excellent automated testing framework:
- It is free and open-source software with a large user base and supports providing community.
- It has cross-browser compatibility and supports multiple browsers like Google Chrome, Mozilla Firefox, Internet Explorer, Edge, Opera, Safari, etc.
- It supports multiple operating systems such as Windows, Linux, macOS, etc.
- It facilitates the usage of multiple programming languages including Scala, Ruby, Python, PHP, Perl, Java, Groovy, C#, etc.
- It provides support for distributed testing as well.
7. What is Selenese? Explain different types of Selenium commands.
The language used for writing test scripts in Selenium IDE is called Selenese. It is a set of commands used to test your web application or system. Selenium commands could be divided into 3 major categories:
- Actions: These are the commands interacting directly with web applications.
- Accessors: These are the commands which allow users to store values in a user-defined variable.
- Assertions: They enable a comparison of the current state of the application with its expected state.
8. What is meant by a locator and name a few different types of locators present in Selenium.
A locator is an address for uniquely identifying web elements within a web page. There are different types of locators present in Selenium to identify web elements uniquely and accurately like:
- ID
- ClassName
- Name
- TagName
- LinkText
- PartialLinkText
- Xpath
- CSS Selector
- DOM.
9. What is the exception test in Selenium?
An exception test is a test that expects an exception to be thrown inside a test class. It expects a @Test annotation followed by the expected exception name in the brackets.
Eg: @Test(expectedException = NoSuchElementException.class)
is an exception test for missing elements in Selenium.
10. What is XPath in Selenium? Explain XPath Absolute and XPath Relative.
XPath, also defined as XML-Path (Extensible Markup Language Path), is a language used to query XML documents and provide functionalities like locating elements in Selenium by iterating through each element in a webpage. In XPath, data is stored in a key-value pair format similar to an HTML tag. It uses a single slash, i.e. ‘ / ’ for creating an absolute path, and a double slash, i.e. ‘ // ’ for creating a relative path for an element to be located on a webpage.
11. In Xpath, what is the difference between "/" and "//"?
- Single Slash "/" - A single slash is used to create an Xpath with an absolute path, i.e., the XPath will begin with the document node/start node. For example,
Absolute XPath: /html/body/div/div/form/input
Here, /html is the root html node.
- Double Slash "//" - The double slash is used to construct an Xpath with a relative path, which means the XPath can start selection from anywhere on the page. For example,
Relative XPath: //input[@id = 'email']
Here, we can locate an input having id = ‘email’ present anywhere in the document object model (DOM).
12. What is the difference between the commands "type" and "typeAndWait" in the context of Selenium?
The "type" command is used to enter keyboard key values into a software web application's text box. It can also be used to choose values from a combo box, whereas the "typeAndWait" command is used when you finish typing and the software web page begins to reload. This command will wait for the page of the software program to reload before proceeding. You must use a basic "type" command if there is no page reload event when typing.
13. Differentiate between findElement() and findElements() in the context of Selenium with proper examples.
Following table lists the differences between findElement() and findElements() in Selenium:
findElement()findElements()The first web element that matches the locator is returned. It gives you a list of all the web items that match the locator. If there are no matching web elements, a NoSuchElementException is produced. If there are no matching elements, an empty list is returned.Syntax − WebElement button = webdriver.findElement(By.name("<<Name value>>"));
Syntax − List<WebElement> buttons = webdriver.findElements(By.name("<<Name value>>"));
- Using
findElements()
:-
// JAVA
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class findElements {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vaibhav\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.exampleurl.com/example.htm";
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> rows = driver.findElements(By.xpath("//table/tbody/tr[2]/td")); // xpath with index appended to get the values from the row 1of table using findElements(), which returns a list
System.out.println("The number of values in row 2 is "+ rows.size());
driver.close();
}
}
Explanation - In the above code, first of all, we import all the necessary headers and then set up the driver for the Chrome browser. We use the findElements()
method to find all the values present in the 2nd row of a table in the given URL web page using the XPath of the element.
- Using
findElement()
:-
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class findTagname {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vaibhav\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.exampleurl.com/example.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("input[id='search']")).sendKeys("Selenium"); //Using id tagname attribute combination for css expression and get the element from findElement()
driver.close();
}
}
Explanation - In the above code, first of all, we import all the necessary headers and then set up the driver for the Chrome browser. We use the findElement() method to find an input element having an id attribute set as search.
14. In Selenium, how will you wait until a web page has been loaded completely?
There are two methods of making sure that the web page has been loaded completely in Selenium.
They are as follows:
1. Immediately after creating the webdriver instance, set an implicit wait:
temp = driver.Manage().Timeouts().ImplicitWait;
On every page navigation or reload, this will try to wait until the page is fully loaded.
2. Call JavaScript return document.readyState till "complete" is returned after page navigation. As a JavaScript executor, the web driver instance can be used.
Code example:
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
15. What is Selenium WebDriver?
Selenium WebDriver, also known as Selenium 2, is a browser automation framework that accepts and sends commands to a browser to implement it. It has direct control over the browser because it communicates with it directly. Java, C#, PHP, Python, Perl, and Ruby are all supported by Selenium WebDriver.
Unlock the Power of Automation with Our Comprehensive Selenium Full Training — Accelerate Your Testing Success Today!
Selenium WebDriver with Java and Python (Basics + Advance + Architect)
16. Is Selenium WebDriver a library?
Selenium WebDriver is a prominent free open-source library for automating browsers and testing web applications.
17. Is Selenium WebDriver an interface or a class?
Selenium WebDriver is usually a set of methods defined by an interface. The browser-specific classes, on the other hand, provide an implementation of it by extending a class. AndroidDriver, ChromeDriver, FirefoxDriver, InternetExplorerDriver, SafariDriver, and others are some of the implementation classes.
18. What are the different types of WebDriver Application Programming Interfaces in Selenium?
The Various Types of WebDriver APIs in Selenium are as follows:
- Opera Driver
- InternetExplorer Driver
- Chrome Driver
- Safari Driver
- Android Driver
- Firefox Driver
- Gecko Driver
- iPhone Driver
- EventFiringWebDriver
- HTMLUnit Driver.
19. What programming languages does Selenium WebDiver support?
The various programming languages that Selenium WebDriver supports are as follows:
- Java
- C#
- Python
- Ruby
- Perl
- PHP
20. What open-source frameworks does Selenium WebDriver support?
The following are the open-source frameworks supported by the Selenium WebDriver:
TestNG:
- TestNG, a testing framework for the Java programming language that was influenced by JUnit and NUnit.
- TestNG was created with the purpose of covering a wider range of test categories, including unit, functional, end-to-end, integration, and so on, with more robust and user-friendly functions.
JUnit:
- It is used for Unit Testing of various types of applications.
21. What is WebDriver's super interface?
SearchContext is the Super Interface of the WebDriver.
22. Explain the following line of code.
Webdriver driver = new FirefoxDriver();
'WebDriver' is an interface, and we are generating a WebDriver object by instantiating a FirefoxDriver object (This object uses Firefox Driver to link the test cases with the Firefox browser).
23. Is it necessary to use Selenium Server to run Selenium WebDriver scripts?
Selenium Server is required when distributing Selenium WebDriver scripts for execution with Selenium Grid. Selenium Grid is a Selenium functionality that allows you to execute test cases on multiple machines on various platforms. You wish to execute your test cases on a remote machine because your local machine is running numerous applications. You will need to set up the remote server so that the test cases can run on it.
24. What will happen if I execute this command? driver.get(“www.interviewbit.com”) ;
An exception is triggered if the URL does not begin with http or https. As a result, the HTTP protocol must be sent to the driver.get()
method.
25. What is an alternative option to driver.get() method to open an URL in Selenium Web Driver?
driver.navigate()
can be used instead. It is used for navigating forwards and backwards in a browser.
26. What is the difference between driver.get() and driver.navigate.to(“url”)?
The difference between the two is as follows:
driver.get()
: To open a URL and have it wait for the entire page to load.driver.navigate.to()
: To navigate to a URL without having to wait for the entire page to load.
27. What are the differences between the methods driver.close() and driver.quit()?
The functions of these two methods (driver.close
and driver.quit
) are nearly identical. Although both allow us to close a browser, there is a distinction.
- To close the current WebDriver instance, use
driver.close()
. - To close all open WebDriver instances, use
driver.quit()
.
28. What are some cases that Selenium WebDriver cannot automate?
Some of the scenarios which we cannot automate are as follows:
- Selenium WebDriver does not support bitmap comparison.
- Using Selenium WebDriver to automate Captcha is not possible.
- Using Selenium WebDriver, we are unable to read bar codes.
- Video streaming scenarios: Selenium will almost never be able to recognise video controllers. To some extent, JavaScript Executor and flex UI selenium will work, although they are not completely dependable.
- Performance testing can be automated, however, it's preferable to avoid using Selenium for performance testing.
29. In Selenium WebDriver, what is an Object Repository?
Instead of hard-coding element locator data in the scripts, the Object Repository is used to store the element locator data in a centralized location. To store all of the element locators, we create a property file (.properties), which acts as an object repository in Selenium WebDriver.
Instead of hard-coding element locator data in the scripts, the Object Repository is used to store the element locator data in a centralized location. To store all of the element locators, we create a property file (.properties), which acts as an object repository in Selenium WebDriver.
Unlock the Power of Automation with Our Comprehensive Selenium Full Training — Accelerate Your Testing Success Today!
Selenium WebDriver with Java and Python (Basics + Advance + Architect)