PageObject with Ruby - 在文本字段中设置文本仅适用于主文件
我正在自动化一个网站,该网站有一个页面,其中包含通过单选按钮选择的选项列表。当选择其中一个无线电时,会出现一个文本字段和一个选择列表。
我创建了一个文件 (test_contracting.rb),通过它我执行测试 (ruby test_contracting.rb) 和一些其他类来代表我的页面。
在我的类 ContractPage 上,我有以下元素声明:
checkbox(:option_sub_domain, :id => "option_sub_domain")
text_field(:domain, :id => "domain_text")
select_list(:tld, :id => "domain_tld")
我在 ContractPage 中创建了一个设置域配置的方法,如下所示:
def configure_domain(config={})
check_option_sub_domain
domain = config[:domain]
tld = config[:tld]
end
当我从 test_contracting.rb 调用方法 configure_domain 时,它会选择单选按钮,但是它不会用值填充该字段。参数正确进入方法。我已经使用“puts”检查过它。即使我将参数更改为像“bla”这样的通用字符串,它也不起作用。烦人的一点是,如果在 test_contracting.rb 上我调用完全相同的组件,它就可以工作。
my_page_instance = ContractPage.new(browser)
my_page_instance.domain = "bla"
my_page_instance.tld = ".com"
我发现有效的是在configure_domain方法中实现以下内容:
domain_element.value = config[:domain]
tld_element.send_keys config[:locaweb_domain]
然后它就起作用了。
我用作参考的 PageObjects 模块的文档可以在这里找到: http://rubydoc.info/github/cheezy/page-object/master/PageObject/Accessors#select_list-instance_method
你们对为什么使用该方法有任何解释吗由页面对象自动生成以设置对象的值在此范围/上下文中不起作用?
顺便说一句,一位朋友用 Java 尝试了同样的事情,但也失败了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ruby 中,所有 equals 方法(以 = 号结尾的方法)都需要有一个接收器。让我向您展示一些代码来说明原因。下面是将局部变量设置为值的代码:
下面是调用 domain= 方法的代码:
为了让 ruby 知道您正在调用一个方法而不是设置局部变量,您需要添加一个接收器。只需将上面的方法更改为此即可:
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:
and here is the code that calls the domain= method:
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:
我对 Selenium 和页面对象的世界还很陌生,但也许我最近的发现之一可能会对您有所帮助。
我发现只有当我开始在前面使用“self”时, select_list 字段的分配方法才对我有用。这就是我在页面对象代码中用来访问它的方法。例如, self.my_select_list="my select list value"
另一个注意事项 - 您提到的 send_keys 解决方法很聪明,可能有多种用途,但在我的情况下,选择列表值是可变的,并且可能有多个选项开头同一封信。
我希望这里的内容对您有用。
更新(2012 年 1 月 3 日)
在深入研究页面对象的实际 Ruby 代码时,我发现 select_list 集也在使用 send_keys,所以实际上我仍然有与上一个相同的限制我注意到直接使用 send_keys 解决方法。 叹息要学的东西太多了,时间却太少了!
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!