Selenium and Web Driver Interview Questions and Answers

Q. What is Selenium? A. Selenium is a suite of tools for browser automation (i.e. automated Web testing). It is composed of

Selenium IDE: A tool for recording and playing back. This is a Fire-fox plugin. y WebDriver and RC provide the APIs for a variety of languages like Java, .NET, PHP, etc. A Java example is shown below. The WebDriver and RC work with most browsers. y Grid transparently distribute your tests on multiple machines so that you can run your tests in parallel, cutting down the time required for running in-browser test suites.

Q. How would you go about using selenium for your web testing? A. The example below shows the steps involved in testing a simple login page with selenium + Web Driver. The pages have HTML snippets as shown below Login page: ... <form method="POST" action="some/url"> <input type="text" name="username"> <input type="text" name="password"> <\form> ...

Login response page ? 1 2 3 4 5 6 7 ... <table> <tr> <td>John</td>< /tr> </table> ...

STEP 1: Configure your Maven to include the required jar file selenium-java-x.x.x.jar.

version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version> </properties> <dependencies> <dependency> <groupId>org.4</junit.2</slf4j.? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <properties> <junit.x.0</selenium.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.5. .seleniumhq.x.7.version>2.version>1.jar will transitively bring in the other relevant driver jars for different browsers.version> <slf4j.version> <selenium.selenium</groupId> <artifactId>selenium-java</artifactId> <version>${selenium.version}</version> </dependency> </dependencies> The selenium-java-x.version}</version> </dependency> <dependency> <groupId>org.version>4.

and not in all JUnit test cases where a paricular element is used.SystemTestPage. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.selenium.By.selenium. import org. import org.openqa.WebElement.systemtest.systemtest. which is page object.STEP 2: It is a best practice to have separate page objects as these page objects can be shared by multiple JUnit test cases.openqa.openqa.CacheLookup. protected SystemTestPage(WebDriver driver) { this.openqa.selenium. If a particular page element changes. public class SystemTestPage { protected WebDriver driver. import org.wrap. you will have to change it only in one import fsg. public class LoginPage extends SystemTestPage { @FindBy(name = "username") @CacheLookup private WebElement userNameInput.driver = } } You can have your page objects extend the above generic SystemTestPage class. import org.openqa. The JUnit test cases will depend on the page objects and should not refer to the page elements directly.WebDriver.selenium. import org.systemtest.FindBy. import org. ? 1 2 3 4 5 6 7 8 9 10 11 12 package @FindBy(name = "password") @CacheLookup .

sendKeys(password). @FindBy(xpath ="//input[@type=###BOT_TEXT###quot;submit###BOT_TEXT###quot;]") @CacheLookup private WebElement submitButton. These JUnit test cases can be run to test your web . import org.openqa.systemtest. import import org. String password) { userNameInput.WebDriverWait.sendKeys(userName).Before.junit.xpath("//td[text()=" + userName + "]")). import junit.framework.FirefoxDriver.openqa. import com. private static final String USER_NAME = "John".getText().selenium.Test.openqa.20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 private WebElement passwordInput. } } STEP 3: Write the JUnit class using the page objects defined above and assert the ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com. userNameInput. submitButton.ui.After. import org. import org. return element.selenium.unittests. import org. } public String getUserName(String userName) { WebElement element = driver.findElement(By. public class LoginTest { private static final String BASE_URL = "http://localhost:7001/login/login. public LoginPage(WebDriver driver){ super(driver).selenium.Assert.junit.selenium.htm". } public void login(String userName. import org.

findElement(By.getUserName(USER_NAME)). loginPage. This uses an XPath expression to locate the element. } @After public void tearDown() { driver. LoginPage. // the page object @Before public void setUp() { driver = new FirefoxDriver(). private LoginPage loginPage. } The XPath expression will be something like //td[[text()=John] which looks for a td element with text value "John".assertEquals(MAC. return element. What are selenium locators? What tools do you use to locate them? A.getText(). private WebDriverWait wait. } } Q. Selenium Locators are the way of finding the HTML element on the page to perform a Selenium action on.xpath("//td[text()=" + userName + "]")). wait = new WebDriverWait(driver. The annotation in the above example is also a locator by name as shown below ? . PASSWORD). } @Test public void testLogin() throws Exception { loginPage.19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 private static final String PASSWORD = "aa44" private WebDriver driver.get(BASE_URL). Assert. driver. 5). The example above has a line asshown below to extract the username element from the Login response page.class). ? 1 2 3 4 public String getUserName(String userName) { WebElement element = driver. loginPage = PageFactory.login(USER_NAME.quit().initElements(driver.

y . which is a Firefox plugin useful in identifying the locators. id. Selenium IDE. etc. There are handy tools to identify the HTML elements or locators.1 2 3 @FindBy(name = "username") @CacheLookup private WebElement userNameInput. css. etc. debugging. This will match the HTML snippet ? 1 <input type="text" name="username"> You could also find by tagName.

then the test cases may fail if the IDs have changed. Google chrome provides a similar functionality to inspect element out of the box.The Fire-bug plugin. The ClickAndWait action will not work for AJAX calls. y Q. The use of the locators need to be carefully thought through. So. if IDs are used in your tests. For example. So. and use "waitForElement" instead. what are some of the challenges with Selenium? A. Unmaintainable automated test cases can take more time than manual tests. In your experience. In general. the biggest complaint is about writing test cases that are not maintainable. which allows you to inspect the elements by right clicking on the page and then selecting "Inspect Element". badly written test cases whether junit tests or web tests. . it is important to write quality test cases by clearly separting the page objects from the test cases as demonstrated in the Q&A above. some frameworks like JSF dynamically generate HTML element IDs. The solution to this problem is to use XPath to find the relevant HTML elements.