0

I'm no Ruby guru so please can I get some help: I want to add whatever code is needed so that there is an option where the following case statement will execute every case. Basically if the user declares a 'browser' name at command line then Cucumber selenium tests will be run against a certain platform/browser. I want it so that if they declare 'all', or if they declare nothing (else) then every case will be executed.

Thanks

case ENV['BROWSER'] when 'safari' $browser = Selenium::WebDriver.for :safari $browser_name = 'Safari' when 'mobile' $browser = Selenium::WebDriver.for :chrome $browser_name = 'Mobile' $browser.manage.window.resize_to(400, 650) else $browser = Selenium::WebDriver.for :chrome $browser_name = 'Chrome' end 
1
  • 1
    In Ruby $-prefixed variables like $browser are global and these are almost always trouble, so they're best avoided. Commented May 6, 2015 at 17:03

2 Answers 2

3

If you need that sort of thing, a case statement is only part of the solution. You need to iterate as well. Break it out a little differently:

BROWSERS = %w[ safari chrome mobile ] BROWSER_ALIASES = { all: BROWSERS } # Interpret the environment variable as either an alias, or a single # step to run. browsers = BROWSER_ALIASES[ENV['BROWSER']] || [ ENV['BROWSER'] ] browsers.each do |browser| case (browser) when 'safari' # ... Safari specific action when 'mobile' # ... end end 

This expansion allows you to trigger zero, one or many of the browsers, plus set up arbitrary aliases as necessary.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. I will give this a go and see where i end up @tadman
0

You don't want a switch then. The purpose of a switch statement is to NOT run every other case (it's designed to be exclusive). Just use if statements instead.

There are actually several ways to set up the ifs but if you want to keep it looking kind of like a switch, this is probably your best bet:

if ENV['BROWSER'] == 'safari' || ENV['BROWSER'] == 'all' # do safari stuff end if ENV['BROWSER'] == 'mobile' || ENV['BROWSER'] == 'all' # do mobile stuff end if ENV['BROWSER'] == 'chrome' || ENV['BROWSER'] == 'all' # do chrome stuff end 

2 Comments

This looks straight forward, though i'm wondering, if the user opts for the 'all' option as declared here, will all browsers be executed concurrently or consecutively?
@user1738702 consecutively. Unless you're somehow running it in a thread environment. A single threaded program will have to wait for whatever it's currently working on to end before it starts on the next bit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.