Google

Oct 24, 2011

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.
  • 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.
  • 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

...
<table>
     <tr>
    <td>John</td><
  /tr>
</table>
... 


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

<properties>
 <junit.version>4.4</junit.version>
 <selenium.version>2.7.0</selenium.version>
 <slf4j.version>1.5.2</slf4j.version>
</properties>

<dependencies>
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>${selenium.version}</version>
 </dependency>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${junit.version}</version>
 </dependency>
 <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>${slf4j.version}</version>
 </dependency>
</dependencies>

The selenium-java-x.x.x.jar will transitively bring in the other relevant driver jars for different browsers.





STEP 2: It is a best practice to have separate page objects as these page objects can be shared by multiple JUnit test cases. If a particular page element changes, you will have to change it only in one place, which is page object, and not in all JUnit test cases where a paricular element is used. The JUnit test cases will depend on the page objects and should not refer to the page elements directly.


package myapp.systemtest;

import org.openqa.selenium.WebDriver;

public class SystemTestPage {
    
    protected WebDriver driver;

    protected SystemTestPage(WebDriver driver) {
        this.driver = driver;
    }
} 


You can have your page objects extend the above generic SystemTestPage class.

package com.systemtest.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;

import myapp.systemtest.SystemTestPage;


public class LoginPage extends SystemTestPage {
       
    @FindBy(name = "username")
    @CacheLookup
    private WebElement userNameInput;
 
 @FindBy(name = "password")
    @CacheLookup
    private WebElement passwordInput;

    @FindBy(xpath ="//input[@type=\"submit\"]")
    @CacheLookup
    private WebElement submitButton;
    
    public LoginPage(WebDriver driver){
        super(driver);
    }
    
    public void login(String userName, String password) { 
        userNameInput.sendKeys(userName);
  userNameInput.sendKeys(password);
        submitButton.submit();
    }
    
    public String getUserName(String userName) { 
        WebElement element = driver.findElement(By.xpath("//td[text()=" + userName + "]"));
        return element.getText();
    }
}


STEP 3: Write the JUnit class using the page objects defined above and assert the results. These JUnit test cases can be run to test your web pages.

package com.unittests;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.systemtest.page.LoginPage;

public class LoginTest {
    
    private static final String BASE_URL = "http://localhost:7001/login/login.htm";
    private static final String USER_NAME = "John";
 private static final String PASSWORD = "aa44"
    
    private WebDriver driver;
    private WebDriverWait wait;
    private LoginPage loginPage; // the page object
    
    @Before
    public void setUp() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 5);
        driver.get(BASE_URL);
        loginPage = PageFactory.initElements(driver, LoginPage.class);
    }
    
    @Test
    public void testLogin() throws Exception {
        loginPage.login(USER_NAME, PASSWORD);
        Assert.assertEquals(MAC, loginPage.getUserName(USER_NAME));
    }

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





Q. What are selenium locators? What tools do you use to locate them?
A. Selenium Locators are the way of finding the HTML element on the page to perform a Selenium action on. The example above has a line asshown below to extract the username element from the Login response page. This uses an XPath expression to locate the element.

public String getUserName(String userName) { 
    WebElement element = driver.findElement(By.xpath("//td[text()=" + userName + "]"));
    return element.getText();
}

The XPath expression will be something like //td[[text()=John] which looks for a td element with text value "John".

The annotation in the above example is also a locator by name as shown below

@FindBy(name = "username")
@CacheLookup
private WebElement userNameInput;   

This will match the HTML snippet

<input type="text" name="username">

You could also find by tagName, id, css, etc.


There are handy tools to identify the HTML elements or locators.

  • Selenium IDE, which is a Firefox plugin useful in identifying the locators, debugging, etc.

  • The Fire-bug plugin, which allows you to inspect the elements by right clicking on the page and then selecting "Inspect Element". Google chrome provides a similar functionality to inspect element out of the box. 


Q. In your experience, what are some of the challenges with Selenium?
A. In general, badly written test cases whether junit tests or web tests, the biggest complaint is about writing test cases that are not maintainable. Unmaintainable automated test cases can take more time than manual tests. So, 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. The use of the locators need to be carefully thought through. For example, some frameworks like JSF dynamically generate HTML element IDs. So, if IDs are used in your tests, then the test cases may fail if the IDs have changed. The solution to this problem is to use XPath to find the relevant HTML elements. The ClickAndWait action will not work for AJAX calls, and use "waitForElement" instead.



Labels:

20 Comments:

Blogger raju said...

Thanks for the very clear question and answers, very helpful
Raj

12:26 AM, July 14, 2012  
Blogger Unknown said...

Glad that you found it useful.

11:48 AM, July 14, 2012  
Anonymous Anonymous said...

Very useful

Many thanks!

2:49 AM, July 25, 2012  
Anonymous Anonymous said...

Very precise and informative.

Thanks a lot

11:00 PM, August 01, 2012  
Anonymous Anonymous said...

Very Nice.

5:04 PM, August 10, 2012  
Anonymous Testing interview questions said...

This is really a great blog, might you be involved in doing an interview about how you created it? If so e-mail me!

3:19 AM, August 18, 2012  
Anonymous Anonymous said...

This questions and answers are very useful for us Thanks for providing these....

2:59 PM, August 23, 2012  
Blogger Satishvegi said...

Given date is clear but need little more information regarding selenium RC with functions, frame work and web driver too.
If you can explain your frame work it will be more helpful

9:48 PM, August 23, 2012  
Anonymous Anonymous said...

Very clear. How would the page objects be used with a properties file that would contain the user name and password?

12:02 AM, September 05, 2012  
Blogger Unknown said...

Good .. keep posting some more questions

11:18 PM, September 13, 2012  
Anonymous Anonymous said...

good

11:41 PM, October 16, 2012  
Blogger Unknown said...

Can anyone help me to write a framework to test : Searching for a particular string by iterating thorugh result page.Once found 'CLICK' on the link and verify the resulting page .

For this a framework has to be set in SELEINIUM either by PHP scripts or JAVA.One should PAGE OBJECTS MODEL, USE DIFFERNT LEVELS OF ABSTRACTION, to represnt actions, locators, data and TEST.

Kindly help me ASAP.

Regards

12:40 AM, November 16, 2012  
Anonymous Anonymous said...

thank you So much..i am sure this wil be really helpful

8:59 AM, July 22, 2013  
Anonymous Anonymous said...

It is very helpful. Thank you.

Isn't it passwordInput.sendKeys(password); at line number 32 of LoginPage class?

2:57 AM, October 21, 2013  
Blogger ruchi said...

for more selenium interview questions ans answers.
please visit-http://www.studyselenium.com/2013/12/100-selenium-interview-questions.html

6:39 PM, January 25, 2014  
Blogger Unknown said...

For more Selenium interview questions please visit http://seleniumrocking.com/interview-questions

3:44 PM, May 03, 2014  
Blogger sravan said...

Hi friends for more selenium interview questions/programs please go through this blog

http://selenium4freshers.blogspot.in/

3:16 AM, May 05, 2014  
Blogger Unknown said...

thanku for informing ruchi goyal .it is very usefull for us

3:05 AM, September 30, 2014  
Blogger Unknown said...

I was totally amazed when i saw this website Best testing tools Online Training first time i thought this is what i am looking for from a long time i am very thankful to you for helping not only me but to all those guys who are new to this IT SECTOR and who wants to make a career in this sector.

12:58 AM, October 01, 2014  
Anonymous Anonymous said...

Though these are useful, they are not enough to crack an automation testing interview, they ask questions for few scenarios, like ... how will u sort in a table, how will you write a forloop for a particular scenario, annotations, methods,these days interviews are based on concepts and coding.

7:58 AM, October 24, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home