List Headline Image
Updated by edureka.co on Oct 18, 2021
 REPORT
edureka.co edureka.co
Owner
15 items   2 followers   0 votes   11 views

Top Selenium Interview Questions And Answers You Must Prepare In 2019

Since there is so much dependency on the web today, ensuring up-time and functioning of web apps is an evident need. Selenium is an automation testing tool developed precisely for that purpose, and this blog lists the frequently asked Selenium Interview Questions for those(freshers & experienced) planning to get into testing domain.

1

What are the significant changes in upgrades in various Selenium versions?

Selenium v1 included only three suite of tools: Selenium IDE, Selenium RC and Selenium Grid. Note that there was no WebDriver in Selenium v1. Selenium WebDriver was introduced in Selenium v2. With the onset of WebDriver, Selenium RC got deprecated and is not in use since. Older versions of RC is available in the market though, but support for RC is not available. Currently, Selenium v3 is in use, and it comprises of IDE, WebDriver and Grid.

IDE is used for recording and playback of tests, WebDriver is used for testing dynamic web applications via a programming interface and Grid is used for deploying tests in remote host machines.

2

Explain the different exceptions in Selenium WebDriver.

Exceptions in Selenium are similar to exceptions in other programming languages. The most common exceptions in Selenium are:

  • TimeoutException: This exception is thrown when a command performing an operation does not complete in the stipulated time
  • NoSuchElementException: This exception is thrown when an element with given attributes is not found on the web page
  • ElementNotVisibleException: This exception is thrown when the element is present in DOM (Document Object Model), but not visible on the web page
  • StaleElementException: This exception is thrown when the element is either deleted or no longer attached to the DOM
3

How can you redirect browsing from a browser through some proxy?

Selenium provides a PROXY class to redirect browsing from a proxy. Look at the example below:

String PROXY = “199.201.125.147:8080”;

org.openqa.selenium.Proxy proxy = new.org.openqa.selenium.Proxy();
proxy.setHTTPProxy(Proxy)
.setFtpProxy(Proxy)
.setSslProxy(Proxy)
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

4

What are the different types of WAIT statements in Selenium WebDriver? Or the question can be framed like this: How d...

There are basically two types of wait statements: Implicit Wait and Explicit Wait.

Implicit wait instructs the WebDriver to wait for some time by polling the DOM. Once you have declared implicit wait, it will be available for the entire life of the WebDriver instance. By default, the value will be 0. If you set a longer default, then the behavior will poll the DOM on a periodic basis depending on the browser/ driver implementation.

Explicit wait instructs the execution to wait for some time until some condition is achieved. Some of those conditions to be attained are:

  • elementToBeClickable
  • elementToBeSelected
  • presenceOfElementLocated
5

Write a code to wait for a particular element to be visible on a page. Write a code to wait for an alert to appear.

We can write a code such that we specify the XPath of the web element that needs to be visible on the page and then ask the WebDriver to wait for a specified time. Look at the sample piece of code below:

WebDriverWait wait=new WebDriverWait(driver, 20);
Element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( “

6

What is the use of JavaScriptExecutor?

JavaScriptExecutor is an interface which provides a mechanism to execute Javascript through the Selenium WebDriver. It provides “executescript” and “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window. An example of that is:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

7

How to handle keyboard and mouse actions using Selenium?

We can handle special keyboard and mouse events by using Advanced User Interactions API. The Advanced User Interactions API contains the Actions and the Action Classes that are needed for executing these events. Most commonly used keyboard and mouse events provided by the Actions class are in the table below:

clickAndHold() - Clicks (without releasing) the current mouse location.
dragAndDrop() - Performs click-and-hold at the location of the source element, moves.
source, target() - Moves to the location of the target element, then releases the mouse.

8

How can you fetch an attribute from an element? How to retrieve typed text from a textbox?

We can fetch the attribute of an element by using the getAttribute() method. Sample code:

WebElement eLogin = driver.findElement(By.name(“Login”);
String LoginClassName = eLogin.getAttribute("classname");

Here, I am finding the web page’s login button named ‘Login’. Once that element is found, getAttribute() can be used to retrieve any attribute value of that element and it can be stored it in string format. In my example, I have retrieved ‘classname’ attribute and stored it in LoginClassName.

Similarly, to retrieve some text from any textbox, we can use getText() method. In the below piece of code I have retrieved the text typed in the ‘Login’ element.

WebElement eLogin = driver.findElement(By.name(“Login”);
String LoginText = Login.getText ();

9

How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons to achieve the special functionality. So it is not enough just to specify keys.ALT or keys.SHIFT or keys.CONTROL functions.

For the purpose of holding onto these keys while subsequent keys are pressed, we need to define two more methods: keyDown(modifier_key) and keyUp(modifier_key)

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it’s kept pressed.

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a key release.
Hence with a combination of these two methods, we can capture the special function of a particular key.

public static void main(String[] args)
{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();

driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);

Actions builder = new Actions(driver);
Action seriesOfActions = builder
.moveToElement(txtUerName)
.click()
.keyDown(txtUserName, Keys.SHIFT)
.sendKeys(txtUserName, “hello”)
.keyUp(txtUserName, Keys.SHIFT)
.doubleClick(txtUserName);
.contextClick();
.build();
seriesOfActions.perform();
}

10

How to set the size of browser window using Selenium?

To maximize the size of browser window, you can use the following piece of code:
driver.manage().window().maximize(); – To maximize the window

To resize the current window to a particular dimension, you can use the setSize() method. Check out the below piece of code:

System.out.println(driver.manage().window().getSize());
Dimension d = new Dimension(420,600);
driver.manage().window().setSize(d);

To set the window to a particular size, use window.resizeTo() method. Check the below piece of code:

((JavascriptExecutor)driver).executeScript("window.resizeTo(1024, 768);");

11

How to switch to a new window (new tab) which opens up after you click on a link?

If you click on a link in a web page, then for changing the WebDriver’s focus/ reference to the new window we need to use the switchTo() command. Look at the below example to switch to a new window:
driver.switchTo().window();

Here, ‘windowName’ is the name of the window you want to switch your reference to.

In case you do not know the name of the window, then you can use the driver.getWindowHandle() command to get the name of all the windows that were initiated by the WebDriver. Note that it will not return the window names of browser windows which are not initiated by your WebDriver.

Once you have the name of the window, then you can use an enhanced for loop to switch to that window. Look at the piece of code below.

String handle= driver.getWindowHandle();
for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle);
}

12

Explain how can you find broken links in a page using Selenium WebDriver?

This is a trick question which the interviewer will present to you. He can provide a situation where in there are 20 links in a web page, and we have to verify which of those 20 links are working and how many are not working (broken).

Since you need to verify the working of every link, the workaround is that, you need to send http requests to all of the links on the web page and analyze the response. Whenever you use driver.get() method to navigate to a URL, it will respond with a status of 200 – OK. 200 – OK denotes that the link is working and it has been obtained. If any other status is obtained, then it is an indication that the link is broken.

But how will you do that?
First, we have to use the anchor tags to determine the different hyperlinks on the web page. For each tag, we can use the attribute ‘href’ value to obtain the hyperlinks and then analyze the response received for each hyperlink when used in driver.get() method.

13

Which technique should you consider using throughout the script “if there is neither frame id nor frame name”?

f neither frame name nor frame id is available, then we can use frame by index.

Let’s say, that there are 3 frames in a web page and if none of them have frame name and frame id, then we can still select those frames by using frame (zero-based) index attribute. Each frame will have an index number. The first frame would be at index “0”, the second at index “1” and the third at index “2”. Once the frame has been selected, all subsequent calls on the WebDriver interface will be made to that frame.

driver.switchTo().frame(int arg0);

C. TestNG Framework For Selenium – Selenium Interview Questions
From here on, you’ll read all the TestNG and Selenium Webdriver interview questions for experienced professionals.

14

Explain what does @Test(invocationCount=?) and @Test(threadPoolSize=?) indicate.

@Test(invocationCount=?) is a parameter that indicates the number of times this method should be invoked.
@Test(threadPoolSize=?) is used for executing suites in parallel. Each suite can be run in a separate thread.

To specify how many times @Test method should be invoked from different threads, you can use the attribute threadPoolSize along with invocationCount. Example:

@Test(threadPoolSize = 3, invocationCount = 10)
public void testServer() {
}

15

Explain what is Group Test in TestNG?

In TestNG, methods can be categorized into groups. When a particular group is being executed, all the methods in that group will be executed. We can execute a group by parameterizing it’s name in group attribute of @Test annotation. Example: @Test(groups={“xxx”})

@Test(groups={“Car”})
public void drive(){
system.out.println(“Driving the vehicle”);
}

@Test(groups={“Car”})
public void changeGear() {
system.out.println("Change Gears”);
}

@Test(groups={“Car”})
public void accelerate(){
system.out.println(“Accelerating”);
}

To continue reading more questions, click here