selecting elements using variables in ruby /watir

你离开我真会死。 提交于 2019-12-25 08:03:22

问题


The real issue I am encountering is the delay on population of drop down lists in a web page.

The call i am using in the script is :-

   clickOndropDown(":id","dropDownAutocomplete","ABC",@b)

where 1. clickOndropDown is the method invoked 2. id is the element selector (which can be :id,xpath, or :css [which are different selectors in my html code for different dropdown boxes] to handle all cases within the code) 3. ABC is the element to be selected from the drop down list 4. @b is the browser object

the function definition is as :

    def clickOndropDown(identifier,selector,targetVal,br)

            begin
                puts "starting off"     # gets printed
                br.element("#{identifier}" => "#{selector}").fire_event :click #--- does not work--- 
                # br.element(:id => "#{selector}").fire_event :click           #--- works 
                puts "inside selector pass 1"

                br.element(:link, targetVal).fire_event :click  
               ...
               ...
               # rest of the code

It does not throw any exception..just does the push statement before the selection statement.

is there any wild card handler for # (--"#{identifier}" ) so that I can write a single code for handling id,xpath,or css


回答1:


I am surprised that there is no exception. I would expect a MissingWayOfFindingObjectException.

The problem is that the identifier is a String instead of a Symbol. The line that is not working is equivalent to:

br.element(":id" => "dropDownAutocomplete")

Which is not the same as:

br.element(:id => "dropDownAutocomplete")

Notice the difference between the String, ":id", and the Symbol, :id.

You should change the method call to send a Symbol and change the method to no longer change it to a String:

def clickOndropDown(identifier,selector,targetVal,br)
  br.element(identifier => selector).fire_event :click
end

clickOndropDown(:id,"dropDownAutocomplete","ABC",@b)

If you want to have a more versatile method, you would be better off to have the clickOndropDown accept a Hash for the locator. This would allow you to accept multiple locator criteria.

def click_on_dropdown(br, target_val, locator)
  br.element(locator).fire_event :click
end

# Call for the original example
click_on_dropdown(@b, "ABC", id: "dropDownAutocomplete")

# Call with multiple selectors
click_on_dropdown(@b, "ABC", id: "dropDownAutocomplete", name: "name_value")


来源:https://stackoverflow.com/questions/40242546/selecting-elements-using-variables-in-ruby-watir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!