Must Read 30 Selenium Interview Questions & Answers: Ultimate Guide 2020

Are you attending an important interview and wondering what are all the Selenium interview questions you will go through?

We have created this most-read Selenium interview questions and answers guide to help you understand the depth of the questions and face it with confidence.

These days web apps are on the rise. With the growing need for web apps, there also is an inevitable requirement to test these web apps. That is where Selenium comes into action. Selenium is one of the commonly used automated testing tools which ensures the web app is working just right. If testing was to be done manually, it would utilize several man-hours and would increase the costing as well.

Further, since they are manual, they are prone to errors. Manual testing does not help in the long run, and so, automated testing like Selenium is gaining demand in the industry. If you are pursuing your career in automated testing, and have an upcoming interview in Selenium, this blog is meant for you.

In this blog, we shall discuss here the most common Selenium interview questions. These are for both – the beginners as well as Selenium interview questions for experienced. 

Selenium Interview Questions & Answers 2020

Q.1) Why pick Selenium over other automated testing tools?

Selenium is open-source. It is very easy to adapt compared to other automated tools in the market. Due to this reason, many companies pick up Selenium automated testing over other traditional methods. 

Q.2) List some benefits of Selenium over tools like TestComplete and QTP. Also, what are the disadvantages?

Selenium does not require a license, unlike TestComplete and QTP, being easy on pockets. The online community offers massive support. The release cycles are smaller, and the feedback is prompt compared to TestComplete and QTP. Further, Selenium works on Mac, Linux, and Windows as well. 

On the contrary, Selenium requires a developer to have high coding skills. Whereas QTP and TestComplete require low to moderate level of coding skills, respectively. 

Q.3) What changes have occurred in the various Selenium version upgrades?

In the first version of Selenium, Selenium v1, it only comprised three suites of tools, which are Selenium IDE, RC, and Grid. The Webdriver was missing. It was only in the second version of Selenium, Selenium v2 that the Webdriver was introduced. Once this was done, Selenium RC was no longer in use.

You can find them in the market, but the support isn’t available. The next version of Selenium is Selenium v3. It consists of Webdriver, IDE, and the Grid. It is currently in use. A newer version, Selenium v4, is also now available.

Selenium IDE is mainly for recording and playing back. The Webdriver is for testing the dynamic web applications using a programming interface. The Grid is used for employing tests in remote host machines. 

You must use the IDE for recording and playback of tests. A WebDriver is used for testing active web applications using a programming interface, and the Grid is employed for deploying tests in isolated host machines.

Q.4) What are the various exceptions in Selenium WebDriver?

Just like any other programming language, you can find exceptions in Selenium as well. You can find the following exceptions in Selenium WebDriver:

TimeoutException: You get this exception when a command does not perform an action in the specified time. 

NoSuchElementException: You get this exception when it cannot find an element with the given attributes on a web page.

ElementNotVisibleException: You get this exception when an element is available in the document object model, but it is not seen on the web page. 

StaleElementException: You get this exception when an element is not attached to the document object model or is deleted. 

Q.5) Explain Selenium exception test

The exception you expect to be thrown inside a test class is an exception test. If you write a test case intending it to throw an exception, you must use the @test annotation and also mention it in the parameters that which exception would be thrown. For instance, 

@Test(expectedException = NoSuchElementException.class)

Q.6) Is there a need for an excel sheet in a project? Is yes, how? 

Excel sheets are used as a data source during testing. Further, it also stores the data set while executing data-driven testing. When excel sheets are used as a data source, it can store: 

Application URL: Developers can mention the environment URL under which the testing is executed. For example, testing environment, development environment, QA environment, production environment, or staging environment.

User name and password information: Excel sheets can keep safe the access credentials like the username of a password of various environments. Developers can encrypt and store these details for security reasons. 

Test cases: Developers can make a table wherein one column write the test case name and the other which says to be executed or not. 

If you are going to use excel sheets for DataDriven Test, you can easily store the information for various duplications to be executed during the tests. For instance, all the data that needs to be written in a text box for testing on a web page can be stored in the excel sheets. 

Q.7) What is POM? List its advantages?

POM stands for Page Object Model. It is a design pattern for creating an Object Repository for web UI elements. Every single web page in the application must have its own corresponding page class, which is in charge of searching the WebElements in that page and then execute operations on them.

The advantages of using the Page object model are:

  • It makes the code readable by letting developers separate operations and UI flows from verification. 
  • Several tests can use the same Object Repository because it is independent of Test Cases.
  • The code becomes reusable.

Q.8) What is a Page Factory?

Page Factory offers an enhanced method to execute the Page Object Model by efficiently using the memory, and the execution is done using object-oriented design. 

 

POM Implementation
With Page Factory Without Page Factory
Uses By() Uses @FindBy()
No imports are required Imports Page factory
No cache storage Cache lookup is faster

 Page Factory initializes the elements of the Page Object or instantiates the Page Objects itself. Annotations for elements can also be produced. It is, in fact, a better way as the describing properties may not be expressive enough to differentiate one object from another

If POM is used without a page factory, instead of having to use ‘FindElements,’ @FindBy is used to look for WebElement, and initElements is used to initialize web elements from the Page Factory class.

@FindBy can accept attributes like tagName, name, partialLinkText , linkText, id, className , css, and xpath.

Q.9) How do you achieve synchronization in WebDriver? Or, tell us about the different types of wait statements Selenium Web Driver?

You can find two wait statements in Selenium web driver, namely, Implicit Wait and Explicit Wait.

Implicit wait commands the WebDriver to wait for a little by polling the DOM. It is present for the complete life of the web driver instance, once the implicit wait is declared. The pre-set value is zero. If you set it more than zero, then the behavior will poll the DOM on a regular basis based on the driver implementation.

Explicit wait commands the execution to wait for a little till a condition is attained like:

  • elementToBeClickable
  • presenceOfElementLocated
  • elementToBeSelected

Read: Spring Interview Questions & Answers

10) What is the use of JavaScriptExecutor?

You can execute JavaScript through Selenium Websriver using JavaScriptExecutor. It is an interface that offers this mechanism. It gives methods like “executescript” and “executeAsyncScript” to run JavaScript in the condition of the currently chosen frame or window. An example of that is:

JavascriptExecutor js = (JavascriptExecutor) driver; 

js.executeScript(Script,Arguments);

Q.11) Which function lets you scroll down a page using JavaScript in Selenium?

The function window.scrollBy() helps you scroll down the page using JavaScript in Selenium. For instance:

((JavascriptExecutor) driver).executeScript(“window.scrollBy(0,500”);

Q.12) How do you handle mouse and keyboard actions using Selenium?

Special mouse and keyboard actions are handled using Advanced User Interactions API. It comprises of the Actions and the Action Classes that are required for performing these events. The most used mouse and keyboard events are given by Action class are:

dragAndDrop(): This event performs click-and-hold at the position of the source element, moves.

source, target(): Moves to the position of the target element and releases the mouse.

clickAndHold(): It clicks the current location of the mouse.

Q.13) What are various types of Selenium frameworks?

The various types of Selenium frameworks are:

  • Keyword Driven Framework: In this framework, the operations and instructions are written in a separate file like Excel. 
  • Data-Driven Framework: In this framework, full test data is taken from some external source files like an XML, Excel, CSV, or some other database table.
  • Hybrid Framework: This framework is a blend of both the Keyword Driven framework and the Data-Driven framework.

Q.14) Name a few files that serve as a data source for various Selenium frameworks.

They can be an XML, Excel, CSV, or even a Text file.

Q.15) What is Selenese?

Selenese is the group of selenium commands to test a web application. Developers can use Assertions, Actions, and Accessors. Assertions are used as checkpoints. Actions are for running operations, and Accessors are used to store the value of a variable. 

Q.16) What is the major difference between a Page Factory and Page Object Model (POM)?

A common selenium interview question. A page factory is a method to initialize web elements within the page object on the creation of the instance. On the other hand, the page object model is a class that states the web page and holds its functionalities. 

Q.17) Does Selenium support handling window pop-ups?

No. Selenium does not support handling pop-ups. An alert, which is a pop-up window, displays a warning message on the screen. You can achieve this by using a few methods like: 

Void dismiss(): When the cancel button is clicked in the alert box, this method is called. 

Void accept(): When the ‘OK’ button of the alert is clicked, this method is called. 

String getText(): If you want to capture the alert message, you must call this method. 

Void sendKeys(String stringToSed): If you want to send some information to the alert box, you must call this method. 

Read: React Interview Questions & Answers

Q.18) Explain Robot class

A Robot class gives control over the keyboard and mouse devices.

The methods comprise:

  • KeyPress(): Called on the event where you want to press a key.
  • KeyRelease(): Called in the event to release the pressed key. 
  • MouseMove(): Called in the event when you have to move the mouse pointer in the X and Y coordinates.
  • MousePress(): Called in the event when you press the left button of the mouse.
  • MouseMove(): Called in the event of releasing the pressed button of the mouse.

Q.19) How to handle many windows in Selenium?

The window handle is a special identifier that has the address of all the windows. It serves as a pointer to a window returning the value in the string. 

  • get.windowhandle(): It gets the current window handle. 
  • get.windowhandles(): Gets the handles of all the windows opened.
  • switch to: Helps in switching across the windows.
  • set: Sets the window handles, which is in the form of a string.
  • action: helps to execute certain actions on the windows.

Q.20) What are Listeners?

The interface that changes the behavior of the system is called listeners in Selenium. They enable customizations of logs and reports. They are of two kinds: TestNG listeners and Webdriver listeners. 

Q.21) Explain Assert and Verify commands

Assert: An assertion is used to differentiate between the real result and the expected result. 

Verify: The test executions aren’t paused no matter if the verify condition is true or false. 

Q.22) How does one navigate back and forth on a webpage?

It is one of the most common selenium interview questions.

You can use the below methods to navigate back and forth.

driver.navigate.forward

driver.manage.navigate

driver.manage.back

driver.navigate.to(“url”)

Q.23) How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

Typically using the keys like ALT, Shift, or Control, we combine them with other keys to activate a function. We cannot just click them alone. We need to define two methods for the purpose of holding onto these keys while the following keys are 

pressed: keyUp(modifier_key) and keyDown(modifier_key) 

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)

Objective: The purpose is it performs a modifier keypress without releasing the modifier key. Following interactions may assume it’s kept pressed.

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)

Objective: The purpose is it performs a key release.

So, with a mix of these two methods, we can capture the special function of a particular key.

Q.24) How do we take screenshots in Selenium WebDriver?

The TakeScreenshot function helps to take a screenshot in Selenium Webdriver. Further, you can save the screenshot taking by using getScreenshotAs() method.

File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);

Q. 25) Can we set the size of the browser window using Selenium? If yes, how? 

Yes. If you wish to maximize the size of browser window, you need to use the code 

driver.manage().window().maximize();

If you wish to resize the current window to a specific dimension, you must use the setSize() method. For instance: 

System.out.println(driver.manage().window().getSize());

Dimension d = new Dimension(420,600);

driver.manage().window().setSize(d);

If you wish to set the window to a particular size, you must use window.resizeTo() method. 

For instance:

((JavascriptExecutor)driver).executeScript(“window.resizeTo(1024, 768);”);

Q.26) How to select a value from the dropdown? How to handle a dropdown?

You most likely will be asked about a question about dropdown and selection of values as it is a little tricky and technical as well. 

The most crucial detail you must know is that to work with a dropdown in Selenium, it is important to use of the html tag: ‘select’. You cannot handle dropdowns without using the select tag. Have a look at the code below:

<select id=”mySelect”>

<option value=”option1″>Cakes</option>

<option value=”option2″>Chocolates</option>

<option value=”option3″> Candies</option>

</select>

In the above code, an HTML ‘select’ tag is used to define a dropdown element. The ID of the select tag here is myselect. We have given three options in the dropdown: Cakes, Chocolates, and Candies. You can see that each of these choices has an attached value attribute. For instance, for Cake, the value is Option1, for Chocolates its Option2, and for Candies, it is Option3. 

To choose a value, you need to:

Identify the ‘select’ html element by using the findelement()

Example:

WebElement mySelectElement = driver.findElement(By.id(“mySelect”));

Select dropdown = new Select(mySelectElement);

Pick up an option from that dropdown element.

To pick an option from that dropdown, there are three ways:

dropdown.selectByVisibleText(“Chocolates”); → Choosing an option by the text that is seen.

dropdown.selectByIndex(“1”); → Choosing an option using the Index number of that option.

dropdown.selectByValue(“option2”); → Choosing an option using the value of that option.

Note that in all the cases, the option “Chocolates” is selected from the dropdown. Points 1 and 3 are obvious and point two; we say “1” because the indexing starts from zero.

Read: Top Nagios Interview Questions & Answers

Q.27) How do you hop to a new tab which opens up after you click on a link?

On clicking a link on a web page, you need to use the switchTo() command to change the focus of the Webdriver. Example: driver.switchTo().window();

where ‘windowName’ is the name of the window, you want to switch your focus to.

If you do not have the name of the window, you can use the driver.getWindowHandle() command to fetch the name of all the windows that were initiated by the WebDriver. Remember, it will never give you the names of those windows which Webdriver did not initiate. 

On getting the name, you need to run through a loop to get to that window. Here is an example:

String handle= driver.getWindowHandle();

for (String handle : driver.getWindowHandles()) 

{

driver.switchTo().window(handle);

}

Q.28) How can one upload a file in Selenium WebDriver?

The command element.send_keys(file path) is used to upload a file in Selenium Webdriver. But before you that, you must use the html tag: ‘input’ where the attribute type should be ‘file’. Here is an example to understand it better:

<input type=”file” name=”my_uploaded_file” size=”50″ class=”pole_plik”>

element = driver.find_element_by_id(”my_uploaded_file”)

element.send_keys(“C:myfile.txt”)

Q.29) What is the importance of testng.xml?

If you are interviewing for Selenium, you surely know the importance of testing. Selenium does not support the generation of the report as well as test case management. We, therefore, use the TestNG framework with Selenium. It is way advanced compared to Junit, and it is easier to implement annotations making TestNG framework the choice with Selenium Webdriver. 

You can define the test suites and grouping of test classes in TestNG, by taking commands from the testing.xml file. It is represented in an XML file and not in a test suite within the testing source code because the suite is a feature of execution. A test suite is a group or collection of test cases.

The testng.xml file should contain the name of all the methods and classes that you wish to execute as a portion of that execution flow.

Some of the advantages of using testng.xml file are:

  • It lets the execution of multiple test cases from multiple classes
  • It lets the execution of test cases in groups, where a single test can belong to multiple groups.
  • It lets parallel execution.

Q. 30) Explain DataProviders in TestNG. Is it possible to call a single data provider method for multiple functions and classes?

One of the advanced selenium interview questions. DataProvider is a feature of TestNG, enabling developers to write DataDriven tests. It supports DataDriven testing, meaning that the same test method can run multiple times with different data-sets. DataProvider is just a method of passing parameters to the test method.

@DataProvider is a method for providing data for a test method. The annotated method must give back an Object[] where each Object[] can be allocated to the parameter list of the test method.

Yes. It possible to call a single data provider method for multiple functions and classes. The same DataProvider can be used in several functions and classes by declaring DataProvider in a separate class and then using it again in multiple classes.

Q. 31) What are the features of TestNG?

  • Before and after annotations
  • XML based test configuration
  • Multithreaded execution
  • Open API
  • Better reporting
  • Data-Driven testing
  • Dependent Groups.
  • Dependent methods

Conclusion

Coming to the end of the Selenium interview questions and answers guide, we hope this helps you get your job. Selenium interview questions for experienced are more technical, but with fierce competition over jobs, it is always great to be prepared well for an interview.

If you are interested in learning Selenium, DevOps and everything about Full Stack development, check out IIIT-B & upGrad’s PG Diploma in Full Stack Software Development Program.

Prepare for a Career of the Future

INDUSTRY TRUSTED LEARNING - INDUSTRY-RECOGNIZED CERTIFICATION.
Enroll Now @ upGrad

Leave a comment

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

×
Download EBook
Download EBook
By clicking Download EBook, you agree to our terms and conditions and our privacy policy.