我有一个要求,我想用Selenium测试下面的场景:
以下是我正在做的事情。但没有成功..
@Test
@InSequence(1)
public void addTestData() {
Warp.initiate(new Activity() {
@Override
public void perform() {
OurPage page = OurPage.on(selenium).withPath(SetupTestData.PATH);
page.addTestDataFor(REFRESH_SHOULD_WORK);
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1L;
});
}
@InSequence(2)
@Test
public void refreshShouldWork() {
Warp.initiate(new Activity() {
@Override
public void perform() {
CountryDetailsPage page = new CountryDetailsPage(selenium).withPath(CountryDetailsPage.PATH);
page.open();
System.out.println("*** PAGE OPENED ****");
waitGui().until().element(page.table(PROGRESSES_TABLE)).is().present();
assertThat(page.table.row(FIRST).cell(FIFTH).text(), is("10"));
// FIXME: Need to find out the way to change the value
page.refresh().click();
System.out.println("*** CLICKED REFRESH ****");
waitGui().until().element(page.table(PROGRESSES_TABLE)).is().present();
assertThat(page.table.row(FIRST).cell(FIFTH).text(), is("20"));
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1L;
@AfterPhase(RENDER_RESPONSE)
public void beforeRenderResponse() {
System.out.println("*** AFTER RENDER RESPONSE****");
}
@AfterPhase(Phase.APPLY_REQUEST_VALUES)
public void afterRenderResponse() {
System.out.println("*** AFTER APPLY REQUEST VALUES ****");
}
});
}
如果您有任何想法我怎么能实现这一目标,请告诉我。感谢
答案 0 :(得分:0)
由于Selenium仅自动化UI,您应该让不同的技术处理数据库。 Selenium本身无法连接到任何数据库并更改您想要的内容。
您可以使用Java来执行此操作。更具体地说,使用Java数据库连接(JDBC)。
以下是Vogella uses
的示例 try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);
// preparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
// parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();
...
} catch (Exception e) {
throw e;
} finally {
close();
}
}
更新上面的代码来更新实际的mysql字段,你会变得很好。虽然我不认为这是一个很好的测试,但同样,UI应该进行更改,Selenium应该检测它。这可能是集成测试的一个很好的候选者。不是Selenium测试。