我有一个包含三种类型用户的网站的登录页面。用户点击他/她的类型并显示un / pw字段,具体取决于用户点击的任何类型。但是,在页面源中,所有三种形式都存在,但根据选择的类型隐藏了两种形式。我正在尝试编写一个behat测试来测试记录每种类型的用户。我的behat测试看起来像这样:
@javascript @core
Feature: Secure Login
As a User
I expect to be asked for my credentials
So that I can use the system securely
Background:
Given I am logged out
Scenario: Failed User1 Login
When I go to user1 interface
When I submit incorrect user1 credentials
Then I should stay logged out
And I should see "The username or password entered does not match our records."
Scenario: Successful User1 Login
When I go to user1 interface
When I submit correct user1 credentials
Then I should be logged in to user1 interface
Scenario: Failed User2 Login
When I go to user2 interface
When I submit incorrect user2 credentials
Then I should stay logged out
And I should see "The username or password entered does not match our records."
Scenario: Successful User2 Login
When I go to user2 interface
When I submit correct user2 credentials
Then I should be logged in to user2 interface
当我查看页面源代码时,这是我在表单中看到的内容:
<form id='user1' name='user1' method="post" role="form">
<input type="hidden" name="subsystem" value="user1"/>
<label for="un1" class="label">Username</label>
<div id="un_inst1"class="label_instruction"></div>
<input autocapitalize="off" id="un1" autocorrect="off" type="text" name="username" value="" style="margin-bottom: 10px" aria-describedby="un_inst1">
<label for="pw1" class="label">Password</label>
<div id="pw_inst1"class="label_instruction"></div>
<input type="password" id="pw1" name="password" style="margin-bottom: 10px;" aria-describedby="pw_inst1">
<div class="login_buttons">
<input type="submit" value="Go" class="btn_go input-submit">
<input type="reset" class="btn_reset input-reset" value="Reset">
<br/>
</div>
</form>
<form id='user2' name='user2' method="post" role="form">
<input type="hidden" name="subsystem" value="resident"/>
<div class="usr-login label">Resident Login</div>
<label for="un2" class="label">Username</label>
<div id="un_inst2"class="label_instruction"></div>
<input autocapitalize="off" autocorrect="off" type="text" id="un2" name="username" value="" style="margin-bottom: 10px" aria-describedby="un_inst2">
<label for="pw2" class="label">Password</label>
<div id="pw_inst2"class="label_instruction"></div>
<input type="password" id="pw2" name="password" style="margin-bottom: 10px;" aria-describedby="pw_inst2">
<div class="login_buttons">
<input type="submit" value="Go" class="btn_go input-submit">
<input type="reset" class="btn_reset input-reset" value="Reset">
<br/>
</div>
</form>
在我的featurecontext文件中,我有以下功能:
public function iSubmitIncorrectCredentials($interface)
{
$button ='Go';
return $this->login("tester" . time(), "bar", $button);
}
private function login($username, $password, $btn)
{
$this->fillField('username', $username);
$this->fillField('password', $password);
$this->pressButton($btn);
}
$ interface用于user1或user2。我遇到的问题是user1的测试通过,但是当我进入user2测试时,我得到了这个错误:
{"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept":"application/json;charset=UTF-8","Content-Length":"36","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:8643"},"httpVersion":"1.1","method":"POST","post":"{\"value\":[\"tester1479232631\\ue004\"]}","url":"/value","urlParsed":{"anchor":"","query":"","file":"value","directory":"/","path":"/value","relative":"/value","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/value","queryKey":{},"chunks":["value"]},"urlOriginal":"/session/ea0f7000-ab5c-11e6-acb9-5946ec8fdb06/element/:wdc:1479232631203/value"}} (WebDriver\Exception\InvalidElementState)
我认为正在发生的是fillField()正在尝试查找用户名字段,但有两个,所以对于user1,测试工作但是对于user2,我得到了错误。除了更改un / pw字段的名称之外,我还想了解如何处理这个问题。 感谢
答案 0 :(得分:1)
每个表单需要有不同的选择器。您可以通过以下几种方式实现此目的:
对于前两种方法,您需要使用另一种方法填充元素并单击字段,因为您将使用css选择器。
例如:找到元素 - &gt; click()并找到元素 - &gt;的setValue()
1)为每种具有不同选择器的类型创建一个方法
loginWithUser1($username, $password){...}
loginWithUser2($username, $password){...}
loginWithUser3($username, $password){...}
您需要为以下元素使用css选择器:
#user1 input[name=username]
#user1 input[name=password]
2)使用相同的方法并基于参数
从数组中构建/读取选择器 login($username, $password, $role)
{
}
3)使用页面对象功能
此选项将使用与第二个相同的方法,但不是从数组中读取选择器,而是使用getElement()方法并使用该对象所需的方法。请在此处查看示例working with elements
我建议尝试最后一个选项,如果你不能尝试使用第二个选项。
提示:避免在您不需要的方法中传递参数,例如来自登录方法的$ btn。该方法应该知道按钮元素,您只需要传递数据。