【问题标题】:How can I upload a PDF file using Karate UI Automation? [duplicate]如何使用空手道 UI 自动化上传 PDF 文件? [复制]
【发布时间】:2020-04-23 16:15:05
【问题描述】:

相关问题:Can upload / download files at Karate Driver?,请您帮我创建空手道 Ui 代码以在此结构中上传 excel PDF:

<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>

我尝试在下面使用此源代码但没有成功:

* 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'

【问题讨论】:

    标签: user-interface karate


    【解决方案1】:

    编辑:首先阅读这个答案:https://stackoverflow.com/a/61904351/143475 - 因为 Chrome 原生集成支持 0.9.6.RC4 中提供的driver.inputFile()

    文件上传是浏览器自动化中众所周知的难题。我们需要社区的一些贡献,但这里有一个我刚刚尝试使用空手道机器人的演示: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

    我能想到的其他选项:

    a) 使用 Karate 的 API 测试功能执行 multipart 文件上传:https://github.com/intuit/karate#multipart-file - 实际上在大多数情况下足以“完成”您拥有的流程。例如,对于您在上面看到的完全相同的流程,它看起来像这样:

    * url 'http://the-internet.herokuapp.com/upload'
    * multipart file file = { read: 'billie.png', filename: 'billie.png', contentType: 'image/png' }
    * method post
    

    通常您可能需要添加一两个 cookie,您可以轻松地将其从 the browser 传递到 API test / HTTP client

    b) 另一个选项是我还没有尝试过的,你可以“伪造”执行文件上传的 UI 部分并用其他东西替换它,如果它可以让你在你的流程中前进。参考这个:https://twitter.com/KarateDSL/status/1248996522357739521

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2020-09-08
      • 1970-01-01
      • 2021-08-14
      • 2021-08-10
      相关资源
      最近更新 更多