PageObject with Ruby - set text in a text field on

2019-03-02 04:52发布

问题:

I'm automating a site that has a page with a list of options selected by a radio button. When selecting one of the radios, a text field and a select list are presented.

I created a file (test_contracting.rb) that is the one through which I execute the test (ruby test_contracting.rb) and some other classes to represent my page.

On my class ContractPage, I have the following element declaration:

  checkbox(:option_sub_domain, :id => "option_sub_domain")
  text_field(:domain, :id => "domain_text") 
  select_list(:tld, :id => "domain_tld")

I've created in the ContractPage a method that sets the configuration of the domain like this:

  def configure_domain(config={})
        check_option_sub_domain
        domain = config[:domain]
        tld = config[:tld]
   end

When I call the method configure_domain from the test_contracting.rb, it selects the radio button, but it doesn't fill the field with the values. The params are getting into the method correctly. I've checked it using "puts". Even if I change the params to a general string like "bla" it doesnt work. The annoying point is that if on test_contracting.rb I call the exact same components, it works.

my_page_instance = ContractPage.new(browser)
my_page_instance.domain = "bla"
my_page_instance.tld = ".com"

What I found to work was to in the configure_domain method, implement the following:

domain_element.value = config[:domain]
tld_element.send_keys config[:locaweb_domain]

Then it worked.

The documentation for the PageObjects module that I'm using as reference can be found here: http://rubydoc.info/github/cheezy/page-object/master/PageObject/Accessors#select_list-instance_method

Do you guys have any explation on why the method auto generated by the pageobject to set the value of the object didnt work in this scope/context ?

By the way, a friend tried the same thing with Java and it failed as well.

回答1:

In ruby all equals methods (methods that end with the = sign) need to have a receiver. Let me show you some code that will demonstrate why. Here is the code that sets a local variable to a value:

domain = "blah"

and here is the code that calls the domain= method:

domain = "blah"

In order for ruby to know that you are calling a method instead of setting a local variable you need to add a receiver. Simply change your method above to this and it will work:

def configure_domain(config={})
    check_option_sub_domain
    self.domain = config[:domain]
    self.tld = config[:tld]
end


回答2:

I'm pretty new to this world of Selenium and page objects but maybe one of my very recent discoveries might help you.

I found that that assignment methods for the select_list fields only worked for me once I started using "self" in front. This is what I have used to access it within my page object code. e.g., self.my_select_list="my select list value"

Another note - The send_keys workaround you mention is clever and might do the trick for a number of uses, but in my case the select list values are variable and may have several options starting with the same letter.

I hope something in here is useful to you.

UPDATE (Jan 3/12)

On diving further into the actual Ruby code for the page object I discovered that the select_list set is also using send_keys, so in actuality I still have the same limitation here as the one I noted using the send_keys workaround directly. sigh So much to learn, so little time!