Friday, June 10, 2011

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>

5 comments:

  1. Hi Krishna,
    I am using webdriver and have a very simple login scenario which I am trying to execute, its not throwing any error but at the same time I am not seeing my steps executed in the browser. Will you be in a position to point where am I going wrong. Also should there be one scenario per story file or you can have multiple scenarios in the same file, if there are multiple scenarios to be executed what changes are needed thnx in advance.

    My console reads something like this:
    Processing system properties {}
    Using 1 threads
    Running story com/example/selBdd/stories/login_steps.story
    Generating reports view to 'D:\Selenium\WorkFiles\EclipseCode\selBdd\target\jbehave' using formats '[stats, html]' and view properties '{defaultFormats=stats, decorateNonHtml=true, viewDirectory=view, decorated=ftl/jbehave-report-decorated.ftl, reports=ftl/jbehave-reports-with-totals.ftl, maps=ftl/jbehave-maps.ftl, navigator=ftl/jbehave-navigator.ftl, views=ftl/jbehave-views.ftl, nonDecorated=ftl/jbehave-report-non-decorated.ftl}'
    Reports view generated with 3 stories (of which 1 pending) containing 1 scenarios (of which 0 failed and 1 pending)

    I am sure the problem if there is any should be somwhere in this part:
    @Override
    public Configuration configuration() {
    return new MostUsefulConfiguration().useStoryLoader(
    new LoadFromClasspath(getClass().getClassLoader()))
    .useStoryReporterBuilder(
    new StoryReporterBuilder().withDefaultFormats().withFormats(Format.HTML));
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
    return new InstanceStepsFactory(configuration(), new LoginSteps());
    }

    @Override
    protected List storyPaths() {
    return new StoryFinder()
    .findPaths(codeLocationFromClass(this.getClass()).getFile(), asList("**/*.story"), null);

    thanks n regards
    randy

    ReplyDelete
  2. @Froyo .. Can u pls send across ur code to saikrishna321@yahoo.com

    will debug n update u

    ReplyDelete
  3. what are the jar files need to included in build path.. i could not run the above sample..it shows "JUnitStory" can not be resolved to a type

    ReplyDelete
  4. I just want to know,how calls are made for html report file which user see as report of story execution?
    I want to update html-output.ftl file such a way that when final report gets generated in
    ..\target\jbehave\view\reports.html file, i don't want columns like Given-story Scenarios, Steps in html output file as I want to delete those permanently ..how i can do that?
    When i see the html-output.ftl file it has very complex code which does not get how the columns are mapped and how to delete the what code...
    ...do I need to delete the code in jbehave-core.css file
    as well? Pls suggest
    Also if i made changes in code , how i can make them persistent/permanent so that every time i compile, i don't see old code...

    ReplyDelete
  5. Hi Krishna,how to setup jbehave and maven with selenium. How to get jars and installation guide lines on same?

    ReplyDelete