Selenium tricky interview questions

Selenium tricky interview questions

Below Are Most Frequently Asked Selenium tricky interview questions . Below Content Is For Interview Preparation You Can Read Below Questions And Answers Before Your Interview To Revised The Concepts You Already Know.

1.What is the difference between driver.getWindowHandle() and driver.getWindowHandles() in selenium?

  • driver.getWindowHandle() : Its return type is String and it will give window handle of currently open window in web browser 
  • driver.getWindowHandles() : Its return type is Set it return a set of window handles which can be used to iterate over all open windows of this WebDriver instance

2.How can I handle dynamic objects in selenium?

There are different ways where we can handle dynamic objects in selenium  It is one of the tricky selenium interview question.
  • Absolute XPath : Absolute Xpath are most frequently used to resolve the dynamic element issues. Reason is, it does not change with page reloads. However problem with Absolute XPath locators is that its very fragile .
  •  Identify Element by starting Text : If the dynamic elements have a definite pattern to them, then we can also use JavaScript function “starts-with” in our element locators to separate the dynamic part of locator from static part.
Starts-with()
//a[starts-with(@id,’link-out’)]
//a[starts-with(text(),’G’)]
  • Identify Element by text contains : If the dynamic elements have a definite pattern to them, then we can also use JavaScript functions “contains” in our element locators to separate the dynamic part of locator from static part.
Contains(); 
//a[contains(text(),’submit’)] --used for text 
//a[contains(@id,’Login’)] --used for attributes 
//a[contains(@data-pid,'23') and contains(@class,'gb_P')] --Example google Home Page
 (https://google.com)

3.What is the page object in Selenium?

Page Object in selenium is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT. The tests then use the methods of this page object class whenever they need to interact with the UI of that page.

  • There is a clean separation between test code and page specific code such as locators (or their use if you’re using a UI Map) and layout.
  • It helps make the code more readable, maintainable, and reusable.
  • Code becomes less and optimized because of the reusable page methods in the POM classes.
  • Below is code for Page object model for sample home page
WebDriver driver;
public class HomePage {

@FindBy(how=How.ID, using="username")
WebElement username;

@FindBy(how=How.ID, using="password")
WebElement password;

@FindBy(how=How.ID, using="submitbtn")
WebElement submit;

void login (){
username.SendKeys("ASE@gmail.com");
password.SendKeys("ESAEW");
submit.Click();
}
}
//Automation Script with page object creation.
public class  Testcases{@Test
public void testcase1{
driver.get("url"); 
HomePage Obj= PageFactory.initElements(driver, HomePage .class);
Obj.login(); //calling method login 
}

For Manual Testing Questions Click here 

5.How do you capturing screenshots with RemoteWebDriver/Selenium Grid ?

While running tests with RemoteWebDriver or Selenium Grid, it is not possible to take the screenshots, as the TakesScreenshot interface is not implemented in RemoteWebDriver. However, we can use the Augmenter class, which adds the TakesScreenshot interface to
the remote driver instance, as shown in the following code example:

driver = new Augmenter().augment(driver);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

The Augmenter class enhances the RemoteWebDriver by adding various interfaces to it, including the TakesScreenshot interface:

driver = new Augmenter().augment(driver); 

Later, we can use the TakesScreenshot interface from RemoteWebDriver to capture the screenshot.

6.How will you Handling session cookies in Selenium?

Websites use cookies to store user preferences, login information, and various other details of the client. It is one of the tricky selenium interview question. The Selenium WebDriver API provides various methods to manage these cookies during testing. Using these methods, we can read cookie values, add cookies, and delete cookies during the test. This can be used to test how the application reacts when cookies are manipulated. The WebDriver.Options interface provides the following methods to manage cookies:

Method Description
addCookie(Cookie cookie)
This method adds a cookie.
getCookieNamed(String name)
This method returns the cookie with a specified name.
getCookies()
This method returns all the cookies for current domain.
deleteCookieNamed(String name)
This method deletes the cookie with a specified name.
deleteAllCookies()
This method deletes all the cookies for current domain.
@Test
public void testCookies() {
driver.get("http://demo.magentocommerce.com/");
// Get the Your language dropdown as instance of Select class

Select language = new Select(driver.findElement(By.id("select-language")));
// Check default selected option is English

assertEquals("English",language.getFirstSelectedOption().getText());
// Store cookies should be none

Cookie storeCookie = driver.manage().getCookieNamed("store");
assertEquals(null, storeCookie);
// Select an option using select_by_visible text

language.selectByVisibleText("French");
// Store cookie should be populated with selected country

storeCookie = driver.manage().getCookieNamed("store");
assertEquals("french", storeCookie.getValue());

}

7.Explain Working with WebDriver events in Selenium ?

The Selenium WebDriver provides the EventFiringWebDriver class, which listens to various events happening during the test execution. For example, events are raised when we navigate to a page, when a click is performed on an element, or value is changed.  It is one of the tricky selenium interview question.The following table lists all the events that we can track during the test execution:

Event Description
beforeNavigateTo
This method is called before the get(String url) or the navigate().to(String url) method is called.
afterNavigateTo
This method is called after the get(String url) or the navigate().to(String url) method is called.
beforeNavigateBack
This method is called before the navigate().back() method.
afterNavigateBack
This method is called after the navigate().back() method.
beforeNavigateForward
This method is called before the navigate(). forward() method.
afterNavigateForward
This method is called after the navigate().forward() method.
beforeFindBy
This method is called before the following methods: ff WebDriver.findElement(...) ff WebDriver.findElements(...) ff WebElement.findElement(...) ff WebElement.findElements(...)
afterFindBy
This method is called after the following methods: ff WebDriver.findElement(...) ff WebDriver.findElements(...) ff WebElement.findElement(...) ff WebElement.findElements(...)
beforeChangeValueOf
This method is called before the WebElement.clear() or the WebElement.sendKeys(...) method.
afterChangeValueOf
This method is called after the WebElement.clear() or the WebElement.sendKeys(...) method.
beforeClickOn
This method is called before the WebElement.click() method.
afterClickOn
This method is called after the WebElement.click() method.
beforeScript
This method is called before the RemoteWebDriver. executeScript(java.lang.String, java.lang. Object[]) method.
afterScript
This method is called after the RemoteWebDriver. executeScript(java.lang.String, java.lang. Object[]) method.
onException
This method is called whenever an exception would be thrown.

We can use these event handlers to process extra commands. For example, before entering a value into a textbox, we can clear the existing value or capture a screenshot even if an exception is raised by WebDriver. This is done with the following steps:

  • Create your own user-defined event listener class. We can add the code that will be invoked when specific events are called to this class
  • Create an EventFiringWebDriver instance using WebDriver instance
  • And register the event listener class to the EventFiringWebDriver instance .

The event listener class can be created in two ways:

  • By implementing the WebDriverEventListener interface
  • By extending the AbstractWebDriverEventListener class

In this recipe, we will see how to use EventFiringWebDriver to listen to the WebDriver
events.

First, we will define an event listener class by implementing the WebDriverEventListener
interface in the following way:
package com.secookbook.examples.chapter04;
import org.openqa.selenium.*;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class MyListener implements WebDriverEventListener {

Working with Selenium API


public void beforeChangeValueOf(WebElement element, WebDriver
driver) {
element.clear();
}
}


Next, we will create a test that uses EventFiringWebDriver:


package com.secookbook.examples.chapter04;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class EventFiringTest {


private WebDriver driver;


@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
}


@Test
public void testEventFiringWebDriver() throws Exception {
EventFiringWebDriver eventDriver =
new EventFiringWebDriver(driver);
MyListener myListener = new MyListener();
eventDriver.register(myListener);
eventDriver.get("http://bit.ly/1DbdhsW");
eventDriver.findElement(By.id("q"))
.sendKeys("Selenium Testing Tools Cookbook");


}


@After
public void tearDown() throws Exception {
driver.quit();

}

}

8.Explain Synchronizing a test with custom-expected conditions in Selenium

With the explicit wait mechanism, we can also build custom-expected conditions along with common conditions using the ExpectedConditions class.  It is one of the tricky selenium interview question. This comes in handy when a wait cannot be handled with a common condition supported by the ExpectedConditions

class.In this recipe, we will explore how to create a custom condition.

We will create a test that will create a wait until an element appears on the page using the ExpectedCondition class as follows:

@Test
public void testExplicitWait() {

WebDriver driver = new FirefoxDriver();

// Launch the sample Ajax application
driver.get("http://cookbook.seleniumacademy.com/AjaxDemo.html");

try {

driver.findElement(By.linkText("Page 4")).click();

WebElement message = new WebDriverWait(driver, 5)
.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.id("page4"));

}
});

assertTrue(message.getText().contains("Nunc nibh tortor"));
} finally {

driver.quit();
}
}

Thanks for Reading “Selenium tricky interview questions”

Author: Saad Mansuri

Leave a Reply

Your e-mail address will not be published. Required fields are marked *