首页 > 解决方案 > 如何使用页面对象 gem 导航到链接?

问题描述

调用方法时如何使用页面对象 gem 导航到页面?

class SidePage
 include PageObject

link(:create, text: /Create/)

def navigate_to(link)
  if link == 'Test'
    create
  else
    navigate_to "http://test.jprr.w3test/#{link}" # --> here i need to navigate on else condition.
  end
end

我需要根据 #{link} 文本在 else 条件下动态导航到给定的链接。

标签: rubyselenium-webdriverwatirpage-object-gem

解决方案


您不能#navigate_to在内部调用,#navigate_to因为它将进入无限循环。有几种方法可以解决这个问题。

最简单的方法是以不同的方式命名您的方法。部分好处是很明显,此页面#navigate_to与其他页面不同。

def navigate_or_click(link)
    if link == 'Test'
        create
    else
        navigate_to "http://test.jprr.w3test/#{link}"
    end
end

如果你想坚持使用#navigate_to方法名,你只需要调用适当的浏览器方法:

def navigate_or_click(link)
    if link == 'Test'
        create
    else
        browser.goto "http://test.jprr.w3test/#{link}" # or platform.navigate_to
    end
end

推荐阅读