为什么会收到错误“ NameError:未初始化的常量Gmails :: Username”

时间:2018-12-28 06:14:28

标签: ruby rspec

如何在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 

1 个答案:

答案 0 :(得分: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])