当前位置: 代码迷 >> Web前端 >> Jbehave(一) First Web Page Sample
  详细解决方案

Jbehave(一) First Web Page Sample

热度:957   发布时间:2012-10-14 14:55:08.0
Jbehave(1) First Web Page Sample
Jbehave(1) First Web Page Sample

1. jbehave totorial sample
use git bash on my win7 system, from the start menu, choose git bash
>pwd
>cd /d/book/jbehave
>git clone https://github.com/jbehave/jbehave-tutorial.git jbehave-tutorial

change the pom.xml, from
<properties>
    <jbehave.core.version>3.6.SNAPSHOT</jbehave.core.version>
    <jbehave.web.version>3.5.SNAPSHOT</jbehave.web.version>
    <jbehave.site.version>3.1.1</jbehave.site.version>
</properties>

to

<properties>
    <jbehave.core.version>3.5.1</jbehave.core.version>
    <jbehave.web.version>3.4</jbehave.web.version>
    <jbehave.site.version>3.1.1</jbehave.site.version>
</properties>

>mvn install

error messages:
[WARNING] Failed to run story etsy_search.story
org.jbehave.core.failures.UUIDExceptionWrapper: org.jbehave.core.failures.Before
OrAfterFailed: Method beforeStory (annotated with @BeforeStory in class org.jbeh
ave.web.selenium.PerStoryWebDriverSteps) failed: org.openqa.selenium.WebDriverEx
ception: Cannot find firefox binary in PATH. Make sure firefox is installed. OS
appears to be: VISTA

Build info: version: '2.5.0', revision: '13548', time: '2011-08-24 13:44:31'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.ver
sion: '1.6.0_26'
Driver info: driver.version: unknown

Solutions:
download and install Firefox 3.x version in the default directory

>mvn install -Dmeta.filter="+color red"

In a directory target/view, a page named 'reports.html' has been generated.

2. Analynize the sample
etsy-stories-java-spring/src/main/stories/    all the stories are here.
etsy-stories-java-spring/src/main/java/org/jbehave/tutorials/etsy/EtsyDotComStories.java   
This is the entry of the tests.

etsy-stories-java-spring/resources/etsy-steps.xml contains the Spring configuration for composition the steps.

3. Study some concepts and ideas
3.1 Write a textual story
create a textual story file store_locator.story
Store Locator Story

Narrative:
Store Locator Function, we can locate a store.
As a user, we can get the current location.

#Scenario: Get Current Location
#Given open home page
#When click the store locator link
#Then get the store locator page button id is uselocation
#When click the button user location
#Then get the google map, the text is 'No stores found in the specified location'

Scenario: Search the Store Location with 78704
Given open home page
When click the store locator link
Then get the store locator page button id is uselocation
When type in the 78704 ZIP code
Then get some stores, see the map button

Scenario: Search the Store Location with 78730
Given open home page
When click the store locator link
Then get the store locator page button id is uselocation
When type in the 78730 ZIP code
Then get some stores, see the map button

3.2 Map all steps to Java methods
Map all the story to our Java methods:
private Home home;

private StoreLocator storeLocator;

public PetcoSteps(PetcoPageFactory pageFactory) {
home = pageFactory.newHome();
storeLocator = pageFactory.newStoreLocator();
}

@Given("open home page")
public void openHomePage() {
home.go();
}

@When("click the store locator link")
public void clickTheStoreLocatorLink() {
home.goToStoreLocator();
}

@Then("get the store locator page button id is $buttonId")
public void getTheStoreLocatorPage(String buttonId) {
MatcherAssert.assertThat(storeLocator.hasButton(buttonId),
Matchers.is(true));
}

@When("click the button user location")
public void clickTheButtonUserLocation() {
storeLocator.clickUserLocator();
}

@Then("get the google map, the text is $message")
public void getTheGoogleMap(String message) {
MatcherAssert.assertThat(storeLocator.hasText(message),
Matchers.is(true));
}

@When("type in the $zipCode ZIP code")
public void typeTheZipCode(String zipCode) {
storeLocator.typeZIPCode(zipCode);
}

@Then("get some stores, see the map button")
public void getMapButton() {
MatcherAssert
.assertThat(storeLocator.hasMapButton(), Matchers.is(true));
}

3.3 Configuration in Spring
my configuration sample is petco-steps.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="driverProvider" class="org.jbehave.web.selenium.FirefoxWebDriverProvider" >
  </bean>
 
  <bean id="petcoPageFactory" class="com.easybddweb.vendors.petco.pages.PetcoPageFactory">
     <constructor-arg ref="driverProvider"/>
  </bean> 
 
  <bean id="petcoSteps" class="com.easybddweb.vendors.petco.steps.PetcoSteps">
     <constructor-arg ref="petcoPageFactory"/>
  </bean> 

  <bean id="petcoLifecycleSteps" class="com.easybddweb.vendors.petco.steps.PetcoLifecycleSteps">
     <constructor-arg ref="driverProvider"/>
  </bean> 
 
</beans>

4.4 We use maven and plugin to run these tests
<plugins>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.core.version}</version>
<executions>
<execution>
<id>unpack-view-resources</id>
<phase>process-resources</phase>
<goals>
<goal>unpack-view-resources</goal>
</goals>
</execution>
<execution>
<id>embeddable-stories</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>${stories}</include>
</includes>
<excludes />
<generateViewAfterStories>true</generateViewAfterStories>
<ignoreFailureInStories>true</ignoreFailureInStories>
<ignoreFailureInView>false</ignoreFailureInView>
<threads>${threads}</threads>
<skip>${skip}</skip>
<metaFilters>
<metaFilter>${meta.filter}</metaFilter>
</metaFilters>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
<execution>
<id>map-stories</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/*StoryMaps.java</include>
</includes>
</configuration>
<goals>
<goal>map-stories-as-embeddables</goal>
</goals>
</execution>
<execution>
<id>report-stepdocs</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>${stories}</include>
</includes>
</configuration>
<goals>
<goal>report-stepdocs-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Ok, the first example for web page test is done. The related things are maven2, jbehave, junit, spring, selenium.
The details are in the sample project easybddweb-1.0.zip.

references:
http://jbehave.org/
http://yangzb.iteye.com/blog/309354
http://jbehave.org/introduction/
http://jbehave.org/documentation/
http://jbehave.org/reference/stable/getting-started.html
https://github.com/jbehave/jbehave-tutorial
http://jbehave.org/reference/stable/running-examples.html
http://groups.google.com/group/webdriver/msg/1f307c5a78c6f8d3?pli=1

  相关解决方案