【问题标题】:How multiple locators for element are working?元素的多个定位器如何工作?
【发布时间】:2020-04-27 09:50:18
【问题描述】:

我有以下 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 =&gt; ["blah-blah-blah-blah", "suspend"]),并尝试使用第二个定位器:link(:suspend_button, :visible_text =&gt; 'Suspend')

这提供了弹性测试。

但如果我“破坏”了第二个定位器 link(:suspend_button, :visible_text =&gt; 'Ssssssuspend'),则测试不适用于第一个定位器:link(:suspend_button, :class =&gt; ["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 组合多个定位器时,幕后有什么魔力? 您能否分享更多示例。 非常感谢!

【问题讨论】:

  • 你在使用页面对象模型吗?
  • 你是说你同时在同一个页面对象中包含了这两个link 声明?
  • @JustinKo,是的,我留下了两个链接声明并运行了测试。我已经编辑了问题 -> 添加了示例代码。

标签: ruby watir


【解决方案1】:

当一个页面对象定义了多个具有相同名称的访问器时,组合定位器就没有什么魔力了。相反,最后一个定义获胜。

访问器方法,例如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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2019-05-10
    • 1970-01-01
    • 2023-03-28
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多