Search code examples
rspeccapybarapoltergeist

RSPEC+Capybara + poltergeist :How to send keys to windows pop up dialgue opened from browser


I want to send keys to FileUpload window using Capybara . JavaScript Driver : Poltergeist.

UI : File Upload Window

click_button 'BrowseBtn'
sleep(1)
popup = page.driver.browser.window_handles.last
page.within_window(popup) do
page.find('body').native.send_key(pathToInvalidTestLicenseFile.to_s)
page.find('body').native.send_key(:Enter)
end

I am not sure of the following things. a) How do I verify whether popup dialog got opened after clicking browse button? I have taken screen shot after clicking browse button , but in the screen shot browse window doesnot come?Is it expected? b) How do I verify whether popup diaglog has received the keys?


Solution

  • You can't send_keys to the system file selection dialog, you have to call #attach_file with identifiers for the file input element. Given html like

    <input type='file' id='my_file_input' name='my_file_input>
    

    You would do something like

    attach_file('my_file_input', pathToInvalidTestLicenseFile)
    

    in your test. The one issue that comes up a lot with file fields is that they are often hidden on the page, and replaced with another element that is stylable. In that case you need to use execute_script to modify the css of the file input to make it visible before calling attach_file. The css that you need to change would depend on how the element is hidden, however if it was just set to opacity: 0 then something like

    page.execute_script("$('#my_file_input').css('opacity', 1);")
    page.attach_file('my_file_input', pathToInvalidTestLicenseFile)
    

    should work (assuming your using jQuery on your page - swap for standard DOM methods if not).

    =========================

    As a second issue - when you do have code that opens another window, rather than using driver specific methods and manual sleeps you should be using something like

    popup = window_opened_by { click_button 'BrowseBtn' }
    

    This doesn't work in this case because you're not opening a new browser window, you're triggering the opening of a system dialog box.