Hi Anisha,

# File: hooks.rb or env.rb

# I don't know if there is a "before feature hook" but
# you could add a tag with a special name to identify 
# which scenario you want to do that (e.g @goto_home_page_once)
# In this way, it will navigate to home page the first time.
# So, you could add the tag to all of those scenarios you want to goto home 
page once
# In your case, with this implementation, you should add the specific tag 
to all scenarios 
# in the feature you need and remove Background and its step.


Before |scenario| do

if scenario.source_tags.map{|tag| tag.name}.include? "@goto_home_page_once"
@browser.goto("www.example.com") if @browser.url != "www.example.com"
end

end

# Basically before each scenario it evaluates all defined tags. 
# If the special tag is on the list (is a scenario from the feature you 
need to do that) 
# it will open home page if browser url is different from the page you need 
(home page)


# Or simply

Before('@goto_home_page_once') do
@browser.goto("www.example.com") if @browser.url != "www.example.com"
end

El miércoles, 5 de febrero de 2014 12:57:21 UTC-3, Anisha escribió:
>
> Hi,
>
> Suppose I want to run a step only once before a feature, then how do I 
> write it? If I add the background section to a scenario outline which has 
> data tables, then with each scenario the background is implemented. Also I 
> tried the Before hook in env.rb but it is not suiting the test case. I have 
> also tried the before scenario hook, I am looking for a before feature hook 
> basically. Following is the scenario I want to handle:
>
> Feature: Search
> Background:
> Given that I am on the home page
>  
> Scenario Outline: When I search for the following queries then I should 
> get the respective results
>
> When I search for <search query>
>  Then the keyword <highlight keyword> should be highlighted
> And <x> x should be highlighted
>  And <y> y should be highlighted
> Examples:
> |search query   | highlight keyword| x | y |
>  |strawberry   | strawberry | No |No |
>  |apple  | apple |No | No |
>  |mango   | mango | yes | No    |
>
>
> step:definitions.rb:
> Given(/^that I am on the home page$/) do
>
>   @browser.goto("www.example.com")
> end
>
> When(/^I search for strawberry$/) do
>   @browser.text_field(:id => "searchbox").wait_until_present
>   @browser.text_field(:id => "searchbox").set "strawberry"
>   @browser.send_keys :enter
>   sleep 1
> end
>
> -----------------------------------------------
>
> In the above case, the /Given that I am on the home page/ is executed for 
> each search query I want to make, which I do not want. I am basically 
> wanting that once I land on the home page then each of the search queries 
> should be executed in order of when and then (when, then, when, then, when 
> then)as the search bar is visible on the page, I basically do not want the 
> page to be reloaded on each search query I want to make. 
> -- 
> Regards,
> Anisha Narang
>  

-- 
-- 
Before posting, please read http://watir.com/support. In short: search before 
you ask, be nice.

[email protected]
http://groups.google.com/group/watir-general
[email protected]

--- 
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to