-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for injecting the Pages page factory into JBehave scena…
…rio libraries via the constructor
- Loading branch information
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 42 additions & 2 deletions
44
src/main/java/net/thucydides/jbehave/ThucydidesStepContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/net/thucydides/jbehave/steps/StoryStepsWithPageObjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/test/resources/stories/aBehaviorWithSeleniumPageObjects.story
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |