2016-11-15 9 views
0

3種類のユーザーを持つサイトのログインページがあります。ユーザは、ユーザがクリックするタイプに応じて、自分のタイプをクリックし、un/pwフィールドが表示されます。しかし、ページソースには3つのフォームがすべて存在しますが、どのタイプが選択されているかによって2つのフォームが隠されています。私は、ユーザーの各タイプをログテストする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 

、ここで私は、フォームのために見るものです:

私featurecontextファイルで
<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> 

は、私はこれらの機能を持っている:

$インターフェースがUSER1またはuser2のためである
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); 
} 

。私がいる問題は、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

私の回答が助けてくれることを願っています。問題があれば教えてください。明日の具体的なコードを追加します。 – lauda

答えて

1

フォームごとに異なるセレクタが必要です。

最初の2つの方法については、要素を入力してフィールドをクリックする必要があります。これはCSSセレクタを使用するためです。
たとえば、次の要素を見つける - >(クリック)し、要素を見つける - >のsetValue()

1)あなたは、CSSを使用する必要があります別のセレクタで

loginWithUser1($username, $password){...} 
loginWithUser2($username, $password){...} 
loginWithUser3($username, $password){...} 

を種類ごとにメソッドを作成以下のような要素のためのセレクタ:

#user1 input[name=username] 
#user1 input[name=password] 

2)パラメータに基づいて、アレイと同じ方法を使用して読み取り/セレクタを構築

login($username, $password, $role) 
{ 

} 

3)ページオブジェクト機能を使用する
このオプションは、2番目のメソッドと同じメソッドを使用しますが、配列からセレクタを読み込む代わりに、getElement()メソッドを使用し、そのオブジェクトで必要なメソッドを使用します。ここをクリックしてくださいworking with elements

私は最後のオプションを試してみることをお勧めします。

Tip: Avoid passing parameters in the method that you don't need like $btn from your login method.The method should know about the button element, you need to pass data only.

関連する問題