Selenium Interview Questions and Answers

Selenium Interview Questions and Answers

Selenium Interview Questions and Answers

In this post, we present some of the commonly asked Selenium interview questions and answers. These questions are taken from our Selenium interview guide. This guide is one of the resources of our Job readiness package.

These interview questions are code based. A typical selenium interview will have conceptual and code-based questions. In another post, we are going to publish conceptual interview questions with answers. 

Below are the code-based Selenium interview questions and answers, selected by our team.

How to simulate scrolling down a page in Selenium?

JavaScript Executor can be used to scroll a page. The page can be scrolled up or down. Below code is used to scroll down vertically 250 pixels –

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(“scroll(0,250);”);

OR we can use below code to scroll for an element –

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(“arguments[0].scrollIntoView(true);”, date);

 


 

What are the different types of waits available in Selenium?

There are two types of wait in Selenium webdriver:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: In Implicit wait, if WebDriver is not able to find an element in the Document Object Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM. The Implicit wait may slow down your tests, because once set, the implicit wait is set for the lifetime of the WebDriver object’s instance.

Implicit waits are implemented as shown below:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait: Explicit wait is a type of wait (applied through code piece), which is used to stop the execution till a specific condition is true.We use WebDriverWait and ExpectedCondition classes of Selenium web driver to implement explicit wait.

Unlike Implicit waits, explicit waits are applied for a particular instance only.

Explicit waits are implemented as shown below:

WebElement txtFirstname = driver.findElement(By.cssSelector(“input[name=’firstname’]”));

WebDriverWait wait = new WebDriverWait(driver,10);

wait.until(ExpectedConditions.elementToBeClickable(txtFirstname));

 

 


How to perform double click using WebDriver?

Actions class of web driver can be used to perform action of double click on an element. The code to simulate double click using Webdriver is shown below: 

WebElement button = driver.findElement(By.cssSelector(“input[name=’button1′]”));

Actions builder = new Actions(driver);

builder.doubleClick(button).build().perform();

 


 

Is FirefoxDriver a Class or an Interface?

FireFoxDriver is a class that implements WebDriver interface.

WebDriver is an Interface, and we define a reference variable (driver) whose type is an interface, as shown below.

WebDriver driver = new FireFoxDriver;

Now any object we assign to it must be an instance of a class that implements the interface.

We can create a reference variable of an interface, but we can’t instantiate any interface or in other words create object of an interface.

WebDriver driver = New WebDriver;

Above code will throw an error as shown below:

Webdriver Interface error


 

How do you select a value in the drop down?

Value in the drop down can be selected using WebDriver’s Select class.
For example consider HTML file like this –

<html>

<body>

<select id = “designation”>

<option value = “PM”>Program Manager</option>

<option value = “QA”>Quality Analyst</option>

<option value = “prog”> Programmer </option>

<option value = “SM”> Sales Manager </option>

</option>

</select>

<body>

</html>

Let’s look at the code to select a value “Quality Analyst” from the drop down:

Select s1 = new Select(driver.findElement(By.id(“designation”)));

 //selecting By Index number

s1.selectByIndex(1);

//Select By Visible Text

s1.selectByVisibleText(“Quality Analyst”);

// Selecting By Value:

s1.selectByValue(“QA”);

 


 

How to capture a screenshot in Selenium Web driver?

The code to capture screen shot in Selenium is shown below:

publicvoid takeSnapshot()

{

  try

{

TakesScreenshot ts = (TakesScreenshot) driver;

File image;

image = ts.getScreenshotAs(OutputType.FILE);

File destFile = new File(“D:\\OnlineTraining\\OnlineBatch\\Screenshot.jpg”));

FileUtils.copyFile(image, destFile);

}

catch (IOException e)

{

System.out.println(“Input Output Exception “);

e.printStackTrace();

}  }

In this way, we can create a method for capturing screenshot and call it whenever we need to take a screenshot in multiple scenario.

Selenium Tutorial for Beginners

 

What’s Next?

In this tutorial, we presented some Selenium interview questions and answers. Our training programs have a Job interview preparation package, which provides you all the help to clear job interviews. The features of these job interview preparation programs are:

  • Interview questions and answers to help you practice (PDF)
  • A mock interview with a Selenium professional, who interviews similar professionals
  • Detailed feedback report

Complete details can be checked at our Selenium Certification Training page. You can also go and read more Selenium tutorials published on this blog.

Want to become an Automation Tester in Selenium?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

Selenium-Certification-training-with-Job-readiness
Selenium Interview questions and answers