Ruby将值传递给方法?的webdriver

时间:2016-04-05 13:57:45

标签: ruby selenium-webdriver ui-automation browser-automation

硒新的红宝石,但基本上我想做的是将contact['rob', 'email', 'subject', 'message']传递给

def contact(name="Default", email="Default", subject="Default", message="Default")
    @name = name
    @email = email
    @subject = subject
    @message = message

然后如果值匹配运行代码如下:

 if(name == 'rob' && email == 'email' && subject == 'subject' && message == 'message')
  name_field = driver.find_element :css => ('#wpcf7-f375-p28-o1 > form > p:nth-child(2) > span > input')
  name_field.click
  name_field.send_keys(name)

  email_field = driver.find_element :css =>  '#wpcf7-f375-p28-o1 > form > p:nth-child(3) > span > input'
  email_field.click
  email_field.send_keys(email)

  subject_field = driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(4) > span > input'
  subject_field.click
  subject_field.send_keys(subject)

  message_field = driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(5) > span > textarea'
  message_field.click
  message_field.send_keys(message)
    else
      puts 'failed'
      end
  end

完整代码:

require 'rubygems'
require 'selenium-webdriver'

class Standard_web_driver_test2

  driver = Selenium::WebDriver.for :firefox
  driver.get 'http://demoqa.com/contact/'
  driver.manage.window.maximize
  wait = Selenium::WebDriver::Wait.new(:timeout => 5)



  def contact(name="Default", email="Default", subject="Default", message="Default")
    @name = name
    @email = email
    @subject = subject
    @message = message
    if(name == 'rob' && email == 'email' && subject == 'subject' && message == 'message')
  name_field = driver.find_element :css => ('#wpcf7-f375-p28-o1 > form > p:nth-child(2) > span > input')
  name_field.click
  name_field.send_keys(name)

  email_field = driver.find_element :css =>  '#wpcf7-f375-p28-o1 > form > p:nth-child(3) > span > input'
  email_field.click
  email_field.send_keys(email)

  subject_field = driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(4) > span > input'
  subject_field.click
  subject_field.send_keys(subject)

  message_field = driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(5) > span > textarea'
  message_field.click
  message_field.send_keys(message)
    else
      puts 'failed'
      end
  end

  contact['rob', 'email', 'subject', 'message']
end

我确信有一种更简单的方法可以做到这一点,但只是尝试习惯使用带代码的块。

错误消息:':未定义的局部变量或方法`contact'for Standard_web_driver_test2:Class(NameError)

使用全局变量之前的新代码:

require 'rubygems'
require 'selenium-webdriver'

class StandardWebdriverTest2

  @@driver = Selenium::WebDriver.for :firefox
  @@driver.get 'http://demoqa.com/contact/'
  @@driver.manage.window.maximize
  wait = Selenium::WebDriver::Wait.new(:timeout => 5)



  def self.contact(name="Default", email="Default", subject="Default", message="Default")
    @name = name
    @email = email
    @subject = subject
    @message = message
    if(name == 'rob' && email == 'email' && subject == 'subject' && message == 'message')
  name_field = @@driver.find_element :css => ('#wpcf7-f375-p28-o1 > form > p:nth-child(2) > span > input')
  name_field.click
  name_field.send_keys(name)

  email_field = @@driver.find_element :css =>  '#wpcf7-f375-p28-o1 > form > p:nth-child(3) > span > input'
  email_field.click
  email_field.send_keys(email)

  subject_field = @@driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(4) > span > input'
  subject_field.click
  subject_field.send_keys(subject)

  message_field = @@driver.find_element :css => '#wpcf7-f375-p28-o1 > form > p:nth-child(5) > span > textarea'
  message_field.click
  message_field.send_keys(message)
    else
      puts 'failed'
    end

  end

  contact('rob', 'email', 'subject', 'message')
end

1 个答案:

答案 0 :(得分:2)

您的班级名称应该是

class StandardWebdriverTest2

根据命名惯例

Class and Module 
Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter, e.g. InvoiceItem

您可以在此命名惯例here

上找到一些建议

其次你在类

中调用contact方法

然后你需要让它成为类方法

class StandardWebdriverTest2
   #your code rest of the code
   def self.contact(name="Default", email="Default", subject="Default", message="Default")
      #your code for contact
   end
 contact("","","","") # call contact method
 end

否则会收到错误undefined local variable or method "contact" for Standard_web_driver_test2:Class (NameError)