Your Logo
chapter 2
Two Minute Tutorial
- Have a Selenium enabled project
- Add the seleniumdsl.jar to your classpath
- Start the Selenium server
- On following tests we are going to use this HTML
<html>
<head>
<title>Login</title>
</head>
<body>
<table id="table">
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>cell_1_1</td>
<td>cell_1_2</td>
</tr>
<tr>
<td>cell_2_1</td>
<td>cell_2_2</td>
</tr>
</tbody>
</table>
<div id="errors">
<!-- populated on invalid login -->
</div>
<form id="form" action="main.html">
Login: <input type="text" name="login"/><br/>
Password: <input type="password" name="password"/><br/>
Remember? <input type="checkbox" name="remember"/>
</form>
</body>
</html> - Run the following test
public class SeleniumTestCase {
private static Selenium selenium;
private Browser browser;
@BeforeClass
public static void beforeStartup() {
selenium = new DefaultSelenium("localhost", SeleniumServer.getDefaultPort(), "*firefox",
"http://localhost:8080/");
selenium.start();
selenium.setContext("Selenium DSL testing");
selenium.setBrowserLogLevel(SeleniumLogLevels.DEBUG);
}
@AfterClass
public static void afterShutdown() {
selenium.stop();
}
@Before
public void setUp() {
browser = new DefaultBrowser(selenium);
browser.open("seleniumdsl/mypage.html");
}
@Test
public void testTableRowCount() {
Table table = browser.currentPage()
.table("table");
Assert.assertEquals(table.getRowCount(), 3);
}
@Test
public void testTableColCount() {
Table table = browser.currentPage()
.table("table");
Assert.assertEquals(table.getColCount(), 2);
}
@Test
public void testTableHeaderLabel() {
Table table = browser.currentPage()
.table("table");
// indexes start at 1
Assert.assertEquals(table.header()
.get(1)
.headerValue(), "First Column");
}
@Test
public void testTableContent() {
Table table = browser.currentPage()
.table("table");
// second row, column named "First Column"
// value() returns null if the cell isn't found
Assert.assertEquals(table.cell(1, "First Column")
.value(), "cell_1_1");
// or
Assert.assertTrue(table.cell(1, "First Column")
.contains("cell_1_1"));
}
@Test
public void testLogin() {
Form form = browser.currentPage()
.form("form");
form.field("login")
.type("myLogin");
form.field("password")
.type("myPassword");
form.check("remember");
form.submit();
// logged successfuly
Assert.assertEquals("Main Page", browser.currentPage()
.title());
}
@Test
public void testInvalidLogin() {
Form form = browser.currentPage()
.form("form");
form.field("login")
.type("invalid_login");
form.field("password")
.type("invalid_password");
form.submit();
Page page = browser.currentPage();
Assert.assertEquals("Login", page.title());
Assert.assertTrue(page.div("errors")
.contains("Invalid login!"));
Assert.assertTrue(form.isChecked("rememeber"));
// ensures that the form remains with the previously typed login
Assert.assertEquals(form.field("login")
.content(), "invalid_login");
// or
Assert.assertTrue(form.field("login")
.contains("invalid_login"));
}
}

