Selenium is an open-source automation testing tool used to test web applications for various browsers. It is an in-demand tool that increases job opportunities for freshers and experienced professionals. These Top 20 Selenium Interview Questions and Answers help you clear the technical rounds of Selenium jobs.
Selenium Interview Questions and Answers for Freshers
1. What is Selenium Suite and what are the different components of Selenium?
Selenium is a package of numerous testing tools and each tool is implemented for various testing requirements. The components of Selenium Suite are Selenium Integrated Development Environment (IDE), Selenium Remote Control (RC), Selenium WebDriver, and Selenium Grid.
2. What are the benefits of automation testing?
Automation testing is the process of automating the manual testing of an application by utilizing testing tools that allow testers to create scripts that can be executed repeatedly.
The benefits of automation testing are as follows:
- It supports the performance and functional testing of an application
- It facilitates the execution of repeated tests and supports parallel execution
- It improves accuracy and efficiency.
- It reduces manual intervention
- It helps large-scale test matrices
- It saves valuable time and money.
3. What are the uses of Selenium for automation testing?
The advantages of Selenium are that it is open-source, language assistance, platform-independent, browser-independent, supports major programming languages and frameworks, is tested on various services, has regular updates, has selenium suites, and can be easily implemented.
4. What are the limitations of selenium as a testing tool?
The disadvantages of the Selenium testing tool are that testing can be done for web applications only, there are no built-in reporting and test management features, there is no availability of reliable tech support, and it may require an in-depth knowledge of programming languages.
5. Can we use Selenium to launch web browsers?
Yes, Selenium offers great support for launching web browsers such as Google Chrome, Internet Explorer, and Mozilla Firefox. Following are the commands to be used in launching web browsers with Selenium.
- WebDriver driver = new FirefoxDriver();
- WebDriver driver = new ChromeDriver();
- WebDriver driver = new InternetExplorerDriver();
6. Define Selenese.
The language used for developing test scripts for Selenium IDE is known as Selenese. There is a set of commands that are to be used for testing the web application or system.
7. List the various commands of Selenese.
Selenium commands are divided into three major categories such as actions that are used to interact with web applications directly, accessors that allow users to store values in a user-defined variable, and assertions that allow a comparison of the current and expected state of the application.
8. Define locator and list any five types of locators of Selenium
A locator is an address that helps the testers identify web elements uniquely within a web page. Some of the types are ID, ClassName, LinkText, Xpath, and DOM.
9. Differentiate assert and verify commands in Selenium
The assert condition is used to stop the execution of the testing when the given condition is false; otherwise, it will continue with the further tests, In verify, the condition does not stop the execution flow irrespective of whether the condition is true or false.
10. Define the exception test of Selenium
An exception test expects an exception to be thrown inside a test class and it expects a @test annotation to follow the expected exception name in the brackets.
11. What is XPath in Selenium?
XPath is defined as XML-Path (Extensible Markup Language Path), which is the language used for querying XML documents and providing functionalities to locate elements in Selenium by repeating through each element in a web page.
12. What are XPath Absolute and XPath Relative?
In XPath, data will be stored in a key-value pair that is similar to an HTML tag. If it uses a single slash “/”, it is an absolute path, and if it uses a double slash “//”, then it is a relative path.
Example for Absolute Path: Absolute XPath: /html/body/div/form/input
Example for Relative Path: Relative XPath: //input[@id = ‘email’]
13. What is the difference between “type” and “typeAndWait” in Selenium?
The “type” command is used for entering the key values into a web application’s text box and it can also be used for choosing values from a combo box. On the other hand, the “typeAndWait” command is used for finishing the typing and allows a web page to begin to reload.
Selenium Interview Questions and Answers for Experienced
14. Explain the differences between findElement() and findElements() in Selenium with examples.
The findElement() command is used to fetch the first element that matches the locator, and if there is no match found, it sends a ‘NoSuchElementException’.
Syntax: WebElement button = webdriver.findElement(By.name(“<<Name value>>”));
The findElements() function produces a list of all elements that match the locator and if there are no matches found, an empty list will be returned.
Syntax: List<WebElement> buttons = webdriver.findElements(By.name(“<<Name value>>”));
Example for 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 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();
}
}
Example for findElements()
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();
}
}
15. How will you wait until a web page has been loaded completely in Selenium?
There are two popular methods to ensure that the web page has been loaded completely in Selenium such as Immediately after developing the webdriver instance with the below syntax,
temp = driver.Manage().Timeouts().ImplicitWait;
the second one is to call JavaScript to return the document.readyState till “complete” is returned with the following syntax.
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript(“return document.readyState”).equals(“complete”));
16. Differentiate driver.close() and driver.quit() in Selenium
The major difference between the both commands is the driver.close() closes the active window on which the user is working, while the driver.quit() closes all the windows opened by the program.
17. What are the navigation commands supported by Selenium?
There are four major navigation commands as below
- navigate().back() used to take the user to the last web page of the browser history.
- navigate().forward() is utilized to navigate to the user’s current web page from their browser’s history.
- navigate().refresh() is used to refresh a webpage and reload its web components.
- navigate().to() used to navigate to a particular URL in a new web browser.
18. What is the use of the Pause Feature in Selenium?
The pause feature is used to handle exceptions in the test script by enabling the user to pause at the statement that causes the exception and enter the debug mode by clicking on the pause icon on the top right corner of the Selenium IDE.
It prevents the complete test case failure and provides the testers a chance to debug the error immediately.
19. What is the code to create right-click and mouse hover actions in Selenium?
To replicate right-click action
actions action = newActions(driver);
WebElement element = driver.findElement(By.id(“elementId”));
action.contextClick(element).perform();
To replicate the mouse hover action
actions action = newActions(driver);
WebElement element = driver.findElement(By.id(“elementId”));
action.moveToElement(element).perform();
20. Write a simple code to illustrate the capturing event of the screenshot in Selenium
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TakeScreenshot {
WebDriver drv;
@ Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
drv.get(“https://google.com”);
}
@ After
public void tearDown() throws Exception {
drv.quit();
}
@ Test
public void test() throws IOException {
File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(“C:\\Screenshot\\Scr.jpg”))
}
}
21. What are the various types of frameworks and connections of Selenium with the Robot framework?
The various types of frameworks of Selenium are as follows
- Behavior-driven Development Framework
- Data-driven Testing Framework
- Keyword-driven Testing Framework
- Library Architecture Testing Framework
- Module-based Testing Framework
- Hybrid Testing Framework
Robot Framework is one of the automation frameworks that can communicate with third-party libraries and functions for executing a web testing library such as Selenium.
22. Explain the use of Selenium through a sample test application
To execute a demo Selenium test script, we require Java SDK for the operating system, Java-based IDE like Eclipse or IntelliJ, and Selenium WebDriver for dependency on Java IDE.
package scalerAcademy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
public class MyFirstTestClass {
public static void main(String[] args) throws InterruptedException {
//It sets the system property to the given value.
System.setProperty(“webdriver.gecko.driver”,”D:\\Softwares\\geckodriver.exe”);
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.google.com/”);
//Launch website in the browser
driver.manage().window().maximize();
//The sleep pauses the execution of the thread for 5000 ms.
Thread.sleep(5000);
driver.quit();
}
}
23. What are the various types involved in Selenium Testing?
There are seven basic steps to performing Selenium Testing
- Create an instance of a WebDriver
- Navigate to a webpage
- Locate an HTML element of the webpage
- Perform actions on an HTML element
- Anticipate browser response from the action
- Execute tests and record the results using a test framework
- Conclude with a test report
Conclusion
Numerous strong choices are becoming more and more popular as the demand for selenium rises. With any luck, these selenium interview questions and answers can come in handy during your interviews. Gain expertise in automated testing with our Selenium training in Chennai.