如何使用Karate UI Automation上传pdf文件? [英] How can to upload the pdf file using Karate UI Automation?
问题描述
相关问题:可以通过空手道驱动程序上传/下载文件吗?,能否请您帮我创建空手道Ui代码,以便在以下结构中上传excel pdf:
Related the issue: Can upload / download files at Karate Driver?, Could you please help me to create the karate Ui code for upload excel pdf in this structure:
<div class="col-sm-6">
<div class="form-group shiny-input-container">
<label>Faça o upload do seu arquivo</label>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-default btn-file">
Browse...
<input id="file_input" name="file_input" type="file" style="display: none;" data-shinyjs-resettable-id="file_input" data-shinyjs-resettable-type="File" data-shinyjs-resettable-value="" class="shinyjs-resettable shiny-bound-input">
</span>
</label>
<input type="text" class="form-control" placeholder="No file selected" readonly="readonly">
</div>
<div id="file_input_progress" class="progress progress-striped active shiny-file-input-progress">
<div class="progress-bar"></div>
</div>
</div>
</div>
我尝试在下面使用此源代码但未成功:
I tried to use this source code below without success:
* def uri = 'http://the-internet.herokuapp.com/upload'
* def uploadSelector = '#file-upload'
* def submitSelector = '#file-submit'
# this function is for getting the full path of a file that is necessary to use with selenium sendKeys method when
# a file. I agree with the fact that every folder in Karate would contain the files used within the feature. Nevertheless
# having it results in a duplication of files if a lot of features use the same files. In this example I put the file in a
# separate folder. Maybe a Karate builtin function for retrieving the full path of a file in this specific case would be
# useful
* def fullPathFile =
"""
function(arg) {
return Java.type('examples.Utility').getFullPath(arg);
}
"""
* def pdfFile = fullPathFile('files/pdf-test.pdf')
Given driver uri
And waitFor(uploadSelector).input(pdfFile)
When submit().click(submitSelector)
And delay(5000)
Then match driver.text('#content > div > h3') == 'File Uploaded!'
And match driver.text('#uploaded-files') contains 'pdf-test.pdf'
推荐答案
首先阅读以下答案: https://stackoverflow. com/a/61904351/143475 -因为Chrome本机集成支持driver.inputFile()
,该版本在0.9.6.RC4
first read this answer: https://stackoverflow.com/a/61904351/143475 - because the Chrome native integration supports driver.inputFile()
which is available in 0.9.6.RC4
文件上传是浏览器自动化中要解决的一个众所周知的难题.我们需要社区提供一些帮助,但这是我刚刚使用空手道机器人进行实验的一个演示:
File upload is a well-known hard problem to solve in browser automation. We will need some contributions from the community, but here is a demo I just experimented with using Karate Robot: https://github.com/intuit/karate/tree/develop/karate-robot
Feature:
Scenario:
* driver 'http://the-internet.herokuapp.com/upload'
* robot { app: '^Chrome', highlight: true }
* robot.click('choose-file.png')
* robot.input('/Users/pthomas3/Desktop')
* robot.input(Key.ENTER)
* robot.click('file-name.png')
* robot.input(Key.ENTER)
* delay(1000)
* click('#file-submit')
* delay(2000)
* screenshot()
您可以在此处看到执行死刑的视频: https://twitter.com/ptrthomas/status /1253373486384295936
You can see a video of the execution here: https://twitter.com/ptrthomas/status/1253373486384295936
我能想到的其他选择:
a)使用Karate的API测试功能执行multipart
文件上传: https://github.com/intuit/karate#multipart-file -实际上,在大多数情况下,这足以完成"您的流程.例如,对于您在上面看到的完全相同的流程,它看起来像这样:
a) Use Karate's API testing capabilities to perform a multipart
file upload: https://github.com/intuit/karate#multipart-file - this is actually in most cases sufficient to "complete" the flow you have. For example for this exact same flow you see above, it looks like this:
* url 'http://the-internet.herokuapp.com/upload'
* multipart file file = { read: 'billie.png', filename: 'billie.png', contentType: 'image/png' }
* method post
通常,您可能需要添加一个或两个cookie,您可以从"nofollow noreferrer>浏览器.rel =" nofollow noreferrer> API测试/HTTP客户端.
And typically you may need to add a cookie or two, which you can easily pass from the browser to the API test / HTTP client.
b)另一个选择是我尚未尝试过的方法,您可以伪造"用户界面中执行文件上传并将其替换为其他内容的部分,如果它可以使您前进的话.引用此: https://twitter.com/KarateDSL/status/1248996522357739521
b) The other option is something I haven't tried yet, you can "fake" the part of the UI that does the file-upload and replace it with something else, if it can get you forward in your flow. Refer this: https://twitter.com/KarateDSL/status/1248996522357739521
这篇关于如何使用Karate UI Automation上传pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!