20 Selenium Interview Questions and Answers you should definitely Prepare
We all know that job interviews can be challenging especially if you don’t have all the information in one place. Moreover, preparing for an interview takes a lot more than Googling a list of common interview questions.
But don’t worry we have chalked down some Selenium interview questions and answers that can save your time and efforts.
Also, in case if you are looking to get hired for Selenium Automation Testing, then you should have the following skills:
- Test architecture
- Test design
- Performance testing
- Configuration management
- Manual testing agility & interaction
- Communication between teams
- Agile, DevOps, and continuous delivery
Let’s start with the basics and then we will step the ladder higher. We have tried to cover selenium java interview questions and selenium testing interview questions also.
1. Please explain the various Selenium components
Selenium IDE- It is a tool for recording and playing back. You can also call it a firefox plugin.
Selenium Grid- It is the best way to run parallel testing on multiple machines. The grid helps you in distributing tests while making sure that you save time in performing them on running in-browser test suites.
Selenium RC and WebDriver – It is a server that allows users to create test scripts in a desirable programming language like Java, .NET, PHP, etc. Also, remember that with most of the browsers Webdriver and RC work.
2. Can you explain the difference between assert and verify commands in Selenium?
Though both assert and verify commands checks whether the given condition is true or false. But one of the basic differences between the two lies after the condition checking is complete.
For instance, if a particular condition comes out to be false in the case of an assert, then the execution stops there only, and no further tests will be done. But if the condition is true then the program control will move to the next test step.
Whereas on the other hand Verify command, does not care about the result of the condition checking. Whether it is true or false, the program will be completed.
3. How will you find an element using Selenium?
There are different ways to find an element in a web page using Selenium:
4. What are the testing types that can be supported by selenium?
Testing types that can be supported by Selenium are as follows:
- Functional Testing
- Regression Testing
- Sanity Testing
- Smoke Testing
- Responsive Testing
- Cross Browser Testing
- UI testing (black box)
- Integration Testing
5. What is the syntax used to launch the browser using WebDriver?
The syntax used for launching any browser like Google Chrome, Mozilla Firefox, and Internet Explorer using WebDriver is:
new WebDriver driverDriver();
6. How will you find an element on the screen?
With the help of WebDriver we can easily find out any element on the screen. And we can do this with the following 3 methods:
- isDisplayed() boolean buttonPresence = driver.findElement(By.id(“some id”)).isDisplayed();
- isEnabled() boolean searchIconEnabled = driver.findElement(By.id(“some id”)).isEnabled();
- isSelected() boolean buttonSelected = driver.findElement(By.id(“some id”)).isSelected();
7. What are the four-parameters you have to pass in Selenium?
The four parameters that you have to pass in Selenium are:
- Port Number
8. How can you “submit” a form using Selenium?
With the help of the Webdriver software testing tool, one can easily submit any form. Just type element.submit(); or you can use click() method to click on any button of the software web application.
9. What is the difference between type keys and type commands?
Type keys Simulates keystroke events on the specified element, as though you typed the value key-by-key.
Type command is mainly used to type text into the text box and text area fields. Type command forces the specified value into the page directly.
This command may or may not have any visible effect, even in cases where typing keys would normally have a visible effect. For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in the field.
10. When should we use findElement() and findElements()?
findElement method is used to access a single web element on a page. It returns the first matching element. It throws a NoSuchElementException exception when it fails to find If the element.
findElements method returns the list of all matching elements. This method returns an empty list when the element is not available or doesn’t exist on the page. It doesn’t throw NoSuchElementException.
11. What are JUnit Annotations and what are different types of annotations which are useful?
JUnit is a Java-based, open-source framework to help you execute unit testing. JUnit is used primarily to test each and every unit or component of your application like classes and methods. It helps to write and run repeatable automated tests, to ensure your code runs as intended. The framework offers support to Selenium for automation testing web-apps or websites.
@BeforeClass: This annotation is used to initialize any object that we use in our running test case. Used to execute some statements before all the test cases mentioned in the script.
@Before: This annotation is used whenever we want to initialize an object for the time the method is being used. This annotation is usually used for setting up the test environment.
@Test: This annotation tells JUnit that the public void method() to which it is attached can be run as a test case. This annotation includes the test method for an application that you want to test.
@After: The initialization done in the @Before annotation method should be released in the @After annotation method. So, this annotation is executed every time after each test method. The primary purpose of @After annotation is to teardown. The teardown is a process of deleting temporary data.
@AfterClass: The initialization done in the @BeforeClass annotation method should be released in the @AfterClass annotation method. So, this annotation is executed once but it will be executed after all tests have finished executing.
@Ignore: This annotation tells JUnit that this method shall not be executed. In scenarios, where our code module is not ready in a particular test case, we can temporarily put that code module in the @Ignore annotation method to avoid test case failure.
12. What is POM (Page Object Model)? What are its advantages?
POM is a design pattern used for enhancing test maintenance and reducing code duplication.
A page object is an object-oriented class that serves as an interface to a page of your application under test. The tests then use the methods of this page object class whenever they need to interact with the User Interface (UI) of that page.
The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently, all changes to support that new UI are in a single place.
Advantages of the Page Object Model Framework:
- Code reusability – Achieve code reusability by writing the code once and use it in different tests
- Code maintainability – There is a clean separation between test code and page specific code such as locators and layout which becomes very easy to maintain code. Code changes only on Page Object Classes when a UI change occurs. It enhances test maintenance and reduces code duplication
- Object Repository – Each page will be defined as a java class. All the fields in the page will be defined in an interface as members. The class will then implement the interface
- Readability – Improves readability due to clean separation between test code and page specific code
13. What is the difference between Page Object Model (POM) and Page Factory?
Page Object is a class that represents a web page and holds the functionality and members. Page Object Model is a way of representing an application in a test framework. For every page in the application, we create a Page Object to reference the page.
Page Factory is a way to initialize the web elements you want to interact with within the page object when you create an instance of it. Page Factory is one way of implementing the Page Object Model.
14. What are the different types of WAIT statements in Selenium WebDriver?
Implicit wait: Directs the WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs.
Explicit Wait: The WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. It is important in cases where there are certain elements that naturally take more time to load.
15. Explain the various types of navigation commands supported by Selenium?
- navigate().back() – This method enables the web browser to click on the back button in the existing browser window. It neither accepts anything nor returns anything.
- navigate().forward() – This method enables the web browser to click on the forward button in the existing browser window. It neither accepts anything nor returns anything
- navigate().refresh() – This method refreshes/reloads the current web page in the existing browser window. It neither accepts anything nor returns anything.
- navigate().to() – This method loads a new web page in the existing browser window. It accepts string as a parameter and returns void.
16. How will you handle web-based pop-ups in Selenium?
WebDriver allows handling web-based pop-ups via the Alert interface. The general syntax is:
Alert alert = driver.switchTo().alert();
A total of 4 methods are available for handling the web-based pop-ups, namely:
- String getText() – This method returns the text displayed on the alert box.
- void accept() – This method clicks on the “Ok” button as soon as the pop-up window appears.
- void dismiss() – This method clicks on the “Cancel” button as soon as the pop-up window appears.
- void sendKeys(String stringToSend) – This method enters the specified string pattern into the alert box.
17. How to take screenshots in Selenium WebDriver?
Screenshot can be taken using the TakeScreenshot function.
By using the getScreenshotAs() method you can save that screenshot.
Example: File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);
18. What is the difference between “/” and “//”?
Single Slash “/” – Single slash is used to create XPath with absolute path i.e. the XPath would be created to start selection from the document node/start node.
Double Slash “//” – Double slash is used to create XPath with a relative path i.e. the XPath would be created to start selection from anywhere within the document.
19. Can you explain the differences between Selenium and QTP?
Below are some parameters listing the differences between Selenium and QTP
- Availability – Selenium is an open-source and free-to-use testing tool while QTP is a licensed and commercial testing tool.
- Browser Compatibility – QTP provides support for only Chrome, Firefox, and Internet Explorer while Selenium can be used with the aforementioned plus Opera, Safari, and several others.
- Object Repository – QTP automatically creates and maintains an Object Repository. While Selenium needs to create an Object Repository while working with the automation testing tool.
- Programming Language Support – The only programming language supported by QTP is VB but Selenium provides support for multiple programming languages including C#, Java, Perl, Python, and Ruby.
- Testing Support – Selenium offers testing for only web applications while QTP provides testing support for both web-based and Windows-based applications.
- Vendor Support – No vendor support is available with Selenium while the support is available for QTP.
20. What is the Robot class in Selenium?
A Robot class in Selenium is used to generate input events for test automation, self-running demos, and other applications where you need control over the mouse and keyboard.
WebDriver cannot handle OS pop-ups, so in Java 1.3, the Robot class was introduced. The primary purpose of this Robot class is to facilitate automated testing for Java platform implementations and to provide control over mouse and keyboard devices.