在这里,我在一个类中有两个方法,每个方法都有一个参数,我想利用路由优势。如果我使用 cucumber ,我如何传递参数我的意思是如果我这样使用,我如何将参数从步骤定义传递给方法:
特征:
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 |
步骤定义:
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):
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]]
}
最佳答案
这是 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
关于ruby - 当我有多个带参数的方法时,如何使用 pageobject 导航路由?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27079179/