Selenium Interview Questions and Answers
1. Introduction
In this example we shall show the most popular interviews questions about Selenium tools and give you exhaustive answers. Questions cover all topics:
- Selenium 1
- Selenium 2
- Selenium IDE
- Selenium Standalone Server
In this article is shown theoretical questions and best practices, which is used by Selenium communities.
2. Interview Questions and Answers
- What is Selenium? Selenium is the complex of tools for automation testing the web applications. This tools is developed in Java. You can use different approaches for create the tests: using Selenium IDE, using one of popular programming languages (Java, C#, Perl, Python). Because right now web applications become very interactive it is to difficult to write automation tests for its. Selenium uses two kind of approaches for doing it: directly using browser api (new version), injecting JavaScript (old version).
- What modules does Selenium consist? Selenium has few different modules:
- Selenium IDE – firefox plugin, The plugin has two modes : records user activities, run the tests by user activities.
- Selenium 1 (Selenium RC) – library, which converts the code to JavaScript and injects it to the browser.
- Selenium 2 (Selenium WebDriver) – library, which uses the browser api for testing.
- Selenium Server (Selenium Grid) – server. It is used to run tests in different environments.
- What the technology is used for searching elements in Selenium? Selenium is used DOM for searching elements. The DOM can look like the Tree Structure.
- How to find different type of elements in Selenium, explain few different ways? Let’s consider this question by simple HTML example
index.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example page for Selenium Test</title> </head> <body> <iframe name="tree" src="tree.html"></iframe> <div id="general"> <div class="form"> <form about="/" method="POST"> User name : <input name="username" type="text"> <a href="/agreements.html">User agreements</a> </form> </div> </div> </body> </html>
You can search the elements by different criterias:
- By Element ID
WebElement element = driver.findElement(By.id("general"));
- By Class name
WebElement element = driver.findElement(By.className("form"));
- By Tag name
WebElement element = driver.findElement(By.tagName("iframe"));
- By Name
WebElement element = driver.findElement(By.name("username"));
- By Link Text
WebElement element = driver.findElement(By.linkText("User agreements"));
- By Partial Link Text
WebElement element = driver.findElement(By.partialLinkText("agreements"));
- By XPATH
List inputs = driver.findElements(By.xpath("//input"));
- Using JavaScript
(WebElement) ((JavascriptExecutor)driver).executeScript("return $('.general')[0]");
- By Element ID
- How to fill different type of input elements? Let’s consired another HTML page:
create.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create new user</title> </head> <body> <form action="/create" method="post"> User name :<input type="text" name="username"> Agreement : <input type="checkbox" name="agreement"> Sex : <input type="radio" name="sex" value="male">Male <input type="radio" name="sex" value="female">Female Country : <select name="country"> <option value="1">Country 1</option> <option value="2">Country 1</option> </select> Description : <textarea name="desc"></textarea> <input id="submit" type="button" value="Submit"> </form> </body> </html>
The main approach looks like : get necessary elements, fill the data.
/** * Test filling the form with different input elements. */ public void fillForm() { //fill the user name driver.findElement(By.name("username")).sendKeys("Petr"); //checked agreement driver.findElement(By.name("agreement")).click(); //choose sex driver.findElements(By.name("sex")).get(0).click(); //select country WebElement select = driver.findElement(By.tagName("select")); select.findElements(By.tagName("option")).get(0).click(); //fill description driver.findElement(By.name("desc")).sendKeys("Petr"); //push on button driver.findElement(By.id("submit")).click(); }
- How to switch between pupops, frames, windows? Let’s imagine that user opens the two windows
index.html
andcreate.html
and we need to switch between them. You can do it by pointing the title of necessary windows. For example :driver.switchTo().window("Create new user");
We can use this approach to switch frames by its name. For exampledriver.switchTo().frame("tree");
- What is Selenium IDE? When is it used? Selenium IDE is firefox plugin. It has frendly user interface. It is used for developing the automation tests. Selenium community recommend to start to learn Selenium with this tools. The use cases look like to record user activites. IDE will gerenete the test code and after that you can run this tests. First of all you need to install the firefox plugin. It looks like below:
After you get your test cases code you can export it to favotire programming languages : Java, Ruby, Python, C#. It looks like below:
After that you get the source code file then you can open in your favorite IDE. The code should look similar :
JavaCodeGeeksTests.java
package com.example.tests; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class JavaCodeGeeksTests { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://www.javacodegeeks.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testJavaCodeGeeksTests() throws Exception { driver.get(baseUrl + "/"); driver.findElement(By.linkText("I reject the FREE eBooks")).click(); driver.findElement(By.cssSelector("span.foobar-close-button")).click(); new Select(driver.findElement(By.id("top-menu-mob"))).selectByVisibleText("regexp:\\s+–\\s+–\\sJava Tutorials"); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }
- What is implicit and explicit wait? The all web apps load and fetch the data from server. sometimes it could not be faster. for this reason the test case should wait some events which should be happended on page. Selenium provides two kind of method to do this job.
- explicit wait – we don’t know how manytimes the event should take so we predict some changes in UI.
For example load the new elements :(new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.id("comments")));
here we pointed30
– timeout in milliseconds, if it exceeds the test will be interuppeted.
ExpectedConditions.presenceOfElementLocated(By.id("comments"))
– expect the show new div block withid="comments"
- implicit wait – we predict that event takes definalty times. For example
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- explicit wait – we don’t know how manytimes the event should take so we predict some changes in UI.
- What kind of tests does selemiun support? Selemiun supports variety tests cases:
- Functional tests
- Regressive tests
- Testing Static Content
- Testing Links
- Testing Dynamic Elements
- Ajax Tests
- What is UI Map? This is development approach when we separate tests data and login codes. For example we have some credential to auth in web site. we create the special class where we should store all such data and use it in tests codes. For example:
UserCredential .java/** * Credential info. * @author parsentev * @since 26.11.2015 */ public class UserCredential { public static final String LOGIN = "login"; public static final String PASSWORD = "password"; }
So when you need to change the user credential, you go to this file and change only this code without searching all places where you use it.
- What is Page Object Pattern? How is it used in Selenium? This pattern is used in automation tests overall. The main advantages are hiding the implementation of auth details and splitting the all web apps tests on small undescended pages.
- What is DDT and how is it supported in Selenium? DDT is the data tests. Selenium does not have special mechanism for working with DDT. You can use another libraries for it. For example, read data from database by JDBC, or read data from file and so on.
- What is Selenium Grid? Where is it used? Selenium Grid is distributed servers which offers to run tests in different enviroments.
- How to execute the Selenium tests in parallel? It can be done by Selenium Gird or by using Thread in Java directly.
- What kind of problems do you have when using Selenium? First, it is not so faster to change the automation tests when UI in web app can be changed faster. However, these problems have all automation testing tools. Another problem is appear in dynamic generation IDs. If you use strategy search by ID you should change it to XPATH. One another problem is working with AJAX. Selenium supports AJAX executing but tester should predict how many times the ajax call takes. You can use explicit or implicit wait for solve with problem.
- Can Selenium be integrated to CI? Yes. It can, because all tests can be exported to programming languages and then it can be run independent from Selenium IDE by JUnit, TestNG and similar libraries
- Can you use Selenium in commercial product? Yes, you can. Selenium is distributed by Apache 2.0 license. It means that you can use it in commercial products free without paing any charge.
- What kind of disadvantages does Selenium IDE have? After you recorded the user activates Selenium IDE generates the case tests tables. This tests table does not support the loops, functions, conditions statements, so you cannot use all programming languages constructions.
- What is Selenium Remote Control? It is the test tool for web apps. It uses injecting JavaScript to browser for testing.
- What advantages does Selenium have for compare with another web testing tools? First, it is to start to write the tests, because you can record the user activity directly, Another thing is Selenium supports many programming languages : Java, C#, Python, Ruby.
- Can you install the Selenium IDE to another browsers then FireFox? Not. Selenium IDE can be installed only to FireFox.
- What kind of browsers can be executed the tests by Selenium?
- Firefox 3.x
- IE 6-8
- Safari 2-4
- Opera 8-10
- Chrome
- Can you use Selenium for testing mobiles app? Yes. if it can be run in mobiles browsers.
- What is Selenese? After you recorded the user activities, Selenium IDE generates the code, this code is written by Selenese.
- How can you run the tests in HTTPS? You need to use Selenium Server and configure the necessary securities environments.
- How can you configure the Selenium Grid? You can do in two ways: 1. adding special key when run the server, 2. using JSON config file.
- What is HtmlUnitDriver?HtmlUnitDriver is web driver which is used HtmlUnit engine. It is the faster web driver in Selenium.
- Can you use navigations browser menu in Selenium tests? Yes. you can. You can handle it by follow code
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.navigate().to();
- How can you save the tests result? You can take the screen shot if tests fail or you can store the testing result to database by JDBC.
- Can you run the load tests by Selenium? Yes, but Selenium is not good choose for such kind of tests. Because it is expense to open the multiple browser at the same time. if you need to test loading, you can use JMeter or Gattling.
3. Conclusion
In this acticle we explained most of frequently ask questions in interviews and gived the answers. Of course this is not all. if you want to improve your knowledge about Selemiun go to official web site Selenium Official Documentation.
Great post! Thanks for sharing the valuable Selenium Interview Questions And Answers. This article will help me to prepare for the interviews.Thanks a lot for sharing this list. very informative and useful article. keep sharing!