Selenium Java Interview Questions

Rohit Kumar·2021년 6월 30일

Ques.1. How can we launch different browsers in Selenium WebDriver

Ans. By creating an instance of the desired browser driver e.g. below command will initialize the Firefox browser.

WebDriver driver = new FirefoxDriver();

Ques.23. What is the use of driver.get(“URL”) and driver.navigate().to(“URL”) commands? Is there any difference between the two?

Ans. Both driver.get(“URL”) and driver.navigate().to(“URL”) commands are used to navigate to a URL passed as parameter.
There is a minor difference between the two commands-

driver.navigate() allows moving back and forward in browser history with the help of driver.navigate().forward() and driver.navigate().back() commands.
In the case of single-page applications (where the URL is appended by ‘#’ to navigate to different sections of the page), driver.navigate().to() navigates to a particular section by changing the URL without refreshing the page whereas driver.get() refreshes the page also.

This refreshing of the page is also the primary reason because of which history is not maintained in the case of the driver.get() command.
Reference – Stack overflow

Ques.24. How can we type text in a textbox element using Selenium?

Ans. With the help of sendKeys() method we can type text in a textbox-

WebElement searchTextBox = driver.findElement(By.id("srch"));
searchTextBox.sendKeys("searchTerm");

Ques.25. How can we clear a text written in a textbox?

Ans. In order to delete the text written in a textbox, we can use the clear() method.

driver.findElement(By.id("elementLocator")).clear();

Ques.26. How to check a checkBox in Selenium?

Ans. The same click() method used for clicking buttons or radio buttons can be used for checking the checkbox as well.

Ques.27. How can we submit a form in Selenium?

Ans. Using the submit() method we can submit a form in selenium.

driver.findElement(By.id("form1")).submit();

Also, the click() method can be used for the same purpose.

Ques.28. Explain the difference between close and quit command.

Ans. The difference between close and quit command is-
driver.close() – Used to close the current browser having a focus.
driver.quit() – Used to close all the browser instances.

Ques.29. How to switch between multiple windows in Selenium?

Ans. Selenium has driver.getWindowHandles() and driver.switchTo().window(“{windowHandleName}”) commands to work with multiple windows.

The getWindowHandles() command returns a list of ids corresponding to each window. 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);
}

Ques.30. What is the difference between driver.getWindowHandle() and driver.getWindowHandles() in Selenium?

Ans. The driver.getWindowHandle() returns a handle of the current window (a single unique identifier).
Whereas driver.getWindowHandles() returns a set of handles of all the windows available.

Ques.31. How can we move to a particular frame in Selenium?

Ans. The driver.switchTo() commands can be used for switching to a particular iframe.

driver.switchTo().frame("{frameIndex/frameId/frameName}");

For locating a frame, we can either use the index (starting from 0), its name, or its Id.

Ques.32. Can we move back and forward in the browser using Selenium?

Ans. Yes, using driver.navigate().back() and driver.navigate().forward() commands, we can move backward and forward in a browser.

Ques.33. What are the different ways to refresh a browser?

Ans. There a multiple ways to refresh a page in Selenium-

Using driver.navigate().refresh() command.
Using sendKeys(Keys.F5) on any textbox on the webpage.
Using driver.get(“URL”) on the current URL or using driver.getCurrentUrl().
Using driver.navigate().to(“URL”) on the current URL or driver.navigate().to(driver.getCurrentUrl());

Ques.34. How can we maximize the browser window in Selenium?

Ans. We can maximize the browser window using the following command-

driver.manage().window().maximize();

Ques.35. How can we fetch a text written over an element?

Ans. Using the getText() method we can fetch the text over an element.

String text = driver.findElement("elementLocator").getText();

Ques.36. How can we find the value of different attributes like name, class, value of an element?

Ans. Using getAttribute(“{attributeName}”) method, we can find the value of different attributes of an element e.g.-

String valueAttribute =
driver.findElement(By.id("locator")).getAttribute("value");

Ques.37. How to delete cookies in Selenium?

Ans. Using deleteAllCookies() method.

driver.manage().deleteAllCookies();

Ques.38. What is an implicit wait in Selenium?

Ans. An implicit wait is a type of wait that waits for a specified time while locating an element before throwing NoSuchElementException. By default, Selenium tries to find web elements immediately when required without any wait. So, it is good to use implicit wait. This wait is applied to all the elements of the current driver instance.

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

Ques.39. What is an explicit wait in Selenium?

Ans. An explicit wait is a type of wait that is applied to a particular web element until the expected condition specified is met.

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));

It is advisable to use explicit waits over implicit waits because higher timeout value of implicit wait (set for handling only some of the elements) gets applied to all the web elements. Thus increasing the overall execution time of the script. On the other hand, we can apply different timeouts to the different elements in case of explicit waits.

Check our detailed tutorial here – Implicit & Explicit Waits in Selenium.

Ques.40. What are some expected conditions that can be used in Explicit waits?

Ans. Some of the commonly used expected conditions of an element that can be used with explicit waits are-

elementToBeClickable(WebElement element or By locator)
stalenessOf(WebElement element)
visibilityOf(WebElement element)
visibilityOfElementLocated(By locator)
invisibilityOfElementLocated(By locator)
attributeContains(WebElement element, String attribute, String value)
alertIsPresent()
titleContains(String title)
titleIs(String title)
textToBePresentInElementLocated(By, String)

Ques.41. What is a fluent wait?

Ans. A fluent wait is a type of wait in which we can also specify polling interval (the time intervals after which driver will try to find the elements when not located) along with the maximum timeout value.

Wait wait = new FluentWait(driver)

.withTimeout(20, SECONDS)

.pollingEvery(5, SECONDS)

.ignoring(NoSuchElementException.class);

WebElement textBox = wait.until(new Function<webdriver,webElement>() {

public WebElement apply(WebDriver driver) {

return driver.findElement(By.id("textBoxId"));

}

}
);

Ques.42. What are the different keyboard operations that can be performed in Selenium?

Ans. The different keyboard operations that can be performed in Selenium are-

.sendKeys(“sequence of characters”) – Used for passing character sequence to an input or textbox element.
.pressKey(“non-text keys”) – Used for keys like control, function keys etc that are non-text.
.releaseKey(“non-text keys”) – Used in conjunction with keypress event to simulate releasing a key from keyboard event.

Ques.43. What are the different mouse actions that can be performed using Selenium?

Ans. The different 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)

Ques.44. Write the code to double-click an element.

Ans. Code to double click an element-

Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.doubleClick(element).perform();

Ques.45. Write the code to right-click an element.

Ans. Code to right-click an element in selenium-

Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.contextClick(element).perform();

Source: ArtOfTesting

0개의 댓글