如何使用 Karate UI 自动化上传 PDF 文件? [英] How can I upload a PDF file using Karate UI Automation?
问题描述
相关问题:可以在Karate Driver上传/下载文件吗?,你能帮我创建空手道 Ui 代码,以这种结构上传 excel PDF:
<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">浏览...<input id="file_input";名称=文件输入"类型=文件"样式=显示:无;"data-shinyjs-resettable-id="file_input";data-shinyjs-resettable-type=文件"data-shinyjs-resettable-value="";class =shinyjs-resettable Shiny-bound-input"></span>标签><输入类型=文本"类=表单控制"占位符=未选择文件"只读=只读"><div id="file_input_progress";class="progress progress-striped active Shiny-file-input-progress"><div class="progress-bar"></div>
我尝试在下面使用此源代码但没有成功:
* def uri = 'http://the-internet.herokuapp.com/upload'* def uploadSelector = '#file-upload'* def submitSelector = '#file-submit'# 此函数用于获取文件的完整路径,该文件在使用 selenium sendKeys 方法时必须使用# 一份文件.我同意空手道中的每个文件夹都包含该功能中使用的文件这一事实.尽管如此# 如果许多功能使用相同的文件,则使用它会导致文件重复.在这个例子中,我把文件放在一个# 单独的文件夹.在这种特定情况下,用于检索文件完整路径的空手道内置函数可能是#有用* def fullPathFile ="功能(参数){返回 Java.type('examples.Utility').getFullPath(arg);}"* def pdfFile = fullPathFile('files/pdf-test.pdf')给定驱动程序uri和 waitFor(uploadSelector).input(pdfFile)当 submit().click(submitSelector)和延迟(5000)然后匹配 driver.text('#content > div > h3') == 'File Uploaded!'并匹配 driver.text('#uploaded-files') 包含 'pdf-test.pdf'
首先阅读此答案:https://stackoverflow.com/a/61904351/143475 - 因为 Chrome 原生集成支持 driver.inputFile()
,它在 0.9.6.RC4 中可用
文件上传是众所周知的浏览器自动化难题.我们需要来自社区的一些贡献,但这是我刚刚使用空手道机器人进行试验的演示:https://github.com/intuit/karate/tree/develop/karate-robot
特点:设想:* 驱动程序http://the-internet.herokuapp.com/upload"*机器人{应用程序:'^ Chrome',突出显示:真实}* robots.click('选择文件.png')* robots.input('/Users/pthomas3/Desktop')* 机器人.输入(键.ENTER)* robots.click('文件名.png')* 机器人.输入(键.ENTER)* 延迟(1000)* 点击('#file-submit')* 延迟(2000)* 截屏()
你可以在这里看到执行的视频:https://twitter.com/ptrthomas/status/1253373486384295936
我能想到的其他选项:
a) 使用 Karate 的 API 测试功能来执行 multipart
文件上传:https://github.com/intuit/karate#multipart-file - 这实际上在大多数情况下足以完成"你拥有的流量.例如,对于您在上面看到的完全相同的流程,它看起来像这样:
* url 'http://the-internet.herokuapp.com/upload'* 多部分文件文件 = { 读取:'billie.png',文件名:'billie.png',内容类型:'image/png' }* 方法贴
通常您可能需要添加一两个 cookie,您可以轻松地从 浏览器到API 测试/HTTP 客户端.
b) 另一个选项是我还没有尝试过的,你可以伪造";UI 的一部分,用于执行文件上传并将其替换为其他内容(如果它可以让您在流程中前进).请参考:https://twitter.com/KarateDSL/status/1248996522357739521
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'
EDIT: 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()
You can see a video of the execution here: https://twitter.com/ptrthomas/status/1253373486384295936
The other options I can think of:
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
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) 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 自动化上传 PDF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!