Selenium Testing Interview Questions and Answers PDF
Selenium Testing Interview Questions and Answers PDF
Answers Pdf
1. How do I launch the browser using WebDriver?
Answer: The following syntax can be used to launch the Browser:
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait
21. Could you explain the line of code Webdriver driver = new FirefoxDriver()?
Answer: ‘WebDriver’ is an interface and we are creating an object of type WebDriver instantiating an
object of FirefoxDriver class.
22. What is the alternate way to click on the login button?
Answer: submit() method could be used as the alternate way to click on the login button, but only if
attribute type=submit.
23. What is the testng.xml file used for?
Answer: testng.xml file is used to configure the whole test suite. Here we can create a test suite, create
test groups, mark tests for parallel execution, add listeners and pass parameters to test scripts. It can be
used for the further test suite triggering.
24. What is an Exception?
Answer: Exceptions are events due to which java program ends abruptly without giving expected output.
Java provides a framework where a user can handle exceptions.
The process of handling Exceptions is called Exception Handling.
Exceptions need to be handled because they break the normal flow of execution of a program.
One of the important intentions of exception handling is to prevent this break and continue program
execution. Sometimes, you might want to perform some series of actions on occurring of a certain
exception.
When an exception occurs, an exception object is created which is technically referred to as ‘Throwing an
Exception’ and we add Try/Catch blocks like,
try {
// Protected code
catch (ExceptionName e) {
// Catch block
1. The piece of code that might throw an exception is added inside the Try block.
2. The Catch statement catches the exception and takes it as a parameter.
3. When no exception is thrown, the try statement is executed and not the catch statement.
Example: When the selenium script fails due to the wrong locator, then the developer should be able to
understand the reason for failure and this can be achieved easily if the exception is handled properly in the
program.
In my experience, it is best to try to avoid WebDriver exceptions whenever possible and catch truly
exceptional cases. Use try/catch to handle things that go wrong and are outside my control.
25. Do you know a way to refresh the browser by using Selenium?
Answer:
The list of commands to refresh a page in Selenium:
navigate().refresh()
getCurrentUrl()
navigate().to(driver.getCurrentUrl())
sendKeys(Keys.F5)
Reports can only be generated using third-party tools like TestNG or JUnit.
43. What are some expected conditions that can be used in Explicit waits?
Answer: Some of the commonly used expected conditions of an element that can be used with expicit
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)
44. What is fluent wait in selenium?
Answer: A fluent wait is a type of wait in which we can also specify polling interval(intervals after which
driver will try to find the element) 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() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“textBoxId”));
45. How to mouse hover an element in selenium?
Answer: Code to mouse hover over an element in selenium-
Actions action = new Actions(driver);
Web Element element=driver. find Element(By.id(“element Id”));
action. move To Element(element).perform();
46. What are DesiredCapabilities in selenium web driver?
Answer: Desired capabilities are a set of key-value pairs that are used for storing or configuring browser-
specific properties like its version, platform, etc in the browser instances.
47. What are some commonly encountered exceptions in selenium?
Answer: Some of the commonly seen exceptions in selenium are-
NoSuchElementException – When no element could be located from the locator provided.
ElementNotVisibleException – When an element is present in the dom but is not visible.
NoAlertPresentException – When we try to switch to an alert but the targetted alert is not present.
NoSuchFrameException – When we try to switch to a frame but the targetted frame is not present.
NoSuchWindowException – When we try to switch to a window but the targetted window is not present.
UnexpectedAlertPresentException – When an unexpected alert blocks the normal interaction of the driver.
TimeoutException – When a command execution gets a timeout.
InvalidElementStateException – When the state of an element is not appropriate for the desired action.
NoSuchAttributeException – When we are trying to fetch an attribute’s value but the attribute is not correct
WebDriverException – When there is some issue with driver instance preventing it from getting launched.
48. How to check which option in the dropdown is selected?
Answer: Using the isSelected() method we can check the state of a dropdown’s option.
Select countriesDropDown = new Select(driver.findElement(By.id(“countries”)));
dropdown.selectByVisibleText(“India”);
//returns true or false value
System.out.println(driver.findElement(By.id(“India”)).isSelected());
49. How to locate a link using its text in selenium?
Answer:
Using link text() and partial link text() we can locate a link. The difference between the two is link text
matches the complete string passed as a parameter to the link texts. Whereas partial link text matches the
string parameter partially with the link texts.
WebElement link1 = driver.findElement(By.linkText(“artOfTesting”));
WebElement link2 = driver.findElement(By.partialLinkText(“artOf”));
50. What are the benefits of Automation Testing?
Answer:
Benefits of Automation testing are:
Supports execution of repeated test cases