• +91 9723535972
  • info@interviewmaterial.com

Selenium Interview Questions and Answers

Selenium Interview Questions and Answers

Question - 101 : - State the major difference between “assert” and “verify” commands in Selenium.

Answer - 101 : -

Both “assert” and “verify” commands check whether the given condition is true or false and the only difference between them is that:

  • Assert: Assert condition stops the execution of the testing if the given condition is false else would continue with the further tests.
  • Verify: Verify the condition doesn’t stop the flow of execution irrespective of the condition being true or false.

Question - 102 : - What is meant by an exception test in Selenium?

Answer - 102 : -

An exception test is a test that expects an exception to be thrown inside a test class. It expects a @Test annotation followed by the expected exception name in the brackets.

Eg: @Test(expectedException = NoSuchElementException.class) is an exception test for missing elements in Selenium.

Question - 103 : - What is meant by XPath in Selenium. Explain XPath Absolute and XPath Relative.

Answer - 103 : -

XPath, also defined as XML-Path (Extensible Markup Language Path), is a language used to query XML documents and provide functionalities like locating elements in Selenium by iterating through each element in a webpage. In XPath, data is stored in a key-value pair format similar to an HTML tag. It uses a single slash, i.e. ‘ / ’ for creating an absolute path, and a double slash, i.e. ‘ // ’ for creating a relative path for an element to be located on a webpage.

Question - 104 : - In Xpath, what is the difference between "/" and "//"?

Answer - 104 : -

Single Slash "/" - A single slash is used to create an Xpath with an absolute path, i.e. the XPath will begin with the document node/start node.

For example, 

Absolute XPath: /html/body/div/div/form/input
Here, /html is the root html node.

Double Slash "//" - The double slash is used to construct an Xpath with a relative path, which means the XPath can start selection from anywhere on the page.

For example,

Relative XPath: //input[@id = 'email']
Here, we can locate an input having id = ‘email’ present anywhere in the document object model (DOM).

Question - 105 : - In Selenium, how will you wait until a web page has been loaded completely?

Answer - 105 : -

There are two methods of making sure that the web page has been loaded completely in Selenium. 

They are as follows :

1. Immediately after creating the webdriver instance, set an implicit wait: 

temp = driver.Manage().Timeouts().ImplicitWait;
On every page navigation or reload, this will try to wait until the page is fully loaded.

2. Call JavaScript return document.readyState till "complete" is returned after page navigation. As a JavaScript executor, the web driver instance can be used. 

Code example:

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
     webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

Question - 106 : - Explain the various navigation commands supported by Selenium?

Answer - 106 : -

Selenium has the support of majorly 4 navigation commands:

navigate().back(): This command is used for taking the user to the last webpage of the browser history.
navigate().forward(): This command is used for taking the user to the next web page of the browser history.
navigate().refresh(): This command is used for reloading the web components of a webpage by refreshing it.
navigate().to(): This command is used for navigating to a particular URL in a new web browser. It takes the URL to be migrated to, as a parameter.

Question - 107 : - Explain the difference between findElement() and findElements() in Selenium.

Answer - 107 : -

Following is the major difference between the two commands:

1.findElement(): command is used for finding a particular element on a web page, it is used to return an object of the first found element by the locator. Eg:

 WebElement element = driver.findElement(By.id(example));
2. findElements(): command is used for finding all the elements in a web page specified by the locator value. The return type of this command is the list of all the matching web elements. Eg: 

List elementList = driver.findElements(By.id(example));

Question - 108 : - With the help of code snippets, explain how we can create right-click and mouse hover actions in Selenium.

Answer - 108 : -

The following code can replicate right-click action:

actions action = newActions(driver);
WebElement element = driver.findElement(By.id("elementId")); 
action.contextClick(element).perform();
The following code can replicate mouse hover action:

actions action = newActions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.moveToElement(element).perform();

Question - 109 : - Explain different types of framework and connection of Selenium with Robot Framework.

Answer - 109 : -

Following are the different types of frameworks:

  • Behavior-Driven Development Framework: This type of framework provides a readable and easily understandable format to Business Analysts, Developers, Testers, etc.
  • Data-Driven Testing Framework: This type of framework helps separate test data from the test-script logic by storing test data in some external database in the form of key-value pairs. These keys can be used for accessing as well as populating data into the test scripts.
  • Keyword-Driven Testing Framework: This type of framework is an extension of the data-driven testing framework. In addition to separating test data and the test-script logic, it also separates a part of the test script code by storing it in an external data file.
  • Library Architecture Testing Framework: This type of framework groups common steps into functions under a library and calls these functions as and when required.
  • Module-Based Testing Framework: This type of framework divides each test application into several isolated and logical modules, with each module having its distinct test script.
  • Hybrid Testing Framework: This type of framework is a combination of the above-mentioned frameworks leveraging all their good features.
  • Robot Framework is a modular open-source automation framework that can interact with 3rd party libraries and functions. To execute a web testing library such as Selenium, a test automation runner or an automation wrapper is required, which is provided to it in the form of Robot Framework. Other popular test runners to serve the same purpose are MSTest, TestNG, Nunit, Junit, etc.
The below diagram shows the connection of the Robot framework to the Selenium library:
       

Question - 110 : - What do you understand about the Page Object Model in the context of Selenium? What are its advantages?

Answer - 110 : -

Page Object Model (POM) is a design pattern that generates an Object Repository for web UI elements and is widely used in test automation. The paradigm has the advantage of reducing code duplication and improving test maintenance. According to this paradigm, each web page in the application should have its own Page Class. This Page class will identify the web page's WebElements and also has Page methods that operate on those WebElements. The names of these methods should correspond to the tasks they perform, for example, if a loader is waiting for the payment gateway to appear, the POM method name could be waitForPaymentScreenDisplay().

Following are the advantages of the Page Object Model (POM) :

  • According to the Page Object Design Pattern, user interface activities and flows should be separated from verification. Our code is clearer and easier to understand as a result of this notion.
  • The second advantage is that the object repository is independent of test cases, allowing us to reuse the same object repository with different tools. For example, we can use Selenium to combine Page Object Model with TestNG/JUnit for functional testing and JBehave/Cucumber for acceptability testing.
  • Because of the reusable page methods in the POM classes, code gets less and more efficient.
  • Methods are given more realistic names that can be easily associated with the UI operation. If we land on the home page after clicking the button, the function name will be 'gotoHomePage()'.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners