我需要自动化注册表单并创建一个新帐户,然后使用相同的帐户详细信息登录新创建的帐号和密码。我需要在一个场景中执行此操作。
功能:创建新用户并捕获用户名,密码并尝试使用这些详细信息登录。
Scenario: test
Given I am on xyz.com
When I click on register
Then I will enter required details for registration
Then I will click on submit
And I will enter new account details to login to test.
答案 0 :(得分:0)
我希望能帮到你
Feature: a sample of test
Scenario: test
Given I am on "xyz.com"
When I click on register
Then I will enter required details for registration
Then I will click on submit
And I will enter new account details to login to test
#############脚步
Given(/^I am on "([^"]*)"$/) do |website|
visit website
end
When(/^I click on register$/) do
find(:xpath, "registerbutton").click
end
Then(/^I will enter required details for registration$/) do
@username = "xpto"
@password = "xptopassword"
fill_in('camp_name', with: @username)
fill_in('camp_name', with: @password)
fill_in('othercamps', with: "etcs")
#repeat for all the camps
end
Then(/^I will click on submit$/) do
find(:xpath, "submit_button").click
end
Then(/^I will enter new account details to login to test$/) do
visit "www.myloginpage.com"
fill_in("camp_username", with: @username)
fill_in("camp_password", with: @password)
find(:xpath, "login_button").click
page.should have_content ("LoggedPAge")
end
您可以使用selenium IDE(firefox的扩展名)来查找所有xpath。
我不喜欢这个解决方案,为了更好的实现,我的建议是阅读页面对象(siteprism)和场景大纲(黄瓜),这是一个更好的代码的开始。
答案 1 :(得分:0)
除非是一个流程,否则我不会在一个场景中这样做。而且我不会去xyz.com,我也不会点击这些东西,因为这不是行为。 Page objects会帮助你。
Scenario: Register a new account
Given I do not have an account
When I register a new account
Then I can use those credentials to access the site
然后我会创建适当的步骤
Given(/^I do not have an account%/) do
@credentials = get_unique_credentials
@browser = Watir::Browser.new :firefox
end
When(/^I register a new account$/) do
visit RegistrationPage do |page|
page.name = @credentials[0]
page.password = @credentials[1]
page.register
end
end
Then(/^I can use those credentials to access the site$/) do
visit LoginPage do |page|
page.name = @credentials[0]
page.password = @credentials[1]
page.login
end
on HomePage do |page|
expect(page.something).to exist
end
end
答案 2 :(得分:0)
简单的答案是你不必在一个场景中这样做。
你在这做两件事:
让我们先处理第二个问题。
要登录,我们必须注册,以便我们得到像
这样的场景Scenario: Sign in
Given I am registered
When I sign in
Then I should be signed in
但我们如何注册?
Scenario: Register
Given I am a new user
When I register
Then I should be registered
现在我们如何实现这个
Given "I am a new user" do
@i = create_new_user
end
When "I register" do
register as: @i
end
Then "I should be registered" do
# Go look at something to see that we are registered
end
当这工作时,我们现在可以实现
Given "I am registered" do
@i = create_new_user
register as: @i
end
我们可以做到这一点,因为我们通过让“注册”方案发挥作用来创建注册能力。
现在我们可以继续登录了
这就是使用Cucumber的BDD的工作原理。您正在努力实现一些行为(通常在何时,例如注册)。然后你使用那种行为(在Givens中),这样你就可以到达一个可以实现一些新行为的地方(登录)
希望有帮助
方法create_new_user,register称为辅助方法。这些是编写更简单的步骤定义的关键。在ruby中,您可以按如下方式定义它们
module SignupStepHelper
def register
...
def create_new_user
...
end
World SignupStepHelper # makes it possible to call the methods in you step defs.