Top 40 Selenium WebDriver Interview Questions & Answers

Let’s learn the Top Selenium Interview questions.

 

1. What is Selenium?

Selenium is a browser-based functional test automation tool. Selenium is an API or set of java files, which you can use in your program to test a web application. It is important to note that selenium is used for Web Automation only, You can’t automate the Desktop application using Selenium WebDriver.

Selenium Webdriver has many language bindings, which means you can write your tests in your favorite programming language and using the respective selenium bindings. For detailed answer visit – What is Selenium & uses of Selenium ?

 

2. How many languages does selenium WebDriver support?

The main ones are: Java, C#, PHP, Ruby, Python, Perl

 

3. What is the set of tools available with Selenium?

Selenium has four set of tools – Selenium IDE, Selenium 1.0 (Selenium RC), Selenium 2.0 (WebDriver) and Selenium Grid.

 

4. Why should Selenium be selected as a test tool?

Selenium

  1. is free and open source
  2. have a large user base and helping communities
  3. have cross Browser compatibility (Firefox, chrome, Internet Explorer, Safari etc.)
  4. have great platform compatibility (Windows, Mac OS, Linux etc.)
  5. supports multiple programming languages (Java, C#, Ruby, Python, Pearl etc.)
  6. has fresh and regular repository developments
  7. supports distributed testing

 

5. What are the limitations of Selenium?

Following are the limitations of Selenium:

  • Selenium supports testing of only web based applications
  • Captcha and Barcode readers cannot be tested using Selenium
  • Reports can only be generated using third party tools like TestNG or JUnit.
  • As Selenium is a free tool, thus there is no ready vendor support though the user can find numerous helping communities.
  • The user is expected to possess prior programming language knowledge.

 

6. How do you connect to Database from selenium?

Connecting to the database is language dependent. In the below example, we assume that Java is being used.

A Connection object represents a connection with a database. When we connect to a database by using connection method, we create a Connection Object, which represents the connection to the database. An application may have one or more than one connections with a single database or many connections with different databases.

We can use the Connection object for the following things:

1). It creates the Statement, PreparedStatement and CallableStatement objects for executing the SQL statements.

2). It helps us to Commit or roll back a JDBC transaction.

3). If you want to know about the database or data source to which you are connected then the Connection object gathers information about the database or data source by the use of DatabaseMetaData.

4). It helps us to close the data source. The Connection.isClosed() method returns true only if the Connection.close() has been called. This method is used to close all the connection.

Firstly we need to establish the connection with the database. This is done by using the method DriverManager.getConnection(). This method takes a string containing a URL. The DriverManager class attempts to locate a driver that can connect to the database represented by the string URL. Whenever the getConnection() method is called the DriverManager class checks the list of all registered Driver classes that can connect to the database specified in the URL.

For programming implementation, you can see DataBase Testing Using Selenium WebDriver Tutorial.

 

7. What is the difference between single and double slash in XPath?

If XPath starts selection from the document node, it’ll allow you to create ‘absolute’ path expressions.

e.g. “/html/body/p” matches all the paragraph elements

If XPath starts selection matching anywhere in the document, then it’ll allow you to create ‘relative’ path expressions.

e.g. “//p” matches all the paragraph elements

For Details visit Absolute and Relative XPaths in Selenium.

 

8. How do you verify the presence of an element after the successful page loading?

You can verify the presence of elements on the web page using an isDisplayed() method of Selenium API. For detailed answer visit – Verify presence of Page Elements in Selenium

 

9. What is XPath? When would I have to use XPath in Selenium?

XPath is a way to navigate in the xml document and this can be used to identify elements in a web page. You may have to use XPath when there is no name/id associated with the element on the page or only partial part of name/id is constant.

See How to create XPath in Selenium , How to Create Dynamic XPath in Selenium

Direct child is denoted with – /

Relative child is denoted with – //

Id, class, names can also be used with XPath –

  • //input[@name=’q’]
  • //input[@id=’lst-ib’]
  • //input[@class=’ lst’]

If only part of id/name/class is constant than “contains” can be used as –

  • //input[contains(@id,’lst-ib’)]

 

10. What is CSS Selector in Selenium?

In selenium you can use CSS Selectors to find the element on WebPage. You can see How to create CSS Selector and Verify CSS Selector in Details in given article.

 

11.  What are the advantages of Selenium Web driver over Selenium RC?

Selenium RC’s architecture is quite complicated while WebDriver’s architecture is simpler than Selenium RC’s.

Though Selenium RC is slower since it uses an additional JavaScript program called Selenium Core. On the contrary, WebDriver is faster than Selenium RC since it speaks directly to the browser and uses browser’s own engine to control it.

Selenium Core, just like other JavaScript codes, can access disabled elements. Web Driver interacts with page elements in a more realistic way.

Selenium RC’s API set is already evolved but contains redundancies and often confusing commands. WebDriver APIs are simpler and do not contain any redundant or confusing commands.

Selenium RC cannot support the headless HtmlUnit browser. It needs a real, visible browser to operate on. Web Driver can support the headless HtmlUnit browser.

Selenium RC has built-in test result generator and it automatically generates an HTML file of test results. Web Driver has no built-in command that automatically generates a Test Results File. For detailed Answer visit Selenium WebDriver Architecture

 

12. State any difference between “GET” and “NAVIGATE”?

Get method will get a page to load or get page source or get the text that’s all. Whereas navigate will guide through the history like refresh, back, forward. For example, if we want to move forward and do some functionality and back to the home page. This can be achieved through navigate() method only. driver.get() will wait till the whole page gets loaded and driver.navigate() will just redirect to that page and will not wait.

 

13. How is the implicit Wait different from Explicit wait?

Implicit Wait sets internally a timeout that will be used for all consecutive Web Element searches. It will try lookup the element again and again for the specified amount of time before throwing a NoSuchElementException if the element could not have been found. It does only this and can’t be forced into anything else – it waits for elements to show up.

Explicit Wait or just Wait is a one-timer used by you for a particular search. It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt Expected Conditions to wait for elements to become clickable, visible, invisible, etc., or just write your own condition that suits your needs.

For Detailed Answer visit Waits in Selenium

 

14. How to Handle Alerts/Popups in Selenium WebDriver?

There are two types of alerts which are commonly referred–

  • Windows based alert pop ups
  • Web based alert pop ups

Web based alert pop ups.

  1. WebDriver offers the users with a very efficient way to handle these pop ups using Alert interface.
  2. void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
  3. void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.
  4. String getText() – The getText() method returns the text displayed on the alert box.
  5. void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.

Windows based alert pop ups.

Handling window based pop-ups have always been a little tricky as we know Selenium is an automation testing tool which supports only web application testing, that means, it doesn’t support windows based applications and window alert is one of them.

  1. Robot class is a java based utility which emulates the keyboard and mouse actions and can be effectively used to handling window based pop up with the help of keyboard events.
  2. The keyPress and keyRelease methods simulate the user pressing and releasing a certain key on the keyboard respectively.

For Detailed Answer and Code Implementation visit Handle Alerts in Selenium

 

15. How to take a screenshot with Selenium WebDriver?

There is predefined way in Selenium to Take Screen Shot. You can find the Detailed answer here- Take ScreenShot in Selenium WebDriver

 

16. What are the available locators in Selenium WebDriver?

  1. ID,
  2. Name,
  3. CSS,
  4. XPath,
  5. Classname,
  6. TagName,
  7. LinkText, and
  8. Partial Link Text.

 

17. How to handle AJAX controls in WebDriver?

AJAX stands for Asynchronous JavaScript and XML. It does not rely on the extra overhead of opening and closing tags that is needed to create valid XML. Most of the time WebDriver automatically handle the Ajax controls and calls/ Incase if it is not able to handle, you can find the detailed answer here – Handle Ajax Calls in Selenium WebDriver

 

18. How to Mouse over a submenu item of header menu?

With the actions object you should first move the menu title, and then move to the popup menu item and click it. To manage these kind of mouse events we need to use Action Class in Selenium, you can find the Detailed Answer here – Handle Mouse Hover in Selenium WebDriver

 

19. How to type in a textbox using Selenium?

User can use sendKeys(“String to be entered”) to enter the string in the textbox.

Syntax:
WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“seleniumtutorial”);

 

20. How can we get a text of a web element?

Get command is used to retrieve the inner text of the specified web element. The command doesn’t require any parameter but returns a string value. It is also one of the extensively used commands for verification of messages, labels, errors etc displayed on the web pages.

Syntax:
String Text = driver.findElement(By.id(“Text”)).getText();

 

21. How to select the value in a dropdown?

The value in the drop down can be selected using WebDriver’s Select class. For detailed answer and code implementation you can visit – Handle DropDown in Selenium WebDriver

 

22. What are the different types of navigation commands?

You can navigate the browser through the Selenium WebDriver. For Detailed implementation, you can visit – How to Navigate Browser using Selenium WebDriver

 

23. How to handle frame in WebDriver?

An inline frame acronym as iframe is used to insert another document with in the current HTML document or simply a web page into a web page by enabling nesting.

Select iframe by id
driver.switchTo().frame(ID of the frame);

Locating iframe using tagName
driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));

Locating iframe using index

frame(index)
driver.switchTo().frame(0);

frame(Name of Frame)
driver.switchTo().frame(“name of the frame”);

frame(WebElement element)
Select Parent Window
driver.switchTo().defaultContent();

 

24. When do we use findElement() and findElements()?

findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.

Syntax:

WebElement element =driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));
findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.

Syntax:
List <WebElement> elementList =driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));

 

25. What is the difference between driver.close() and driver.quit command?

close(): WebDriver’s close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver. The command neither requires any parameter nor does is return any value.

quit(): Unlike close() method, quit() method closes down all the windows that the program has opened. Same as close() method, the command neither requires any parameter nor does is return any value.

 

26. How to retrieve css properties of an element?

The values of the css properties can be retrieved using a get() method:

Syntax:
driver.findElement(By.id(“id“)).getCssValue(“name of css attribute”);
driver.findElement(By.id(“id“)).getCssValue(“font-size”);

 

27. What are JUnit annotations?

You can find the detailed answer in JUnit Annotations Tutorial .

 

28. What is TestNG and how is it better than Junit?

There are various advantages that make TestNG superior to JUnit. You can find the details at Advantage of TestNG over Junit .

Advantages of TestNG over Junit.

  1. In Junit we have to declare @BeforeClass and @AfterClass. It is a constraint Junit where as in TestNG there is no constraint like this.
  2. Additional Levels of setUp/tearDown level are available in TestNG.
    1. @ Before/AfterSuite
    2. @Before/AfterTest and
    3. @Before/AfterGroup
  3. There is no need to extend any class in TestNG.
  4. There is no method name constraint in TestNG as in Junit.
  5. In TestNG we can tell the test that one method is dependent on another method whereas in Junit this is not possible.
  6. Grouping of test cases is available in TestNG whereas the same is not available in Junit. Execution can be done based on Groups. For example, if you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. And if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity”. TestNG will automatically execute the cases belonging to the “Sanity” group.
  7. Also TestNG supports parallel test case execution.

 

29How to set test case priority in TestNG?

We can set the Test Case Priority in TestNG. For Detailed answer visit How to Set Test Case Execution Priority in TestNG .

 

30. What is a framework?

Framework is a constructive blend of various guidelines, coding standards, concepts, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections etc. to pillar automation testing.

 

31. Can you broadly classify TDD, BDD and DDD frameworks and What’s the Difference?

You would have heard of all these acronyms buzzing all around. Here I’ll briefly explain them and tell how exactly they will help in the system test life cycle.

TDD – Test Driven Development.

It’s also called test-driven design, is a method of software development in which unit testing is repeatedly done on source code. Write your tests watch it fails and then refactor it. The concept is we write these tests to check if the code we wrote works fine. After each test, refactoring is done and then the same or a similar test is performed again. The process is iterated as many times as necessary until each unit is functionally working as expected. TDD was introduced first by XP. I believe I have explained enough in simple terms.

BDD – Behavior Driven Development.

Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design. Its purpose is to help the folks devising the system (i.e. the developer) identify appropriate tests to write–that is, tests that reflect the behavior desired by the stakeholders.

DDD-Domain Driven Development.

DDD is about mapping business domain concepts into software artifacts. A DDD framework offers following benefits:

  • Helps the team to create a common model, between the business and IT stakeholders
  • The model is modular, extensible and easy to maintain as the design reflects the business model.
  • It improves the re-usability and testability of the business domain objects.

 

32. What is the difference between POI and jxl jar?

# JXL jar POI jar
1 JXL supports “.xls” format i.e. binary based format. JXL doesn’t support Excel 2007 and “.xlsx” format i.e. XML based format POI jar supports all of these formats
2 JXL API was last updated in the year 2009 POI is regularly updated and released
3 The JXL documentation is not as comprehensive as that of POI POI has a well prepared and highly comprehensive documentation
4 JXL API doesn’t support rich text formatting POI API supports rich text formatting
5 JXL API is faster than POI API POI API is slower than JXL API

 

33. What is Object Repository? How can we create Object Repository in Selenium?

Object Repository is a term used to refer to the collection of web elements belonging to Application Under Test (AUT) along with their locator values. Thus, whenever the element is required within the script, the locator value can be populated from the Object Repository. Object Repository is used to store locators in a centralized location instead of hard coding them within the scripts.

In Selenium, objects can be stored in an excel sheet or properties files , which can be populated inside the script whenever required.

 

34. What is Data driven framework & Keyword Driven?

Data driven framework.

In this framework test case logic resides in test Scripts. Test Data is separated and kept outside the Test Scripts. Test Data is read from the external files (Excel File) and are loaded into the variables inside the Test Script. Variables are used both for Input values and for Verification values.

Keyword Driven.

The keyword/table driven framework requires the development of data tables and keywords. They are independent of the test automation tool used to execute them. Tests can be designed with or without the Application. In a keyword-driven test, the functionality of the Application-under-test is documented in a table as well as in step-by-step instructions for each test.

 

35. Can we run a group of test cases using TestNG?

Test cases in group using TestNG will be executed with the below options.

If you want to execute the test cases based on one of the group like regression test or smoke test-

@Test(groups = {“regressiontest”, “smoketest”})

For detailed implementation and better understanding visit – How to Group the TestCases in TestNG

 

36. Which web driver implementation is the fastest?

HTMLUnitDriver. Simple reason is HTMLUnitDriver does not execute tests on the browser but use plain HTTP request-response. It is far quicker than launching a browser and executing tests. For HTMLUnit Driver implementation visit – How to execute Selenium Tests using HTMLUnit Browser

 

37. What is the difference between Assert and Verify?

Assert- it is used to verify the result. If the test case fail then it will stop the execution of the test case there itself and move the control to other test case.

Verify- it is also used to verify the result. If the test case fail then it will not stop the execution of that test case.

For more Detailed Answer visit Assertion in Selenium

 

38. Are there any limitations while injecting capabilities in WebDriver to perform tests on a browser which is not supported by WebDriver?

Major limitation of injecting Capabilities is that “findElement” command may not work as expected. This is because WebDriver uses Selenium Core to make “Capability injection” work which is limited by java script security policies.

 

39. Is it possible to interact with hidden elements using WebDriver?

Since WebDriver tries to exercise browser as closely as real users would, hence simple answer is No, But you can use java script execution capabilities to interact with hidden elements. But beware, this is not how an user interacts with application.

 

40. Can WebDriver handle UntrustedSSLCertificates?

Yes you can handle the SSL Certificate using Selenium. For better and detailed understanding visit – How to handle Untrusted SSL Certificates in Selenium .

 

That’s all, Thank you readers. Stay visit and keep sharing .. 🙂

 

3 Comments
  1. K Sandhya 8:44 AM / September 2, 2016 - Reply

    Wonderful and Informative article …. Thanks for sharing …

  2. Mark Sim 5:09 PM / September 6, 2016 - Reply

    Nice explanation …. Put some data on Grid as well … Rest article is good

  3. Alrian Mirea 11:22 AM / October 14, 2016 - Reply

    Thanks …. Nice Article

Leave a Reply