In a previous post I went through the mechanics of creating a simple JBehave test. This time around, I want to do something a little more “real”.
So, let’s make another simple example that uses Selenium. That way, we have real feel for the mechanisms we would use.
We’ll start once again with our scenario:
Given I am on “http://www.bitmotif.com”
When I click on link “Test Page For Selenium Remote Control”
Then the title is “Bit Motif ยป Test Page For Selenium Remote Control”
This scenario needs to be saved in a file named “navigation_scenario”.
Using the same approach we took in the previous post, we’ll create our scenario:
package com.bitmotif.jbehaveexamples.part.two;
import org.jbehave.scenario.Scenario;
public class NavigationScenario extends Scenario {
public NavigationScenario() {
super(new NavigationScenarioSteps());
}
}
And, we’ll create our scenario steps:
package com.bitmotif.jbehaveexamples.part.two;
import org.jbehave.scenario.steps.Steps;
import org.jbehave.scenario.annotations.*;
import org.junit.Assert;
public class NavigationScenarioSteps extends Steps {
private SystemUnderTest system;
@BeforeScenario
public void startSystem() throws Exception {
system = new SystemUnderTest();
system.start();
}
@AfterScenario
public void stopSystem() throws Exception {
system.stop();
}
@Given("I am on \"$url\"")
public void startOnUrl(String url) {
system.open(url);
}
@When("I click on link \"$linkText\"")
public void clickLink(String linkText) {
system.clickLink(linkText);
}
@Then("the title is \"$title\"")
public void checkTitle(String title) {
Assert.assertEquals(title, system.getCurrentTitle());
}
}
This time around we need to make certain we have the ability to start and stop our system. We annotate the starting and the stopping of the system, so we know the state of the system when we run.
Like I said before, we have a system under test that is “real”. In this particular case, our system is really a pass-through to Selenium Remote Control:
package com.bitmotif.jbehaveexamples.part.two;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import org.openqa.selenium.server.SeleniumServer;
public class SystemUnderTest {
private static final String MAX_WAIT_TIME_IN_MS = "60000";
private static final String BASE_URL = "http://www.bitmotif.com";
private Selenium selenium =
new DefaultSelenium("localhost", 4444, "*firefox", BASE_URL);
private SeleniumServer seleniumServer;
public void start() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}
public void stop() {
selenium.stop();
seleniumServer.stop();
}
public void open(String url) {
selenium.open(url);
}
public void clickLink(String linkText) {
selenium.click("link=" + linkText);
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
}
public String getCurrentTitle() {
return selenium.getTitle();
}
}
With this in place, we can run our scenario. Tres cool.