5

나는 poltergeist와 phantomjs를 자바 스크립트 드라이버로 사용하여 요청 테스트를 위해 capybara를 사용하여 몇 가지 테스트를 작성했습니다. Capybara : js : true로 양식 필드를 찾을 수 없습니다.

로그인 양식을 작성을위한 다음 단계

는 JS없이 잘 작동 :

Capybara::ElementNotFound: Unable to find field "Email" 

로그인 폼 자체 :

it "signs in" do 
    visit new_user_session_path 
    fill_in "Email", with: user 
    fill_in "Password", with: password||"foobar" 
    click_button "Login" 
end 

내가 JS 내 테스트를 정의하면 it "signs in", js: true do 내 테스트가 오류와 함께 실패 프론트 엔드에서 폼 생성기 및 부트 스트랩으로 simple_form을 사용하여 빌드됩니다. 필드에 레이블이 없습니다. 검색 텍스트는 자리 표시 자 속성에만 포함됩니다.

= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| 
    = f.input :email, :placeholder => User.human_attribute_name(:email), :label => false, :input_html => {:class => 'input-xlarge'} 
    = f.input :password, :placeholder => User.human_attribute_name(:password), :label => false, :input_html => {:class => 'input-xlarge'} 
    %div 
    = f.button :submit, "Login", :class => 'btn btn-large btn-primary' 

이 코드는 다음과 같은 HTML 코드

<form accept-charset="UTF-8" action="https://stackoverflow.com/users/login" class="simple_form new_user" id="new_user" method="post" novalidate="novalidate"> 
    <div style="margin:0;padding:0;display:inline"> 
    <input name="utf8" type="hidden" value="✓"> 
    </div> 
    <div class="control-group email required user_email"> 
    <div class="controls"> 
     <input class="string email required input-xlarge" id="user_email" name="user[email]" placeholder="Email" size="50" type="email" value=""> 
    </div> 
    </div> 
    <div class="control-group password required user_password"> 
    <div class="controls"> 
     <input class="password required input-xlarge" id="user_password" name="user[password]" placeholder="Password" size="50" type="password"> 
    </div> 
    </div> 
    <div> 
    <input class="btn btn btn-large btn-primary" name="commit" type="submit" value="Login"> 
    </div> 
</form> 

를 생성 어떻게 필드가 JS가 활성화 된 경우에도 발견되는 것을 보장하는 어떤 아이디어가 있습니까?

+0

작업 솔루션을 찾을 수 있었습니까? – Scott

+0

그것은 당신에게 도움이 될 것입니다이 링크를 시도하십시오 http://stackoverflow.com/questions/15784398/test-case-for-hidden-field-is-not-working-with-cucumber – Arvind

답변

3

Webrat 테스트가 어떻게 통과했는지 모르겠습니다. 내 경험에 카피 바라는 일치하는 레이블 또는 ID가없는 경우 "이메일"을 찾을 수 없습니다. JS => 사실 : 당신이 레이블을 사용하지 않기 때문에 귀하의 경우에는

는 당신이 때 렌더링되는 visit new_user_session_path 후 스크린 샷 찍기 ID

fill_in "user_email", with user.email 
# user_email is the id created by simple_form in general case. Verify yours. 
# Don't need "#" before id. 
+0

이 시도하지만, 카피 바라 'user_email'필드를 찾을 수 없습니다. 클래스 user_email을 가진 요소가 있다는 것이 문제가 될 수 있습니까? –

+1

@MichaelEf, 가능합니다. 나는 하나의 프로젝트에 모달을 숨기고있을 때 같은 것을 만났다. 오류는 '야심 찬 경기'여야하지만 '찾을 수 없음'일 수도 있습니다. 모든 HTML 코드를 두 번 확인하십시오. –

+0

오류는 여전히 ElementNotFound입니다. user_email 클래스는 simple_form에 의해 생성됩니다. 이것을 제거 할 방법을 찾지 못했습니다. 그래서 내 양식 필드를 : label => 'Email'및 'Password'로 변경했습니다. 이제 레이블이 생성되어 인쇄되었지만 js 사용 가능 테스트에는 성공하지 못했습니다. –

1

으로 현장을 찾아 HTML을 확인하는 것이 좋습니다.

visit new_user_session_path 
save_and_open_page 

아무것도 렌더링되지되는 경우, 단지 빈 HTML 문서,

config.use_transactional_fixtures = false 

는 또한 DatabaseCleaner 보석을 사용해보십시오 당신의 spec_helper.rb에 있는지 확인하십시오.

RSpec.configure do |config| 
    config.use_transactional_fixtures = false 

    config.before(:suite) do 
    DatabaseCleaner.clean_with :truncation 
    end 

    config.before(:each) do 
    if example.metadata[:js] 
     DatabaseCleaner.strategy = :truncation 
    else 
     DatabaseCleaner.strategy = :transaction 
    end 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 
관련 문제