Selenium Interview Questions - All About Testing Selenium Interview Questions

Friday 23 August 2019

Selenium Interview Questions

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. What is exception test in Selenium?
An exception test is an exception that you expect will be thrown inside a test class. If you have written a test case in such way that it should throw an exception, then you can use the @Test annotation and specify which exception you will be expecting by mentioning it in the parameters. Take a look at the example below: @Test(expectedException = NoSuchElementException.class)
Do note the syntax, where the exception is suffixed with .class

4. Why and how will you use an Excel Sheet in your project?
The reason we use Excel sheets is because it can be used as a data source for tests. An excel sheet can also be used to store the data set while performing DataDriven Testing. These are the two main reasons for using Excel sheets.
When you use the excel sheet as a data source, you can store the following:
Application URL for all environments: You can specify the URL of the environment in which you want to do the testing like: development environment or testing environment or QA environment or staging environment or production/ pre-production environment.
User name and password credentials of different environments: You can store the access credentials of the different applications/ environments in the excel sheet. You can store them in an encoded format and whenever you want to use them, you can decode them instead of leaving it plain and unprotected.
Test cases to be executed: You can list down the entire set of test cases in a column and in the next column, you can specify either Yes or No which indicates if you want that particular test case to be executed or ignored.
When you use the excel sheet for DataDriven Test, you can store the data for different iterations to be performed in the tests. For example, while testing a web page, the different sets of input data that needs to be passed to the test box can be stored in the excel sheet

5. 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:
1 JavascriptExecutor js = (JavascriptExecutor) driver;
2 js.executeScript(Script,Arguments);

6. How to scroll down a page using JavaScript in Selenium?
We can scroll down a page by using window.scrollBy() function. Example:
1 ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

7. How to scroll down to a particular element?
To scroll down to a particular element on a web page, we can use the function scrollIntoView(). Example:
1 ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);

8. 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:
Selenium functions and their explanation
Method               Description
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

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 take screenshots in Selenium WebDriver?
You can take a screenshot by using the TakeScreenshot function. By using getScreenshotAs() method you can save that screenshot. Example:
File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);

# How to set the size of the 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. Explain how you will login into any site if it is showing any authentication popup for username and password?
Since there will be popup for logging in, we need to use the explicit command and verify if the alert is actually present. Only if the alert is present, we need to pass the username and password credentials. The sample code for using the explicit wait command and verifying the alert is below:
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));

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 <a> to determine the different hyperlinks on the web page. For each <a> 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. What is parameterization in TestNG? How to pass parameters using testng.xml?
Parameterization is the technique of defining values in testng.xml file and sending them as parameters to the test class. This technique is especially useful when we need to pass multiple login credentials of various test environments. Take a look at the code below, in which “myName” is annotated as a parameter.
public class ParameterizedTest1{
@Test
@Parameters("myName")
public void parameterTest(String myName) {
System.out.println("Parameterized value is : " + myName);
}
}
To pass parameters using testng.xml file, we need to use ‘parameters’ tag. Look at the below code for example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name=”CustomSuite">
  <test name=”CustomTest”>
   <parameter name="myName" value=”John"/>
    <classes>
     <class name="ParameterizedTest1" />
    </classes>
  </test>
</suite>

 14. 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”);
}

15. How does TestNG allow you to state dependencies? Explain it with an example.
Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. Method dependency only works if the “depend-on-method” is part of the same class or any of the inherited base classes (i.e. while extending a class). Syntax:
@Test(dependsOnMethods = { “initEnvironmentTest” })
@Test(groups={“Car”})
public void drive(){
system.out.println(“Driving the vehicle”);
}
@Test(dependsOnMethods={“drive”},groups={cars})
public void changeGear() {
system.out.println("Change Gears”);
}
@Test(dependsOnMethods={“changeGear”},groups={“Car”})
public void accelerate(){
system.out.println(“Accelerating”);
}

16. 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() {
}

0 comments:

Post a Comment