简体   繁体   中英

Loading Variable Library Into Watir

I'm trying to load a file into watir that will contain all the constants I need to use for a number of different functions across a site. Whenever I do this though it always says that browser is undefined.

All of the constants start browser.xxxxx. I've looked around and it appears that there doesn't be anyway to load a single file containing all the variables you may need, can anyone confirm that this is a definite impossibility?

Thanks

You can load a file containing variables. However, you will need to make some changes to your approach:

  1. The variable name browser.xxxx is not valid as you cannot include periods (.) in a variable name. You need a different variable name, for example, use underscores instead - browser_xxxx .
  2. A variable like browser_xxxx is a local variable. It would only be available in the file it is declared. If you want to use it in another file, you should use a constant, eg BROWSER_URL , or create a module so that you can do Browser.url .

Example Using Constants

Say your constants.rb file contains:

BROWSER_URL = 'www.google.ca'

Then your other script file (assuming in the same folder) can do:

require_relative 'constants'
require 'watir'

browser = Watir::Browser.new
browser.goto BROWSER_URL

Example Using Module

You can create a module in your constants.rb file that contants your various constants:

module Browser
  @url = 'www.google.ca'

  class << self
    attr_reader :url  
  end
end

Then your other script file (assuming in the same folder) can do:

require_relative 'constants'
require 'watir'

browser = Watir::Browser.new
browser.goto Browser.url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM