简体   繁体   English

使用Capybara / Selenium Webdriver测试HTML5文件上传-Ruby

[英]Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby

I have a simple modal that appears in which the user is shown the browse button to add the file to upload. 我有一个简单的模态,其中向用户显示浏览按钮以添加要上传的文件。 Due to an unknown issue, be it the fact its an HTML5 file input therefore the browser adds its own functions to it, this has become a pain to test. 由于一个未知的问题,实际上是它的HTML5文件输入,因此浏览器为其添加了自己的功能,这已经成为测试的难题。

On my page I have: 在我的页面上,我有:

       <input type="file" id="photo_upload">

Capybara offers a solution out of the box which is: Capybara提供了一个开箱即用的解决方案:

       attach_file <<upload_file_id>>, <<file_path>>

This behind the scenes executes a send_keys command to push the file_path into the path container for this input, however this simply did not work with my setup. 这在后台执行send_keys命令以将file_path推送到用于此输入的路径容器中,但是这对于我的设置根本不起作用。 I am running Firefox 25.0.1 on Windows 8. I tried both a relative path and a full path to this file, with forward and backslash combinations. 我在Windows 8上运行Firefox 25.0.1。我尝试了此文件的相对路径和完整路径,并使用正斜杠和反斜杠组合。

When I mean it did not work, I mean when my ajax script executes from clicking the button 'upload' next to it, it does not send any file object in the params. 当我说它不起作用时,我的意思是当我的ajax脚本通过单击旁边的“上传”按钮执行时,它不会在params中发送任何文件对象。

I even tried to use capybara to send the file path directly: 我什至尝试使用capybara直接发送文件路径:

       find_field(<<upload_file_id>>).native.send_keys(<<file_path>>)

Next up, was to attempt to use selenium to push it in using: 接下来,尝试使用硒将硒推入使用:

       element = driver.find_element(:id, <<upload_file_id>>)
       element.send_keys <<file_path>>

Then I tried executing script to ensure the element was visible, and then setting it: 然后,我尝试执行脚本以确保该元素可见,然后进行设置:

      element = page.execute_script(
          "document.getElementById('#{<<upload_file_id>>}').style.visibility = 'visible';                  
           document.getElementById('#{<<upload_file_id>>}').style.height = '20px'; 
           document.getElementById('#{<<upload_file_id>>}').style.width = '60px';  
           document.getElementById('#{<<upload_file_id>>}').style.opacity = 1; return 
           document.getElementById('#{<<upload_file_id>>}')")
     find_field(field_locator).native.send_keys(<<file_path>>)

This didn't work either. 这也不起作用。 Now I am completely stuck. 现在我完全被困住了。 All the help on here and google points to using the above, but it just simply does not work for my setup. 这里和google上的所有帮助都指向使用上面的方法,但这对我的设置完全不起作用。

My options as far as I can see it are to use a windows automation script and jump out of capybara, run the script, and then continue, or to directly call the upload url either from capybara using a post or calling the js ajax that currently does it. 据我所知,我的选择是使用Windows自动化脚本并跳出capybara,运行该脚本,然后继续,或者直接使用信息从capybara调用上传URL或调用当前的js ajax可以。

So I have solved it, and its not too ugly. 因此,我已经解决了它,并且它不太难看。 I used the automation route via AutoIT. 我通过AutoIT使用了自动化路线。 The bundle you download with AutoIT includes a script to exe converter and using the below script (I can not take credit for the script) I created an exe: 您通过AutoIT下载的捆绑软件中包含一个脚本到exe转换器,并使用以下脚本(我不能相信该脚本)创建了一个exe:

Local Const $dialogTitle = $CmdLine[2]
Local Const $timeout = 5

Local $windowFound = WinWait($dialogTitle, "", $timeout)

$windowFound = WinWait($dialogTitle, "", $timeout)
Local $windowHandle

If $windowFound Then
    $windowHandle = WinGetHandle("[LAST]")
    WinActivate($windowHandle)

    ControlSetText($windowHandle, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
    ControlClick($windowHandle, "", "[CLASS:Button; TEXT:&Open]")        
Else
    MsgBox(0, "", "Could not find window.")
    Exit 1
EndIf

In my capybara script, I merely run: 在我的水豚脚本中,我只运行:

find_field(<<upload_file_id>>).click
system("<<full_path>>\\file_upload.exe \"#{<<file_path>>}\" \"File Upload\"")

and it works perfectly! 而且效果很好! In fact, I think I prefer the fact it exactly mimics what a user would be doing. 实际上,我认为我更喜欢这样的事实,即它完全模仿用户的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM