Using Selenium with TestNG
Selenium is a framework that makes it easy to run functional / acceptance tests on your web applications.
One advantage of using Selenium programmatically with a framework like TestNG is that it allows you to create intelligent fixtures, which is challenging to do with Fit-style tables. TestNG is especially a good match for Selenium because it enables you to do somethings that aren't possible using other frameworks, such as test dependencies, rerun failed tests, and set up parametric tests with parameters defined in separate files.
public class CreateWidgetUATest {
private Selenium driver;
@Parameters({"selen-svr-addr","brwsr-path","aut-addr"})
@BeforeClass
private void init(String selenSrvrAddr, String bpath, String appPath) throws Exception {
driver = new DefaultSelenium(selenSrvrAddr, SeleniumServer.getDefaultPort(), bpath, appPath);
driver.start();
}
@Parameters({"aut-addr"})
@Test
public void verifyCreate(String appPath) throws Exception {
driver.open(appPath + "/CreateWidget.html");
driver.type("widget", "book-01");
driver.select("type","book");
driver.type("definition", "book widget type book");
driver.click("submit");
assertEquals(driver.getText("success"), "The widget book-01 was successfully created.", "test didn't return expected message");
}
@Parameters({"aut-addr"})
@Test
public void verifyCreationError(String appPath) throws Exception {
driver.open(appPath + "/CreateWidget.html");
driver.type("widget", "book-02");
driver.select("type", "book");
// definition explicitly set to blank
driver.type("definition","");
driver.click("submit");
assertEquals(driver.getText("failure"), "There was an error in creating the widget.", "test didn't return expected message");
}
@AfterClass
private void stop() throws Exception {
driver.stop();
}
}
Now link the parameter names with values in testng.xml file:
<parameter name='selen-svr-addr' value='localhost' />
<parameter name='aut-addr' value='http://localhost:8080/gt15/' />
<parameter name='brwsr-path' value='*firefox' />
DbUnit fixture for a collection of test:
public class DatabaseFixture {
@Parameters({"seed-path","db-driver","db-url","db-user","db-psswrd")
@BeforeTest
public void seedDatabase(String seedpath, String driver, String url, String user, String pssword) throws Exception {
IDatabaseConnection conn = this.getConnection(driver, url, user, pssword);
IDataSet data = this.getDataSet(seedpath);
try {
DatabaseOperation.CLEAN_INSERT.execute(conn, data);
} finally {
conn.close();
}
}
private IDataSet getDataSet(String path) throws IOException, DataSetException {
return new FlatXmlDataSet(new File(path));
}
private IDatabaseConnection getConnection(String driver, String url, String user, String pssword ) throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection jdbcConnection = DriverManager.getConnection(url, user, pssword);
return new DatabaseConnection(jdbcConnection);
}
}
Next, associate real values for these parameters in testng.xml:
<parameter name='seed-path' value='test/conf/gt15-seed.xml' />
<parameter name='db-driver' value='org.hsqldb.jdbcDriver' />
<parameter name='db-url' value='jdbc:hsqldb:hsql://127.0.0.1' />
<parameter name='db-user' value='sa' />
<parameter name='db-psswrd' value='' />
page_revision: 5, last_edited: 1226195550|%e %b %Y, %H:%M %Z (%O ago)





