元素的多个定位器如何工作?

问题描述 投票:0回答:1

我有以下HTML:

<a class="js-open-modal suspend" data-track-event="interacted_with_account_manage_settings_web" 
 data-track-properties="{&quot;location&quot;:&quot;clients&quot;,&quot;button_clicked&quot;:&quot;Suspend&quot;}" data-track="always"
href="/client_suspension/new?client_id=86">Suspend</a>

并且我已经用这两个定位符声明了元素(按钮):

link(:suspend_button, :class => ["js-open-modal", "suspend"])
link(:suspend_button, :visible_text => 'Suspend')

[我尝试“破坏”第一个定位器:link(:suspend_button, :class => ["blah-blah-blah-blah", "suspend"]),并使用第二个定位器:link(:suspend_button, :visible_text => 'Suspend')进行测试。

这提供了弹性测试。

但是如果我“中断”了第二个定位器link(:suspend_button, :visible_text => 'Ssssssuspend'),则该测试不适用于第一个定位器:link(:suspend_button, :class => ["js-open-modal", "suspend"])

页面是:

class MyClientsPage < Base
  include PageObject


  link(:suspend_button, :class => ["js-open-modal", "suspend"])
  link(:suspend_button, :visible_text => 'Suspend')

end

测试是:

require 'spec_helper'
require "rspec/expectations"

describe 'Partner/client switch' do

  it 'Client account is suspended' do
    on(MyClientsPage).suspend_button
  end
end

使用Watir组合多个定位器时,幕后的魔术是什么?您能否分享更多示例。提前非常感谢!

ruby watir
1个回答
0
投票

当页面对象用相同的名称定义多个访问器时,组合定位器没有任何魔术。相反,最后一个定义获胜。

访问器方法,例如link,动态定义页面对象的方法。有效的页面对象:

    class MyClientsPage < Base
      include PageObject

      link(:suspend_button, :class => ["js-open-modal", "suspend"])
      link(:suspend_button, :visible_text => 'Suspend')
    end

已转换为类似内容:

    class MyClientsPage < Base
      include PageObject

      def suspend_button
        browser.link(:class => ["js-open-modal", "suspend"]).click
      end

      def suspend_button
        browser.link(:visible_text => 'Suspend').click
      end
    end

与所有Ruby(并非特定于Watir或Page-Object一样),如果两次定义相同的方法,则最后一个定义基本上会覆盖前一个。这就是为什么您可以修改第一个访问器而没有任何影响的原因-即您正在修改被丢弃的定义。

如果要查找具有特定类可见文本的链接,则需要一个访问器:

    class MyClientsPage < Base
      include PageObject

      link(:suspend_button, :class => ["js-open-modal", "suspend"], :visible_text => 'Suspend')
    end
© www.soinside.com 2019 - 2024. All rights reserved.