DEV Community

Cover image for What are top questions asked during a Selenium Interview?
rahul
rahul

Posted on

What are top questions asked during a Selenium Interview?

With the increase in technology and modern websites and applications, rigorous testing is required before making it public. The testing is performed using various tools. One such tool is Selenium. Selenium is an open-source automated tool that is used to test web applications across various cross-browsers and supports a lot of programming languages. Here are the top selenium interview questions for both beginners and experienced professionals.

Que-1 What is an XPath?
Ans- An Xpath is a syntax that is used for defining elements. Its a query language that is used for selecting nodes from XML documents. Also, it is one of the various other locators supported by selenium Webdriver.

Que-2 What are various ways of locating an element in Selenium?
Ans- Locators are used to locating elements in a selenium Webdriver. And there are various ways to locate these elements
Id
XPath
CSS selector
className
tagName
name
linkText
partialLinkText

Que-3 How can we type text in a textbox element using Selenium?
Ans- We can type text in the textbox with the help of sendKeys() method.
WebElement searchTextBox = driver.findElement(By.id("srch"));
searchTextBox.sendKeys("searchTerm");

Que-4 How to switch between multiple windows in Selenium?
Ans- A window handler is a unique identifier that incorporates the address of all windows.
Selenium has two commands to work with multiple windows that are driver.getWindowHandles() and driver.switchTo().window(“{windowHandleName}”)
The get.windowhandle(): helps in getting the window handle of the current window and switch to: helps in switching between the windows
If we pass a particular window handle to the driver.switchTo().window(“{windowHandleName}”) command then we can switch control/focus to that particular window.
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

Que-5 How can we fetch a text written over an element?
Ans- With the help of the getText() method we can fetch the text over an element.
String text = driver.findElement("elementLocator").getText();

Que-6 What are the various mouse actions that can be performed using Selenium?
Ans- The various mouse events supported in Selenium are-
click(WebElement element)
doubleClick(WebElement element)
contextClick(WebElement element)
mouseDown(WebElement element)
mouseUp(WebElement element)
mouseMove(WebElement element)
mouseMove(WebElement element, long xOffset, long yOffset)

Que-7 How can we capture screenshots using Selenium?
Ans- getScreenshotAs method of the TakesScreenshot interface is used to take the screenshots using selenium
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\testScreenShot.jpg"));

Que-8 How to right-click an element?
Ans- We can right-click an element using the following code snippet
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.contextClick(element).perform();

Que-9 How to do drag and drop in Selenium?
Ans- Drag and drop is used mostly during automation testing, we can drag and drop elements with the help of Action Class that is inbuilt and is used for handling mouse and keyboard elements using the sample code given below
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(SourceElement)
.moveToElement(TargetElement)
.release(TargetElement)
.build();
dragAndDrop.perform();

Que-10 What is Page Factory?
Ans- Page Factory is an inbuilt Page Object Model framework. It cab be consideed as an extenssion of Page Objetcs.
PageFactory.initElements() : used to intialize all web elements with @FindBy annotation.
public class SamplePage {
WebDriver driver;
@FindBy(id="search")
WebElement searchTextBox;
@FindBy(name="searchBtn")
WebElement searchButton;
//Constructor
public samplePage(WebDriver driver){
this.driver = driver;
//initElements method to initialize all elements
PageFactory.initElements(driver, this);
}
//Sample method
public void search(String searchTerm){
searchTextBox.sendKeys(searchTerm);

searchButton.click();
}
}

Que- 11 What is a data-driven framework?
Ans- Data-driven framework is used to derive test cases from the test data that is put in external files like CSV, Excel, etc. It is a technique used to differentiate the “data set” from the actual “test case” so that one can easily modify the code according to the functionality.
TestNG provides inherent support for data-driven testing using @dataProvider annotation.

Que-12 What is TestNG?
Ans- NG in TestNG stands for Next Generation. TestNG is an automation framework that can be integrated with Selenium or any other automation tool. It's quite important when one is developing the framework from scratch.
This is the end of our top questions that are mostly asked in interviews. Thanks for reading. :)

REFERENCES:-
Selenium Interview Questions
Selenium
Selenium Basics

Top comments (0)