First look at Selenium Inspector
Last week, Team Dev released Selenium Inspector – a Java API wrapper over the existing Selenium Java API. Note that Selenium Inspector API does not replace the Selenium Java API, you can use both at the same time! This article compares the same program written in 3 different ways: Selenese (HTML) using Selenium IDE, in Java using Selenium Java API and in Java using Selenium Inspector API.
The Test
The test goes to google.com and searches for the keyword “yahoo”. It then makes sure that the link “Yahoo!” appears in the search results.
Approach 1: Using Selenium IDE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="" />
<title>helloworld</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr>
<td rowspan="1" colspan="3">helloworld</td>
</tr>
</thead>
<tbody>
<tr>
<td>openAndWait</td>
<td>http://www.google.com</td>
<td></td>
</tr>
<tr>
<td>assertElementPresent</td>
<td>q</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>q</td>
<td>yahoo</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>btnG</td>
<td></td>
</tr>
<tr>
<td>assertElementPresent</td>
<td>link=Yahoo!</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
Approach 2: Using Selenium API
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
public class HelloWorldSeleniumTestCase {
private static DefaultSelenium selenium;
private static SeleniumServer seleniumServer;
@BeforeClass
public static void setUp() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
selenium.start();
}
@Test
public void testHelloWorld() {
selenium.open("http://www.google.com");
Assert.assertTrue(selenium.isElementPresent("q"));
selenium.type("q", "yahoo");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
// assert that the search box remembers the query
Assert.assertEquals("yahoo", selenium.getValue("q"));
// assert that you find the expected link in results
Assert.assertTrue(selenium.isElementPresent("link=Yahoo!"));
}
@AfterClass
public static void tearDown() {
selenium.stop();
}
}
Approach 3: Using Selenium Inspector API
You need junit 4.5 to run. If you are using Junit 4.3 or lower, you will get the following error.
Caused by: java.lang.ClassNotFoundException: org.junit.runners.BlockJUnit4ClassRunner at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
JUnit code
import org.junit.BeforeClass;
import org.junit.Test;
import org.seleniuminspector.ElementByLocatorInspector;
import org.seleniuminspector.ElementInspector;
import org.seleniuminspector.SeleniumFactory;
import org.seleniuminspector.SeleniumHolder;
import org.seleniuminspector.SeleniumTestCase;
import org.seleniuminspector.SeleniumWithServerAutostartFactory;
import org.seleniuminspector.ServerLoadingMode;
import org.seleniuminspector.html.InputInspector;
public class HelloWorldSeleniumInspectorTestCase extends SeleniumTestCase {
@BeforeClass
public static void initSeleniumFactory() {
SeleniumHolder seleniumHolder = SeleniumHolder.getInstance();
SeleniumFactory factory = new SeleniumWithServerAutostartFactory(4444, "*firefox", "http://www.google.com");
seleniumHolder.setSeleniumFactory(factory);
}
@Test
public void submitData() {
openAndWait("http://www.google.com/");
InputInspector inputField = new InputInspector("q");
inputField.assertElementExists();
inputField.type("yahoo");
getSelenium().click("btnG");
ServerLoadingMode.getInstance().waitForLoad();
// assert that the search box remembers the query
inputField.assertValue("yahoo");
// assert that you find the expected link in results
ElementInspector elementInspector = new ElementByLocatorInspector("link=Yahoo!");
elementInspector.assertElementExists();
}
}
Comparison
Selenium API is action oriented, various actions that you can perform with the selenium API. There is a roughly one to one mapping of Selenium IDE commands and Selenium Java API. However, Selenium Inspector is based around objects called “Inspectors”. Inspectors are objects that inspect a specific things of the page, e.g. InputInspector which inspects an input field. Inspector then provide you with easy high level api. For example Input Inspector provides the following high level commands:
Class InputInspector
d |
assertValue(java.lang.String value) – Assert that the input has the given value |
boolean |
disabled() – Assert that the input is disabled |
java.lang.String |
name() – Get the name of the input |
java.lang.String |
type() – Get the type of the input |
void |
type(java.lang.String text) – Type of given text in the input |
void |
typeKeys(java.lang.String text) – Type the given keys in the input |
java.lang.String |
value() - Get the value of the input |
The idea is to provide easy to functions in addition to the existing Selenium Java API.
Some of the other inspectors are:
- ElementInspector – This is the base class for most of the other Inspectors. The purpose of ElementInspector is to provide easy means for inspecting client-side DOM elements in Selenium functional tests. An instance of ElementInspector is bound to a specific DOM node that is specified during the element’s creation.
- ElementByExpressionInspector – Defines an inspector for an element retrieved with an associated script. Note that like for Selenium.getEval method, the script runs in context of “selenium” object. Use “window” to refer to the tested window.
- ElementByLocatorInspector – Inspects an element by the specified Selenium locator.
- ElementByReferenceInspector – Inspects the same element as the associated element inspector. This class is mainly useful for extending by other specialized inspectors to provide a flexible way of referring to other elements. Some of the available extensions are InputInspector, LiInspector, TableInspector, TableRowInspector, TableSectionInspector, TextAreaInspector, UlInspector
- Other inspectors – DocumentInspector, TableCellInspector, TableColumnInspector, SubElementByPathInspector, SubElementByExpressionInspector, WindowInspector
- JSF inspectors – the Selenium Inspector websites talked about OpenFaces Inspectors — a package of selenium inspectors created for easier testing of OpenFaces JSF components. I haven’t taken a look at it but I think the idea is to encapsulate and reuse JSF input specific behavior in a [Inspector] class and reuse!
Conclusion
Inspectors try to provide high level functionality to various elements in the page to provide ease of use and facilitate code reuse!
Reference
Official Selenium Inspector website
Related posts:




Nice work Vineet. Thanks for the post.
Can i genreate html case from a junit ( which is genreated by selinium?)
You cannot convert a junit test case to a Selenese HTML test. You can however use Selenium IDE to write an HTML test and export it to junit.
Great Informative article on Selenium Inspector. Thanks for Sharing.
Please watch my videos on Selenium in my website. Any feedback is appreciated.
http://whiteboxqa.com/#selenium.php