Я получаю «NoMethodError», когда я уже определил метод - PullRequest
0 голосов
/ 30 января 2020

Написание моего Ruby -Watir-огурца bdd теста я получаю это:

NoMethodError: undefined method `msg=' for #<Watir::Browser:0x0000558fbcdc0cc0>
./features/support/pages/message_page.rb:27:in `escribir'
./features/step_definitions/message_steps.rb:14:in `/^I'm able to write and send a "([^"]*)" successfully$/'
./features/send_messages.feature:12:in `Then I'm able to write and send a "Robot message" successfully'
./features/send_messages.feature:9:in `Then I'm able to write and send a "<message>" successfully'
1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m12.939s

Process finished with exit code 1

Когда я уже определил метод:

class MessagePage
  include PageObject
  @@browser = Watir::Browser.new
  page_url 'https://www.linkedin.com/messaging/'
  text_field(:searchcontact, name: 'searchTerm')
  div(:txtmessage,:role => "textbox")
  button(:btnsend,:type => 'submit')
  div(:txtfield,:xpath =>"//div//div[@role='textbox']")
  text_field(:mensaje,:xpath =>"//div//div[@role='textbox']")
  div(:msg,:role => "textbox") /// HERE!!!

  def searchcontact contact
    self.searchcontact = contact
    #searchcontact(contact).send_keys(:enter)
    wait 5
  end

  def buscar contact
    wait_until do
      searchcontact_element.visible?
      self.searchcontact = contact
    end
    self.searchcontact = contact
  end

  def escribir (message)
    self.msg = message
    wait 5
  end

  def writemessage message
    wait_until do
      msg_element.visible?
      self.msg = message
    end
    self.msg = message
  end

  def sendmessage
    btnsend
  end



end

Ответы [ 2 ]

1 голос
/ 30 января 2020

Метод доступа div(:msg,:role => "textbox") не генерирует метод #msg=. Он определяет только:

  • #msg - Получает текст div
  • #msg? - Проверить, присутствует ли div
  • #msg_element - Получить the PageObject :: Elements :: Element

Вам потребуется либо вручную определить метод, либо создать виджет для редактируемых элементов содержимого.

Определить вручную Setter

Contenteditable элементы могут быть введены с использованием метода #set. Вы можете использовать это для создания метода установки:

class MessagePage
  include PageObject

  div(:msg, role: "textbox")

  def msg=(value)
    msg_element.set(value)
  end
end

page = MessagePage.new(browser)
page.msg = 'your text'
p page.msg
#=> "your text"

Определить виджет

Если вам приходится иметь дело с несколькими contenteditable элементами, вы должны создать виджет для избавьте от необходимости вручную создавать каждый из сеттеров.

class Contentedtiable < PageObject::Elements::Element
  def self.accessor_methods(widget, name)
    #
    # Set text
    #
    widget.send('define_method', "#{name}=") do |value|
      self.send("#{name}_element").set(value)
    end

    #
    # Get text
    #
    widget.send('define_method', "#{name}") do
      self.send("#{name}_element").text
    end
  end

  PageObject.register_widget :contenteditable, self, :element
end

class MyPage
  include PageObject

  contenteditable(:msg, tag_name: 'div', role: 'textbox')
end

page = MyPage.new(browser)
page.msg = 'your text'
p page.msg
#=> "your text"
0 голосов
/ 30 января 2020

Как только вы нажмете на текстовое поле, вы можете отправить ключи к нему. Так что-то вроде

msg.click затем msg.send_keys ('blah')

Или, может быть, это текстовое поле, с которым вы можете работать напрямую, в зависимости от того, как закодирована ваша страница. send_keys ('бла') Может работать напрямую.

...