使用 ruby​​/watir 中的变量选择元素 [英] selecting elements using variables in ruby /watir

查看:26
本文介绍了使用 ruby​​/watir 中的变量选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的真正问题是网页中下拉列表的填充延迟.

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)

哪里1. clickOndropDown 是调用的方法2. id 是元素选择器(可以是 :id,xpath 或 :css [这是我的 html 代码中针对不同下拉框的不同选择器] 来处理代码中的所有情况)3. ABC是下拉列表中要选择的元素4.@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

函数定义如下:

    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

它不会抛出任何异常..只是在选择语句之前执行 push 语句.

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

是否有 # (--"#{identifier}" ) 的通配符处理程序,以便我可以编写单个代码来处理 id、xpath 或 css

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

推荐答案

我很惊讶没有例外.我希望 MissingWayOfFindingObjectException.

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

问题在于标识符是一个 String 而不是 Symbol.不工作的行相当于:

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")

不一样的是:

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

注意 String":id"Symbol:id 之间的区别.

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

您应该更改方法调用以发送 Symbol 并将方法更改为不再将其更改为 String:

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)

如果您想拥有更通用的方法,最好让 clickOndropDown 接受定位器的 Hash.这将允许您接受多个定位器条件.

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")

这篇关于使用 ruby​​/watir 中的变量选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆