I recently finished setting up automated test framework using JBehave, Selenium and Page Object Pattern. In the process, I came cross several posts that helped me along the way. However, I was not able to find any implementation of class Page or how to use SeleniumSteps. This post is here to close the gap.
The scenario I am going to cover is a user logging in to mytest.com.
The first step is to write the file named user_login_successfully.txt to describe the scenario in plain English.
"Given the user is on the login page
When the user enters username me
And the user enters password password
And the user submits their login
Then the user is on the home page"
The next step is to create a UserLoginSuccessfully.java to represent the scenario.
"public class UserLoginSuccessfully extends org.jbehave.scenario.Scenario{
public UserLoginSuccessfully() {
super(new LoginSteps());
}
}"
Next is LoginSteps.java which executes the scenario step by step.
"public class LoginSteps extends org.jbehave.web.selenium.SeleniumSteps {
private Page currentPage;
protected Selenium createSelenium() {
return new DefaultSelenium("localhost", 4444, "*firefox",
"http://mytest.com");
}
@Given("that the user is on the login page")
public void notLogin() throws Exception {
currentPage = new LoginPage(selenium);
currentPage.open();
assertTrue(currentPage.verifyTitle());
}
@When("the user enters username $username")
public void setUserName(String username) {
loginPage().setUserName(username);
}
@When("the user enters password $password")
public void setPassword(String password) {
loginPage().setPassword(password);
}
@When("the user submits their login")
public void submitLogin() {
loginPage().login();
}
@Then("the user is on the homepage")
public void homePage() {
currentPage = new HomePage(selenium);
assertTrue(currentPage.verifyTitle());
}
private LoginPage loginPage() {
return (LoginPage) currentPage;
}
}"
Now is the time to implement Page Object Pattern. Page is a POJO.
"public abstract class Page {
protected Selenium selenium;
public Page(Selenium selenium) {
this.selenium = selenium;
}
public void open() {
selenium.open(getUrl());
}
public abstract String getUrl();
public abstract String getTitle();
public boolean verifyTitle() {
return getTitle().equals(selenium.getTitle());
}
}"
LoginPage.java and HomePage.java extend Page.java. If the id of the text field of username in the login page changes, you only need to modify one place in LoginPage.java. This is the reason that Page Object Pattern is so powerful.
"public class LoginPage extends Page {
public LoginPage(Selenium selenium) {
super(selenium);
}
public String getUrl() {
return "/login/login.html";
}
public String getTitle() {
return "Login - MyTest";
}
public void setUserName(String username) {
selenium.typeKeys("j_username", username);
}
public void setPassword(String password) {
selenium.typeKeys("j_password", password);
}
public void login() {
selenium.click("j_submit");
}
}"
"public class HomePage extends Page {
public HomePage(Selenium selenium) {
super(selenium);
}
public String getUrl() {
return "/home/index.html";
}
public String getTitle() {
return "Welcome - Test";
}
}"
You are done. Just start the Selenium server, you can run the test as a junit test in your choice of IDE and it should work like a charm.
Here are the links to the posts that helped me along the way.
http://blog.m.artins.net/acceptance-tests-with-jbehave-selenium-page-objects/
http://www.bitmotif.com/java/jbehaveselenium-example/
http://code.google.com/p/selenium/wiki/PageObjects
No comments:
Post a Comment