Selenium Interview Questions: Freshers and Experienced

Selenium interview questions constitute a major part of any QA and testing interview.  The reason being, Selenium is the most popular automation tool of current times. For a better understanding of our readers, we have divided Selenium interview questions and answers into the sections as below- 

  1. Selenium interview questions for freshers 
  2. Selenium interview questions for experienced 

Selenium Interview Questions For Freshers 

What is Selenium? 

    • Selenium is an open-source suite consisting of multiple components. It is not a single automation tool, it is a group of 4 components. 
    • It is used for automation testing of web applications. 
    • It supports functional testing and regression testing. 
    • Selenium supports automation testing across these- 
      • Browsers– Internet Explorer, Safari, Chrome, Firefox, Opera 
      • Operating System – Windows, Linux, Unix, Mac
      • Programming languages– Java, Perl, Ruby, Python, C#, JavaScript, PHP


What are the different components I should know while preparing Selenium Interview Questions? 

  • Selenium Integrated Development Environment(IDE): This automation test tool records user actions in the form of a script and then playbacks it. This comes as a plugin for Firefox and Chrome browsers. Test scripts can be exported from Selenium IDE to Selenium webdriver in the required programming language. Moreover, automation with Selenium IDE does not require programming skills because IDE records user actions and then creates scripts automatically. 
  • Selenium Remote Control(RC): Before running test scripts, the Selenium RC server needs to be started. This server sits between the browser and the test scripts. Then, Selenium RC server interacts with the browser after converting the programming language into JavaScript commands. This extra layer of JavaScript commands makes Selenium RC slower than Selenium Webdriver. Since the webdriver directly communicates with the browser using HTTP calls, it is faster than the Selenium RC. 
  • Selenium Grid: Selenium Grid is used for parallel execution of test cases across different multiple browsers and machines. Therefore, helps in saving test execution time. 
  • Selenium Webdriver: Selenium webdriver is the most popular automation tool due to its simple architecture and the ability to directly communicate with browsers using HTTP calls.  

Different components of Selenium asked during a Selenium interview

What are the advantages of Selenium as a tool? 

  • Open source- Selenium is free and open source, therefore this is the first choice for most organisations for automation testing. 
  • Multiple supported programming languages-  Selenium supports Java, JavaScript, PHP, Ruby, Python, C#, Perl, etc.  
  • Multi-platform support- Windows, Mac, Linux, and Unix are supported by Selenium. 
  • Cross-browser testing- Supports multiple browser testing  e.g. Chrome, Safari, Firefox, Internet Explorer, etc.
  • Online community support- Since it has widespread usage there is a huge Selenium user base that provides support to the new users. 
  • Parallel execution support-  Selenium grid supports parallel execution, distributed across different machines. This is a favourable feature of Selenium for testers. 


What are the disadvantages of Selenium? 

  • It supports the testing of web-based applications only. 
  • Testers should possess good programming language knowledge to write test scripts. 
  • It does not support mobile application testing. 
  • The in-built report generation tool is unavailable. 
  • Captcha testing is not possible using Selenium. 
  • It is an open-source automation tool, therefore official vendor support is unavailable for Selenium.  


Explain the Selenium Webdriver architecture? 

Selenium webdriver architecture to understand it betterSelenium Webdriver architecture consists of the below components- 

  • Selenium client libraries– Selenium supports  multiple programming languages (e.g. Python, C#, Java, JavaScript, Perl, PHP, and Ruby) due to the various language bindings available in the Selenium client libraries. Therefore,  users can write test scripts in any of the supported programming languages. 
  • JSON wire protocol– JavaScript Object Notation(JSON) wire protocol transfers data between client and server using the HTTP calls. The test scripts are converted into JSON and sent to browser drivers via HTTP protocol.
  • Browser drivers– Every browser has its own specific driver for Selenium. The browser driver receives JSON commands and runs them on the browser. The responses are sent back via HTTP.
  • Browsers- Supported browsers that run the commands received from browser drivers on the Application Under Test (AUT). 


What are the different  webdriver exceptions in Selenium? 

Few common webdriver exceptions in Selenium 

  • NoSuchElementException– When the web element  provided in the test script is not found on the webpage.
  • TimeoutException– When an operation does not get completed in the defined time.
  • WebDriverException– When the webdriver is not initialised.
  • ElementNotVisibleException– When the element is not visible (property is set as hidden), however, is present on the web page. 
  • ElementNotSelectableException– When a web element is present on a webpage but is disabled, so cannot be selected.
  • StaleElementException– When the web element is deleted from the webpage or its Document Object Model (DOM) has changed. 


What is  Webdriver wait in Selenium? 

There are 3 types of wait – 

  • Implicit webdriver wait – Implicit wait in Selenium remains active during the entire period of a particular webdriver instance. Implicit wait provides a default wait time (e.g. 10 seconds) to the WebdriverWebdriver will wait for 10 seconds for the web element’s visibility. If the web element is not found even after 10 seconds then the Selenium webdriver will throw NoSuchElementException. 
  • Explicit webdriver wait – Explicit wait remains active for a single web element  search only. This search is based on the web element’s visibility related conditions. Webdriver waits until the mentioned condition is met, if the condition is not met then webdriver throws ElementNotVisibleException.
    • Few of the search conditions can be: 
      • elementToBeClickable() 
      • elementToBeSelected() 
      • alertIsPresent()  
      • textToBePresentInElement() 
      • invisibilityOfTheElementLocated() 
      • titleIs() 
      • titleContains() 
  • Fluent webdriver wait – Selenium Webdriver waits for a maximum wait time based on the search condition as in the case of explicit wait. Together with this, it checks the web element periodically, based on the  frequency of periodic check. We can define this periodic frequency in the script. If it does not find the element after the maximum wait time then it throws ElementNotFoundException.  


What are the different locators in Selenium? 

Locators help us to uniquely locate the web elements on the webpage. Below are the different types of locators that can be frequently asked as a Selenium interview question 

  • ID Name 
  • className 
  • tagName 
  • Xpath 
  • cssSelector 
  • linkText 
  • partialLinkText 


How to create test automation architecture using Selenium. 

Test automation architecture using Selenium
Image from www.logigear.com

A test automation architecture consists of the following components- 

  • Application Under Test(AUT) 
  • Selenium Webdriver 
  • Individual browser drivers   
  • Browsers- The real browsers 
  • Element wrappers- To identify and interact with web elements separately 
  • Page Object Model- consists of the web pages as page objects and methods for interactions. 
  • Data-Driven testing files- For data-driven testing data files and read-write code 
  • Driver manager- This is an abstract class to create, destroy, and manage browser driver instances and webdriver instances. 
  • Selenium Core- consists of the driver manager and element wrapper class. 
  • Selenium Test- consists of the actual Selenium test cases. 
  • Dependency,  build, and test management – Maven by POM.xml 
  • TestNG- For unit testing, parallel runs, grouping, data-driven testing, parameterisation, prioritisation, etc. 
  • Reporting- TestNG reports are used by default. 


Selenium Interview Questions F
or Experienced 

Explain the difference between assert and verify in Selenium? 

Assert- When a test condition is not met, the test execution stops and marks that test case as failed. The test execution aborts and all the subsequent test cases remain unexecuted. 

Verify– When a test condition is not met, the test execution continues however, it marks that particular test case as failed. The subsequent test cases are executed.

How to handle web element dropdown in Selenium Webdriver? 

We can interact with the dropdown in Selenium Webdriver through the ‘Select’ class of Selenium. Selenium webdriver provides three methods(functions) in the Select class. Using these methods  we can select the value from the dropdown. 

  • selectByIndex: Selects option from the dropdown using its index- e.g. 1, 2,3 etc. 
  • selectByValue: Selects option from the dropdown using its value. 
  • selectByVisibleText: Selects option from the dropdown using the visible text of the option in the dropdown.
     

What do you understand by  Page Object Model(POM) in Selenium and what are its advantages? 

Page Object Model is a design form used for creating a test automation framework. 

Page Object Model architecture has Page Object Classes(web elements + methods) and separate test methods.  

Page Object Model Architecture in Selenium
Image from guru99.com
  • We create a Page Object class(e.g. ClassA in diagram) for every web page. 
  • Page Object Class’s variables represent the web elements of the web page(WebElement in diagram).  
  • Page Object Class’s methods(Method in the diagram) help to interact with the web elements of the web page. 
  • Hence finally we have a whole Object Repository consisting of all Page Objects. 
  • We keep the Test Methods(e.g. ClassB in the diagram) separate from the Page Object class. 

Advantages of POM: 

Reusability– The Page Object repository has different Page Objects. The same Page Object can be used with multiple third-party tools like testNG, Cucumber etc. 

Code optimization – Reusable test methods reduce the lines of code and this leads to code optimisation. 

Maintainability – Separate Page Objects, page methods,  test methods, increases the   simplicity of code. In case of any modification, the maintenance becomes easier. 

Readability – Cleaner code increases the readability of the code. 


What is Selenium actions class
? Explain methods of actions class in Selenium. 

Mouse and keyboard actions on the Application Under Test(AUT), are handled through actions class in Selenium. Drag and drop, double click, key up, Ctrl keys, shift key all these events are handled through actions class. 

 Mouse events methods- 

  • click()– Click where mouse is pointing 
  • doubleClick()– Double click where mouse is pointing  
  • clickAndHold()– Click at mouse location and hold without releasing 
  • contextClick()– Context click at mouse position 
  • dragAndDrop(source, target)– drags from source location and drops at target location 
  • dragAndDropBy(source, x offset, y offset)– drags from source location and drops at location provided by offset 
  • moveToElement(toElement)– moves mouse cursor to the element 
  • release()– releases mouse pointer 

 Keyboard events methods- 

  • keyUp(modifier key)– performs key press 
  • keyDown(modifier key)– performs key release 
  • sendKeys(onElement, keys)– sends keys to the element 


How to handle file upload  in Selenium? 

We can use three methods to perform file upload in Selenium-

  • sendKeys()- We use the sendKeys() method of keyboard events to upload the file easily. First, we identify the web element where the user should provide the file path, which is mostly a text input. Then through sendKeys() we provide the path and  click on the ‘upload’ button. You can find an example here. 
  • AutoIT tool- AutoIT is a third-party tool, which helps to automate the process of file upload. We need to create a .exe file that accepts the argument as ‘upload filename’. This script can be run on Windows only. You can find the contents of the AutoIT script here.
  • Robot class – It is used to automate alerts and pop-ups in Windows. An object of an instance of Robot class is created and with the help of keyboard events, the file is uploaded. You can find an example here. 


How to read and write a file in Selenium Webdriver? 

The inclusion of data-driven testing is very important for a test automation framework. In order to read and write on Microsoft Excel sheets during test automation, we use a third-party Java library called Apache POI – Poor Obfuscation Implementation. And this  library helps in reading and writing the .xls and .xlsx files through its methods. 

Apache POI classes and Interface to understand Selenium as an automation tool
Image from guru99.com 

Interfaces in POI- 

  • Workbook 
  • Sheet 
  • Row 
  • Cell 

Classes for implementation of xls files- 

  • HSSFWorkbook 
  • HSSFSheet 
  • HSSFRow 
  • HSSFCell 

Classes for implementation of xlsx files- 

  • XSSFWorkbook 
  • XSSFSheet 
  • XSSRow 
  • XSSFCell 

 Code implementation to read and write excel file here. 


How to handle pop-up windows and alerts in Selenium? 

While testing an application we may receive multiple alerts and popups. We may receive two types of alerts- 

  1. Window-based– Called as dialog-box, this is an alert in Selenium. Below are the different types of alert in Selenium. 
    • Simple alert- has an only OK button
    • Prompt alert- has some text input, OK, and Cancel button. 
    • Confirmation alert- has OK and Cancel for confirmation from the user 
  2. JavaScript or web-based– Called as a pop-up which is dependent on an application. 

Methods to handle alert in Selenium 

  • dismiss()- to click cancel button 
  • accept()- to click ok button 
  • sendKeys()- to send data keys 
  • getText()- to get the message from alert 
  • switchTo().alert()- to change the control from screen to alert 

Methods to handle pop-up window 

  • getWindowHandles()- used to switch between all the windows present on screen 
  • getWindowHandle()- used to handle the current  window on the screen 


How to capture a 
screenshot in Selenium Webdriver? 

  • Typecast webdriver instance into TakesScreenshot. 
  • Call getScreenshotAS() which is a method of TakesScreenshot. 
  • Copy the  file to the source path, using the copyFile method. 

 Code snippets on how to take screenshots in Selenium are here. 

Related Articles

Responses