Selenium interview questions for experience

Selenium Interview Questions

Selenium interview questions for experience

Below Are Most Frequently Asked Selenium interview questions for experience . Below Content Is For Interview Preparation You Can Read Below Questions And Answers Before Your Interview To Revised The Concepts You Already Know.

1.Why do we write Webdriver driver = new Firefoxdriver() in selenium instead of WebDriver driver = new Web driver ()?

It is frequently asked Selenium interview questions for experience
  • In Interface we can only have the Method description and not the definition of Method. As we know inside interface we only mention the method name but not the functionality of the method.
  • we cannot create object for an Interface.
  • If we try to create an instance of Webdriver we system will throws error in the IDE.
WebDriver Driver = new FirefoxDriver();
  • WebDriver methods are implemented in the Firefox class for firefox browser  so we can say that the functionality of the method are written inside the Firefox class (same applies for Chrome and IE).

Many a time, developers don’t assign the name attribute to windows. In such cases, we can use its window handle attribute. However, the handle attributes keep changing and  it becomes difficult to identify the window, especially when there is more than one window open. Using the handle and title attributes of the page displayed in a window, we can build a more reliable way to identify child windows.

2.What is the difference between WebDriver and RemoteWebDriver in Selenium?

It is frequently asked Selenium interview questions for experience

  • Remote Web Driver requires the selenium-server-standalone to be running (the others do not). This could be running on the same machine or a “remote” one.
  • If we want to use the browser on the machine that is running the automation, then we can use everything but RemoteWebDriver.
  • The major difference is that RemoteWebDriver sends that request to open and control a web browser to a server, so you normally wouldn’t see the browser open and do it’s thing.
  • If we are using Selenium Grid then it is mandatory to use Selenium Webdriver.

3.Where do I store gecko /chromedriver so Selenium finds it?

We can store driver in any folder of system, we just need to give the right path in setProperty like,

System.setProperty(“webdriver.gecko.driver” , “//path to gecko driver exe”).

It’s recommended to create to store it in project workspace it self, that would be easy if you migrate script from one system to another. 

4.What are the best practices for maintaining the testing framework using Selenium webdriver using Java?

  • Using page object model with PageFactory : In page Object model we create an class of each page of our application. The goal is to encapsulate the functions of each page in a page object, and to use these objects in the test our Test scripts.
  • Keeping test scripts separate from Java implementation code : Keep it in a separate directory for clarity. Test scripts should be readable by a product manager or other non-programmer. It’s a good practice to keep test scripts separate and creating packages module wise. If they are getting unmanageable, consider them refactoring.
  • Use Maven for managing jar dependencies : Maven is very good build management tool and you can easily manage you dependencies from POM.xml 
  • Use TestNG instead of Junit : In TestNG you get lots of options such as grouping test cases Assertions , Paraller execution , You can easily execute group of test cases with help of TestNG.xml file.
  • Use drivers instantiation such as new FireFoxDriver,  new ChromeDriver in Before Annotations: As your script use @BeforeClass or @BeforeTest or any other @Before annotation of create an instance of your browser. To avoid the repetition of this code as it is very basic and needed scipt will be get used everywhere.
  • Using Good Reporting tool – You can keep you managers and stack holders happy with beautiful reports such as extent reports like Allure Reports , ReportNG and these will collate your results into something readable and Presentable.
  • If possible Use Jenkins : May be you are at early stage of your Automation framework development and environment is not stable yet but whenever possible use Jenkins for scheduling jobs and sending mails in case of build failure.

5.What is the difference between selenium 3.0 and selenium 2.0?

  • Selenium 3.0 has became a W3C (World wide Web consortium) standard.
  • Selenium 3.0 removed Selenium Core but supports Selenium RC indirectly through back-end Webdriver.
  • If we want to work with the older versions i.e. less than 47 version, there is no need to install Gecko driver but working with more than 47+ either in Selenium 2.0 or Selenium 3.0 requires Gecko driver installed and configured.
  • The Selenium RC APIs have been moved to a “legacy” package.
  • Selenium 3 has bug fixes from selenium 2 also it is more mobile automation focused.
  • The old Selenium Core libraries are dropped in 3.0.

For Manual Testing Questions Click here   

6.Explain Identifying and handling frames in Selenium

HTML frames allow developers to present documents in multiple views, which may be in a separate child window or sub-window. Multiple views offer developers a way to keep certain information visible while other views are scrolled or replaced. For example, within the same window, one frame might display a static banner, the second a navigation menu, and the third the main document that can be scrolled through or replaced by navigating in the second frame. It is frequently asked Selenium interview questions for experience.

A page with frames is created using the <frameset> tag or the <iframe> tag. All frame tags are nested with a <frameset> tag. In the following example, a page will display three frames, each loading different HTML pages:

<html>
<frameset cols="25%,*,25%" frameborder="NO" framespacing="0"
border="0">
<frame id="left" src="frame_a.htm" />
<frame src="frame_b.htm" />
<frame name="right" src="frame_c.htm" />
</frameset>
</html>

Frames can be identified by an ID or through the name attribute. In this recipe, we will identify and work with frames using the driver.switchTo().frame() method of the
WebDriver.TargetLocator interface, which is used to locate a given frame or window using the id, name, instance of WebElement, and the index.

Let's create a test on a simple page that has three frames. We will use id identify these frames and interact with contents of these frames, as shown in
the following examples:
In the testFrameWithIdOrName test, we will use the name and id attributes to
identify frames, as shown in the following code:

package com.secookbook.examples.chapter06;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class FramesTest {
public static WebDriver driver;

@BeforeClass

public static void setUp() {

driver = new FirefoxDriver();

driver.get("http://cookbook.seleniumacademy.com/Frames.html");

driver.manage().window().maximize();
}
@Test
public void testFrameWithIdOrName() {
try {
// Activate the frame on left side using it's id
attribute
driver.switchTo().frame("left");
// Get an element from the frame on left side and verify it's
// contents

WebElement msg = driver.findElement(By.tagName("p"));

assertEquals("This is Left Frame", msg.getText());
}

finally {
// Activate the Page, this will move context from frame back to the

// Page
driver.switchTo().defaultContent();
}

try {
// Activate the frame on right side using it's name
attribute
driver.switchTo().frame("right");
// Get an element from the frame on right side andverify it's
// contents
WebElement msg = driver.findElement(By.tagName("p"));

assertEquals("This is Right Frame", msg.getText());
}
finally {

// Activate the Page, this will move context fromframe back to the
// Page

driver.switchTo().defaultContent();
}
}
@AfterClass
public static void tearDown() {
// Close the Parent Popup Window

driver.close();
driver.quit();
}
}

7.How will you Identify and handle child window in Selenium?

It is frequently asked Selenium interview questions for experience. In Selenium WebDriver, testing multiple windows involves identifying a window, switching the
driver context to the window, then executing steps on the window, and finally, switching back
to the browser.

The Selenium WebDriver allows us to identify a window by its name attribute or window
handle, and switching between the window and the browser window is done using the
WebDriver.switchTo().window() method of WebDriver.TargetLocator.

In this recipe, we will identify and handle a window by using its name attribute. Developers provide the name attribute for a window that is different from its title. In the following example, a user can open a window by clicking on the Help button. In this case, the developer has provided HelpWindow as its name:

<button id="helpbutton" on
Click='window.open("help.html","HelpWindow","width=500,height=500"
);'>Help</button>
Let's create a test that identifies a window using its name attribute, as shown in the following
code example:

package com.secookbook.examples.chapter06;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class WindowTest {
public static WebDriver driver;

@BeforeClass
public static void setUp() {
driver = new FirefoxDriver();
driver.get("http://cookbook.seleniumacademy.com/Config.html");
driver.manage().window().maximize();
}

@Test
public void testWindowUsingName() {
// Store WindowHandle of parent browser window
String parentWindowId = driver.getWindowHandle();
// Clicking Help button will open Help Page in a new child window
driver.findElement(By.id("helpbutton")).click();

try {
// Switch to the Help window using name
driver.switchTo().window("HelpWindow");

try {
// Check the driver context is in Help window
assertEquals("Help", driver.getTitle());
} finally {
// Close the Help window
driver.close();
}
}

finally {
// Switch to the parent browser window
driver.switchTo().window(parentWindowId);
}
// Check driver context is in parent browser window
assertEquals("Build my Car - Configuration",
driver.getTitle());

}

@AfterClass
public static void tearDown() {
driver.quit();
}
}

8.How will you Identify and handle a window by its title ?

Many a time, developers don’t assign the name attribute to windows. In such cases, we can use its window handle attribute. However, the handle attributes keep changing and  it becomes difficult to identify the window, especially when there is more than one window open. Using the handle and title attributes of the page displayed in a window, we can build a more reliable way to identify child windows.

In this recipe, we will use the title attribute to identify the window and then perform operations on it.

We will create a test that retrieves the handles of all the open windows in the current driver context. We will iterate through this list and check the title matching the criteria, as follows:


@Test
public void testWindowUsingTitle() {
// Store WindowHandle of parent browser window
String parentWindowId = driver.getWindowHandle();
// Clicking Visit Us Button will open Visit Us Page in a new
child
// window
driver.findElement(By.id("visitbutton")).click();
// Get Handles of all the open windows
// iterate through list and check if tile of
// each window matches with expected window title


try {
for (String windowId : driver.getWindowHandles()) {
String title = driver.switchTo().window(windowId).getTitle();
if (title.equals("Visit Us")) {
assertEquals("Visit Us", driver.getTitle());
// Close the Visit Us window
driver.close();

}
}
}

finally {
// Switch to the parent browser window
driver.switchTo().window(parentWindowId);
}
// Check driver context is in parent browser window
assertEquals("Build my Car - Configuration", driver.getTitle());
}

9.Explain Identifying and handling a pop-up window by its content

It is frequently asked Selenium interview questions for experience. In certain situations, developers neither assign a name attribute nor provide a title to the page displayed in a window. This becomes more complex when a test needs to deal with multiple windows open at the same time and identify the desired window.

As a workaround to this problem, we can check the contents of each window returned by the driver.getWindowHandles() method to identify the desired window.

Let's create a test that retrieves the handles of all the open windows in the current driver context. It will then iterate through this list, switching to the window and then checking for the content, which will help in identifying the correct window, as shown in the following code example:


@Test
public void testWindowUsingContents() {
// Store WindowHandle of parent browser window


String currentWindowId = driver.getWindowHandle();
// Clicking Chat Button will open Chat Page in a new child
window


driver.findElement(By.id("chatbutton")).click();
// There is no name or title provided for Chat Page window
// We will iterate through all the open windows
// and check the contents to find out if it's Chat window


try {
for (String windowId : driver.getWindowHandles()) {
driver.switchTo().window(windowId);
// We will use the page source to check the contents


String pageSource = driver.getPageSource();


if (pageSource.contains("Configuration - Online Chat")) {
// Check the page for an element displaying a expected
// message
assertTrue(driver.findElement(By.tagName("p")).getText()

.equals("Wait while we connect you to Chat..."));

// Find the Close Button on Chat Window and close the
window
// by clicking Close Button


driver.findElement(By.id("closebutton")).click();
break;
}
}
}

finally {
// Switch back to the parent browser window
driver.switchTo().window(currentWindowId);
}
// Check driver context is in parent browser window
assertEquals("Build my Car - Configuration", driver.getTitle());
}

Author: Saad Mansuri

Leave a Reply

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