# How to set up data before story runner steps?

**URL:** <https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302>\
**Category:** RSpec\
**Created:** [February 27, 2008, 6:23am UTC](https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302 "2008-02-27T06:23:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Bei](https://www.ruby-forum.com/u/Bei)\
**Post date:** [February 27, 2008, 6:23am UTC](https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302/1 "2008-02-27T06:23:46Z")

</div>

Hi

```
 I'm using story runner of RSpec now. What I'm trying to do is

```

initializing the @selenium before any steps of one story is executing,  
and stop @selenium after any story is finished. just as @BeforeClass  
and @AfterClass in jUnit  
Any suggestions on this?

```
 Thanks!!!

```

Bei

here is my code for  
STEPS:

```
 steps_for(:search) do

Given("user could access search page") do
    @google_page = GooglePage.new(@selenium)
    @google_page.open;
end

Given("user enter search test: $search_text") do |$search_text|
    @google_page.search_text=$search_text
end

When("do search") do
    @search_result_page = @google_page.search
end

Then("show search result") do
    @search_result_page.should_not == nil
end

```

end

STORY:

Story: #000  
As A command human  
I want to get information quickly  
So that I could save me time on doing real things

Scenario: could search  
Given user could access search page  
And user enter search test: asdfasdfasdfas  
When do search  
Then show search result

GLUE:  
with\_steps\_for :search do  
run ‘stories/000.story’  
end

---

<div class="post-metadata">

**Author:** ![Bei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Bei](https://www.ruby-forum.com/u/Bei)\
**Post date:** [February 27, 2008, 6:33am UTC](https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302/2 "2008-02-27T06:33:10Z")

</div>

On Tue, Feb 26, 2008 at 10:53 PM, Bei [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> ```
> I'm using story runner of RSpec now. What I'm trying to do is
> 
> ```
> 
> initializing the @selenium before any steps of one story is executing,  
> and stop @selenium after any story is finished. just as @BeforeClass  
> and @AfterClass in jUnit  
> Any suggestions on this?

There is no way built in to handle this yet. Is anybody doing this yet?

If not, you’ll have to experiment. If you’re interested, I think the  
way to handle it is to register a listener like this:

Spec::Story::Runner.register\_listener(listener)

The listener object should receive messages like run\_started,  
story\_started, scenario\_started and story\_ended, run\_ended and  
scenario\_failed or scenario\_pending.

Be sure to implement method\_missing to ignore everything else.

Good luck, and let us know what works.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![Bei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Bei](https://www.ruby-forum.com/u/Bei)\
**Post date:** [February 27, 2008, 11:26am UTC](https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302/3 "2008-02-27T11:26:19Z")

</div>

Bei schrieb:

> Hi
> 
> ```
> I'm using story runner of RSpec now. What I'm trying to do is
> 
> ```
> 
> initializing the @selenium before any steps of one story is executing,  
> and stop @selenium after any story is finished. just as @BeforeClass  
> and @AfterClass in jUnit  
> Any suggestions on this?
> 
> ```
> Thanks!!!
> 
> ```

Hi,

my helpers/selenium\_helper.rb file look like that:

# Alias some more ruby-like methods on SeleniumDriver, to make it play

# better with rspec matchers.

module Selenium  
class SeleniumDriver  
alias\_method :original\_method\_missing, :method\_missing  
def method\_missing method\_name, _args  
if method\_name.to\_s =~ /^has\_._?$/  
real\_method = method\_name.to\_s.sub /has\_(.\*)?$/, ‘is\_\1’  
if respond\_to? real\_method  
return send(real\_method, args)  
end  
elsif respond\_to?(real\_method = “get\_” + method\_name.to\_s)  
return send(real\_method)  
end  
return original\_method\_missing(method\_name, args)  
end  
end  
end

$selenium\_driver = nil unless $selenium\_driver  
$selenium\_listener\_added = false unless $selenium\_listener\_added

module StoryHelper  
def selenium  
$selenium\_driver  
end

Spec::Story::World.send :include, self  
end

class SeleniumListener  
include Singleton  
def scenario\_started(\*args)  
$selenium\_driver = Selenium::SeleniumDriver.new(‘localhost’, 4444,  
‘\*firefox’,  
‘[http://localhost:3000/](http://localhost:3000/)’,  
15000)  
$selenium\_driver.start  
end

def scenario\_succeeded(\*args)  
$selenium\_driver.stop  
$selenium\_driver = nil  
end

alias :scenario\_pending :scenario\_succeeded  
alias :scenario\_failed :scenario\_succeeded  
end

unless $selenium\_listener\_added

Spec::Story::Runner.scenario\_runner.add\_listener(SeleniumListener.instance)  
$selenium\_listener\_added = true  
end

Best wishes,  
Thomas

---

<div class="post-metadata">

**Author:** ![Bei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Bei](https://www.ruby-forum.com/u/Bei)\
**Post date:** [October 9, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/how-to-set-up-data-before-story-runner-steps/130302/4 "2008-10-09T15:25:11Z")

</div>

Hello,

i written a rails plugin for driving Selenium from  
the RSpec Story Runner:

> **[GitHub - tmak/rspec-rails-selenium-story](https://github.com/tmak/rspec-rails-selenium-story)**
>
> Contribute to tmak/rspec-rails-selenium-story development by creating an account on GitHub.

Now, i want to migrate my plugin to cucumber.

My question:  
I used the scenario\_failed method of the Story Listener to make  
screenshots of failing story scenarios.  
How can i do that with cucumber?

Thanks,  
Thomas
