Skip to content

Commit

Permalink
Added support for injecting the Pages page factory into JBehave scena…
Browse files Browse the repository at this point in the history
…rio libraries via the constructor
  • Loading branch information
wakaleo committed Jul 8, 2012
1 parent 03d3d47 commit 12149d5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/main/java/net/thucydides/jbehave/ThucydidesStepContext.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
package net.thucydides.jbehave;

import net.thucydides.core.pages.Pages;
import net.thucydides.core.webdriver.ThucydidesWebDriverSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;

/**
* Keeps track of instantiated JBehave step libraries used in ThucydidesWebdriverIntegration tests.
*/
public class ThucydidesStepContext {

private static final Logger LOGGER = LoggerFactory.getLogger(ThucydidesStepContext.class);

public ThucydidesStepContext() {
}

public Object newInstanceOf(Class<?> type) {
public Object newInstanceOf(final Class<?> type) {
try {
return type.newInstance();
ThucydidesWebDriverSupport.getPages();
if (hasConstructorWithPagesParameter(type)) {
return createNewPageEnabledStepCandidate(type);
} else {
return type.newInstance();
}
} catch (Exception e) {
throw new ThucydidesStepInitializationError(e);
}
}

private boolean hasConstructorWithPagesParameter(Class<?> type) {
Class[] constructorArgs = new Class[1];
constructorArgs[0] = Pages.class;
try {
type.getConstructor(constructorArgs);
} catch (NoSuchMethodException e) {
return false;
}
return true;
}

private <T> T createNewPageEnabledStepCandidate(final Class<T> type) {
T newInstance = null;
try {
Pages pageFactory = ThucydidesWebDriverSupport.getPages();
Class[] constructorArgs = new Class[1];
constructorArgs[0] = Pages.class;
Constructor<T> constructor = type.getConstructor(constructorArgs);
newInstance = constructor.newInstance(pageFactory);
} catch (Exception e) {
LOGGER.info("Failed to instantiate page of type {} ({})", type, e.getMessage());
}
return newInstance;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ public void a_test_should_use_a_different_browser_if_requested() throws Throwabl
assertThat(outcomes.get(0).getResult(), is(TestResult.SUCCESS));
}

@Test
public void a_jbehave_step_library_can_use_page_objects_directly() throws Throwable {

// Given
ThucydidesJUnitStories story = new AStorySample("aBehaviorWithSeleniumPageObjects.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.get(0).getResult(), is(TestResult.SUCCESS));
}

@Test
public void should_be_able_to_specifiy_the_browser_in_the_base_test() throws Throwable {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.thucydides.jbehave.steps;

import net.thucydides.core.pages.Pages;
import net.thucydides.jbehave.pages.StaticSitePage;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class StoryStepsWithPageObjects {

private final Pages pages;

StaticSitePage indexPage;

public StoryStepsWithPageObjects(Pages pages) {
this.pages = pages;
indexPage = pages.get(StaticSitePage.class);
}

@Given("I start on the test page")
public void givenIAmOnTheTestPage() {
indexPage.open();
}

@When("I enter a first name $firstname")
public void whenIEnterTheFirstName(String firstname) {
indexPage.setFirstName(firstname);
}

@When("I enter a last name $lastname")
public void whenIEnterTheLastName(String lastname) {
indexPage.setLastName(lastname);
}

@Then("I should see the $firstname and $lastname in the names fields")
public void thenIShouldSeeInTheNamesFields(String firstname,
String lastname) {
assertThat(indexPage.firstName().getValue(), is(firstname));
assertThat(indexPage.lastName().getValue(), is(lastname));
}
}
14 changes: 14 additions & 0 deletions src/test/resources/stories/aBehaviorWithSeleniumPageObjects.story
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Meta:
@driver htmlunit

Scenario: A scenario that uses selenium

Given I start on the test page
When I enter a first name <firstname>
And I enter a last name <lastname>
Then I should see the <firstname> and <lastname> in the names fields

Examples:
|firstname|lastname|
|Joe | Bloggs|
|John | Doe |

0 comments on commit 12149d5

Please sign in to comment.