chadenis
chadenis

Reputation: 71

How can I use a variable in 3 different Ruby files? Global Variable?

I have 1 file where I make a login to a site and another site where I create a new user, and another file where I logout from the site.

I just created 3 different classes that are in 3 different files and need to use the same variable: 'browser = Watir::Browser.new' in each class.

I'm planning to create a 4th file 'test.rb' from where I call the other 3 files, 'login.rb', 'createEmployee.rb' and 'logout.rb'.

Upvotes: 0

Views: 366

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59601

Here is some sample code to get you started. Create the instance of Watir::Browser in your test file, and then pass it into the constructor of each instance of the class you make.

class Login
    def initialize(browser)
        @browser = browser
    end
end

class User
    def initialize(browser)
        @browser = browser
    end
end

class Logout
    def initialize(browser)
        @browser = browser
    end
end

# in test.rb
browser = Watir::Browser.new
login = Login.new(browser)
user = User.new(browser)
logout = Logout.new(browser)

Upvotes: 1

Related Questions