Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 硒和红宝石元素映射_Ruby_Selenium - Fatal编程技术网

Ruby 硒和红宝石元素映射

Ruby 硒和红宝石元素映射,ruby,selenium,Ruby,Selenium,我希望使用pageObject模型映射我的元素,尽管我面临以下问题: 1:。如果我没有驱动程序,就会抛出错误,这是可以的,因为我只在实例化类时映射驱动程序 element = @driver.find_element(:id => 'username') def initialize driver @driver = driver @driver.navigate.to "http://www.google.com"

我希望使用pageObject模型映射我的元素,尽管我面临以下问题:

1:。如果我没有驱动程序,就会抛出错误,这是可以的,因为我只在实例化类时映射驱动程序

     element = @driver.find_element(:id => 'username') 

      def initialize driver
         @driver = driver
         @driver.navigate.to "http://www.google.com" 
      end

     def set_username input
        element.send_keys input
     end
2:。在下面的方法中,它不会抱怨缺少驱动程序,因为我之前正在初始化它并将其作为全局变量传递。但现在,它甚至在打开页面之前就尝试映射元素,但由于找不到元素而失败

     element = $driver.find_element(:id => 'username') 

      def initialize 
         $driver.navigate.to "http://www.google.com" 
      end

     def set_username input
        element.send_keys input
     end
问题是:有没有什么厚颜无耻的方法可以映射我的元素并将它们分配给对象,但只有在我实际需要使用它们时才编译/读取它们?我只在set_username中使用它执行一些操作,并且我只想在这个方法中使用它时触发对象映射,例如。。。我不喜欢使用现有的pageObject框架…

对于,您有一个表示正在测试的页面的对象,以及表示较低级别HTML对象的对象。在这种情况下,您可能有以下类:

# Represents a text input HTML element...
class TextInput
  attr_reader :element

  def initialize(driver, id)
    element = driver.find_element(:id => id) 
  end

  def type(text)
    element.send_keys text
  end
end

# Represents the page you are testing...
class SomePage
  attr_reader :driver, :username

  def initialize(driver)
    @driver = driver
  end

  def username
    @username ||= TextInput.new(@driver, 'username')
  end
end
初始化驱动程序后,将其传递给SomePage并使用它 来驱动你正在做的事情

some_page = SomePage.new(driver)
some_page.username.type("Timmy")
我不能担保,因为我从未使用过它,但它会处理那些HTML层对象,并为您提供一种特定于域的语言,用于将它们构建到页面对象类中。值得一看