当我有多个带参数的方法时,如何使用页面对象导航路径?

时间:2014-11-22 15:40:54

标签: ruby selenium-webdriver cucumber watir-webdriver page-object-gem

这里,我在一个类中有两个方法,每个方法都有一个参数,我想使用路由优势。如果我使用黄瓜,我如何传递参数?我的意思是如果我使用这样的话,如何将步骤定义中的参数传递给方法:

特征:

Feature: Github Test Case

Background:
  Given I am on githubpage

Scenario Outline: I should see one of the repositories
  When I click on "<user>" and select "<repo>" link
  Then I should see "Information Technology Association website repo"

Examples:
  | user   | repo        |
  | sample | sample_repo |

步骤def:

Given(/^I am on githubpage$/) do
 visit(LoginPage).do_login
end

Then(/^I should see "([^"]*)"$/) do |message|
  @current_page.text.should include message
end


When(/^I click on "([^"]*)" and select "([^"]*)" link$/) do |user, repo|
 # currently using like this
 navigate_to(GithubPage).click_on(user)
 navigate_to(GithubPage).select_repo(repo)

 # but i need like this
 navigate_to(GithubPage).select_repo

 # or
 navigate_all
end

类:

  class GithubPage
  include PageObject

  link(:repo, text: /Repositories/)

  def click_on(user)
    span_element(text: "#{user}", index: 1).click
    repo_element.click
  end

  def select_repo(repo)
    link_element(xpath: "//a[contains(text(),'#{repo}')]").when_present.click
  end
end

路线:

PageObject::PageFactory.routes = {
    :default => [[GithubPage, :click_on], [GithubPage, :select_repo]]
}

1 个答案:

答案 0 :(得分:2)

以下是来自PageObject::PageFactory的示例,其中Cheezy将参数作为路径定义的一部分传递给方法:

PageObject::PageFactory.routes = {
  :default => [[PageOne,:method1], [PageTwoA,:method2], [PageThree,:method3]],
  :another_route => [[PageOne,:method1, "arg1"], [PageTwoB,:method2b], [PageThree,:method3]]
}

问题是,当然,在定义这些路由时,您没有该参数。您需要动态加载它们。像这样的东西可能有效,但我还没有测试过它:

When /^I click on "([^"]*)" and select "([^"]*)" link$/ do |user, repo|
  PageObject::PageFactory.routes[:default].map do |route|
    route << user if route[1] == :click_on
    route << repo if route[1] == :select_repo 
  end
  navigate_all
end

但如果你要解决所有麻烦,你最好将一个块传递给PageObject :: PageFactory#on:

When /^I click on "([^"]*)" and select "([^"]*)" link$/ do |user, repo|
  on GithubPage do |page|
    page.click_on user
    page.select_repo repo
  end
end