【问题标题】:Why do I get the error " NameError: uninitialized constant Gmails::Username"为什么我会收到错误“NameError: uninitialized constant Gmails::Username”
【发布时间】:2018-12-28 06:14:28
【问题描述】:

如何在 Gmails.SetPassword 中传递 row[0,1] 的值。请建议更改相同的值。下面是相同的代码。

module Gmails
  extend RSpec::Matchers
  extend Capybara::DSL

  $Gmails_Username_Input= "//input[@id='identifierId']"
  $Gmails_IdentifierNext_Button="div[id= 'identifierNext']"
  $Gmails_Password_Input="input[name= 'password']"
  $Gmails_PasswordNext_Button="div[id='passwordNext']"

  book = Spreadsheet.open('Data.xls')
  sheet1 = book.worksheet('Sheet1') # can use an index or worksheet name
  sheet1.each do |row|
    break if row[0].nil? # if first cell empty
    puts row.join(',') # looks like it calls "to_s" on each cell's Value
    puts row[0,1]
  end

  def Gmails.OpenGmail
    visit "https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin"
  end  

  def Gmails.SetEmailId
    Gmails.SetElement $Gmails_Username_Input, row[0,1]
  end

  def Gmails.ClickNext
    Gmails.ClickElement $Gmails_IdentifierNext_Button
  end

  def Gmails.SetPassword
    Gmails.SetElement $Gmails_Password_Input, row[1,1]
  end

  def Gmails.ClickPasswordNext
    Gmails.ClickElement $Gmails_PasswordNext_Button
  end

  def Gmails.ClickElement objectpath
    if(objectpath.start_with?('/'))
      find(:xpath, objectpath).click
    else
      find(:css, objectpath).click
    end
  end

  def Gmails.SetElement objectpath ,gmailsTextValue
    if(objectpath.start_with?('/'))
      find(:xpath, objectpath).set gmailsTextValue
    else
      find(:css, objectpath).set gmailsTextValue
    end
  end
end 

【问题讨论】:

  • Ruby 不是 Javascript...
  • @ManishaSingh:对于您发布的代码,我认为您不会收到此错误消息,因为没有引用模块 Gmails::Username。错误消息指的是哪个行号?

标签: ruby rspec


【解决方案1】:

抱歉,在重构您的代码使其看起来像 Ruby 之前,我无法理解它:

module Gmails
  extend RSpec::Matchers
  extend Capybara::DSL

  book = Spreadsheet.open('Data.xls')
  sheet1 = book.worksheet('Sheet1') # can use an index or worksheet name
  sheet1.each do |row|
    break if row[0].nil? # if first cell empty
    puts row.join(',') # looks like it calls "to_s" on each cell's Value
    puts row[0,1]
  end

  def open_gmail
    visit "https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin"
  end

  def set_email_id(id)
    set_element "//input[@id='identifierId']", id
  end

  def click_next
    click_element "div[id= 'identifierNext']"
  end

  def set_password(password)
    set_element "input[name= 'password']", password
  end

  def click_password_next
    click_element "div[id='passwordNext']"
  end

  def find_element(object_path)
    find(object_path.start_with?('/') ? :xpath : :css, object_path)
  end

  def click_element(object_path)
    find_element(object_path).click
  end

  def set_element(object_path, value)
    find_element(object_path).set(value)
  end
end

好的,现在对眼睛更容易了。另请注意,我如何将idpassword 参数添加到set_email_idset_password 方法中。现在您可以像set_password("secret_password") 一样称呼他们。

我认为,当您问“如何通过row[0,1]”时,您实际上并不想这样做。我相信row[0]是第一列的内容,所以:

row = ['hello', 'world']
row[0] # => 'hello'

当您调用 row[0,1] 时,您不是在请求第一列的值,而是在请求数组的一个切片:

row[0,1] # => ['hello']
# same as: 
row.slice(0,1) # => ['hello']

我相信你真正想要的是,将第一列的值传递给set_password 方法:

def set_password(password)
  set_element "input[name= 'password']", password
end

set_password(row[0])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2012-12-30
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多