简体   繁体   English

将变量库加载到Watir中

[英]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. 我正在尝试将一个文件加载到watir中,该文件将包含在整个站点上用于许多不同功能所需的所有常量。 Whenever I do this though it always says that browser is undefined. 每当我执行此操作时,尽管它总是表示浏览器未定义。

All of the constants start browser.xxxxx. 所有常量都启动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. 变量名称browser.xxxx无效,因为您不能在变量名称中包含句点(。)。 You need a different variable name, for example, use underscores instead - browser_xxxx . 您需要一个不同的变量名,例如,使用下划线browser_xxxx
  2. A variable like browser_xxxx is a local variable. browser_xxxx这样的变量是局部变量。 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 . 如果要在另一个文件中使用它,则应使用一个常量,例如BROWSER_URL ,或创建一个模块,以便可以执行Browser.url

Example Using Constants 使用常量的示例

Say your constants.rb file contains: 假设您的constants.rb文件包含:

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: 您可以在constants.rb文件中创建一个可处理各种常量的模块:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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