Showing posts with label JBehave. Show all posts
Showing posts with label JBehave. Show all posts

Friday, June 10, 2011

Introduction to Jbehave

JBehave is a framework for Behaviour-Driven Development
Behaviour-driven development (BDD) is an evolution of test-driven development (TDD) and acceptance-test driven design, and is intended to make these practices more accessible and intuitive to newcomers and experts alike.
It shifts the vocabulary from being test-based to behaviour-based, and positions itself as a design philosophy.

Features of JBehave include:
  • Pure Java implementation, which plays well with Java-based enterprises or when interfacing to any environment that exposes a Java API.
  • Users can specify and run text-based user stories, which allows "out-in" development.
  • User stories can be specified as classpath resources or external URL-based resources.
  • User stories can be executed concurrently, specifying the number of concurrent threads.
  • User stories can be documented via generic user-defined meta information that allows easy story filtering and organisation into story maps.
  • Annotation-based binding of textual steps to Java methods, with auto-conversion of string arguments to any parameter type (including generic types) via custom parameter converters.
  • Annotation-based configuration and Steps class specifications
  • Dependency Injection support allowing both configuration and Steps instances composed via your favourite container (Guice, PicoContainer, Spring, Weld).
  • Groovy scripting supported for writing configuration and Steps instances
  • Extensible story reporting: outputs stories executed in different human-readable file-based formats (HTML, TXT, XML). Fully style-able view.
  • Story cross reference report format in JSON and XML, consumable by external applications.
  • Auto-generation of pending steps so the build is not broken by a missing step, but has option to configure breaking build for pending steps.
  • Pluggable step prioritising strategy. Strategies bundled in core include: by priority field and by Levenshtein Distance.
  • Localisation of user stories, allowing them to be written in any language.
  • IDE integration: stories can be run as JUnit tests or other annotation-based unit test frameworks, providing easy integration with your favourite IDE.
  • Ant integration: allows stories to be run via Ant task
  • Maven integration: allows stories to be run via Maven plugin at given build phase

Selenium + Maven + Jbehave

YahooLoginScenaris.java

package ch.sai.blog.jbehave.story;

import java.util.List;

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.junit.JUnitStory;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.reporters.StoryReporterBuilder.Format;
import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.junit.Test;


import ch.sai.blog.jbehave.step.YahooLoginSteps;

public class YahooLoginScenarios extends JUnitStory{
    @Override
    public Configuration configuration() {
       
        return new MostUsefulConfiguration().useStoryLoader(

        new LoadFromClasspath(getClass().getClassLoader()))
                .useStoryReporterBuilder(

                        new StoryReporterBuilder().withDefaultFormats().withFormats(Format.HTML));
    }

    @Override
    public List<CandidateSteps> candidateSteps() {

        return new InstanceStepsFactory(configuration(), new YahooLoginSteps())
                .createCandidateSteps();
    }

   
    @Test
    public void run() throws Throwable {

        super.run();
    }     
   
}

 YahooLoginSteps.java

package ch.sai.blog.jbehave.step;

import org.jbehave.core.annotations.AfterScenario;
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import ch.sai.blog.jbehave.domain.SystemUnderTestScenarios;


public class YahooLoginSteps   {
       private SystemUnderTestScenarios system;

       @BeforeScenario
       public void startSystem() throws Exception {
          system = new SystemUnderTestScenarios();
          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 type $username and $password")
       public void click(String username,String password) {
         
          system.clickLink(username, password);
       }
      
     /* @Then("the page should display $errormessage")
      public void invaliduser(String errormsg)
      {
        system.invalid(errormsg);
      }*/
     
   
      @Then("click on $link")
       public void signout(String linkText) {
           system.logoff(linkText);
       }
    }
 
 SystemUnderTest.java


package ch.sai.blog.jbehave.domain;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import org.openqa.selenium.server.SeleniumServer;

public class SystemUnderTestScenarios {
  
   private static final String BASE_URL = "https://login.yahoo.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();
      selenium.windowFocus();
      selenium.windowMaximize();
   }

   public void stop() {
      selenium.stop();
      seleniumServer.stop();
   }

   public void open(String url) {
      selenium.open(url);
    
   }



   public void logoff(String linkText) {
       selenium.click("link=" + linkText);

   }

   public void clickLink(String username,String password) {
        selenium.type("username", username);
        selenium.type("passwd", password);
        selenium.click(".save");
        //selenium.waitForPageToLoad("30000");
       
        }

/*public void invalid(String errormsg) {
    selenium.isTextPresent(errormsg);
        selenium.captureScreenshot("c:\\test1.jpg");
       
   
   
}*/
  
 
       
   
}

POM file


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ch.frankel.blog.jbehave</groupId>
    <artifactId>jbehave-example</artifactId>
    <version>1.0.0</version>
    <name>JBehave code examples</name>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jbehave</groupId>
                <artifactId>jbehave-maven-plugin</artifactId>
                <version>3.0</version>
                <executions>
                    <execution>
                        <id>run-stories-as-paths</id>
                        <phase>integration-test</phase>
                        <configuration>
                            <includes>
                                <include>ch/frankel/blog/jbehave/story/*.story</include>
                            </includes>
                            <scope>test</scope>
                        </configuration>
                        <goals>
                            <goal>run-stories-as-paths</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
           
           
                   
           
           
           
           
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-core</artifactId>
            <version>3.4</version>
        </dependency>
    </dependencies>
</project>