3

I am automating using Cucumber and Watir Webdriver and I want to know if there is a way to clear the state of the browser instead of closing it after every run so that I can use Scenario Outline and I open just one instance of the browser and clear state of the browser for other examples listed in the example table

Scenario Outline: This is an example of what I want to achieve.
  Given I visit the <Website>
  Then the current page must be <page_title>
  Example:
    |Website|page_title|
    |google| Google|
    |Facebook|Welcome to Facebook|
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
Vamsi
  • 78
  • 9
  • Can you elaborate on what you mean by "clear the state of the browser"? Do you just mean cookies? Or you also talking about other things like history, memorized passwords, etc.? – Justin Ko Nov 05 '13 at 03:52
  • @JustinKo - I want to use the same instance of browser so that I dont have to open a new browser instance for each example in my table no matter how many examples I have; I just open one instance of browser, run the test, clear the cookies and use the same browser window to run the next one listed in the example table and kill the browser only after executing all the listed examples. – Vamsi Nov 05 '13 at 18:10

2 Answers2

3

Assuming that, in terms of resetting the browser, you just need to clear the cookies, you can use the following hooks.

# Create a browser that will be used for all scenarios
browser = Watir::Browser.new
Before do
  @browser = browser
end

# Clear the state (cookies) before each scenario
Before do |scenario|
  @browser.cookies.clear
end

# Close the browser after all scenarios completed
at_exit do
  browser.close
end
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
0

Are u talking about the tITEL becuse if you are there is any of tutorials out there and it is easy try typeing changeing thw state of the titel in google

  • It has got nothing to do with changing the title , I want to open the browser once and then run the test multiple times rather than opening a new instance of browser for each run. – Vamsi Nov 04 '13 at 14:26
  • It's not clear why you prefer to leave the browser open. Your cookies are reset between runs if it is closed. For closing, I would research the various types of hooks the cucumber has. – Dave McNulla Nov 04 '13 at 23:07
  • Speed. Opening and closing the browser takes time.. I run the same browser, and only clear cookies specifically if I know the given scenario needs them. I even leave users logged in, if it's the right user. My 'login' method first checks to see if the right user is already logged in, if not it calls logout and logs in the right guy, if the right user is logged in, it just proceeds. That saved a HUGE amount of time (nearly 1/3) especially in scenario outlines that start by logging in the same user. (steps/scenarios that don't create or destroy data tend to use 'fixture' users) – Chuck van der Linden Nov 08 '13 at 04:59
  • I'm not fond of saving time, but I understand the pressure. We have 15 virtual machines that run cukes for nearly an hour to finish the run. – Dave McNulla Nov 22 '13 at 06:07