用于测试的代码示例
Feature: Photo Download Test
Background:
* configure continueOnStepFailure = true
* configure followRedirects = false
Scenario:
# initial request to capture random photo
* url 'https://source.unsplash.com'
Given path 'user/c_v_r/100x100'
When method GET
Then status 302
* def imageFilePath = karate.response.header('Location')
* print 'imageFilePath-',imageFilePath
* def photoName = imageFilePath.split('?')[0].split('/')[3]
* print 'photoName-',photoName
* def imageType = imageFilePath.split('?')[1].split('&')[3].split('=')[1]
* print 'imageType-',imageType
* def fileName = photoName + '.' + imageType
* def contentType = 'image/' + imageType
# image file capture attempts
* def image = read(imageFilePath)
* def image = read('file:'+imageFilePath)
* def image = karate.read('file:'+imageFilePath)
* print 'image-',image
# 200 that contains no response value
* url imageFilePath
Given path ''
When method GET
Then status 200
* def image = response
我们希望从直接网址下载照片,但最终响应只是返回 200,没有有效负载
11:17:16.494 [com.intuit.karate.Main.main()] DEBUG com.intuit.karate - response time in milliseconds: 8281
2 < 200
2 < Connection: keep-alive
2 < Content-Length: 3576
2 < last-modified: Wed, 21 Feb 2024 18:17:16 GMT
2 < cache-control: public, max-age=31536000
2 < x-imgix-id: 865a9f7e088d398a398951413abaddd4d059a862
2 < Server: Google Frontend
2 < X-Imgix-Render-Farm: 02.66056
2 < Date: Wed, 21 Feb 2024 18:17:16 GMT
2 < Age: 0
2 < Accept-Ranges: bytes
2 < Content-Type: image/jpeg
2 < Access-Control-Allow-Origin: *
2 < Timing-Allow-Origin: *
2 < Cross-Origin-Resource-Policy: cross-origin
2 < X-Content-Type-Options: nosniff
2 < X-Served-By: cache-sjc1000139-SJC, cache-dfw-kdfw8210069-DFW
2 < X-Cache: MISS, MISS
如果我们从这里备份,我们可以捕获之前重定向(302)请求的url,但是当尝试直接从url分配或读取图像时,相对路径也包含在搜索中,因此结果是未找到文件。即使使用 url 声明文件也会导致文件未找到错误 - 见下文
有没有一种方法可以通过简单的读取('target/filename.ext')将图像简单地下载到目标文件夹以便以后使用?
我尝试了很多选择,但似乎遗漏了一些东西
01: read(imageFilePath)
<<<<
org.graalvm.polyglot.PolyglotException: java.io.FileNotFoundException: /localfilepath for test feature/https:/images.unsplash.com/photo-1608231975456-2f2d9fb1b49b?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=100&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx8MTcwODU0MTgyNQ&ixlib=rb-4.0.3&q=80&utm_campaign=api-credit&utm_medium=referral&utm_source=unsplash_source&w=100 (No such file or directory)
01: read('file:'+imageFilePath)
<<<<
org.graalvm.polyglot.PolyglotException: not found: https://images.unsplash.com/photo-1597428963794-aba7182f3abf?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=100&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx8MTcwODU0MTc4MA&ixlib=rb-4.0.3&q=80&utm_campaign=api-credit&utm_medium=referral&utm_source=unsplash_source&w=100
01: karate.read('file:'+imageFilePath)
<<<<
org.graalvm.polyglot.PolyglotException: not found: https://images.unsplash.com/photo-1597431842922-d9686a23baa6?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=100&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx8MTcwODU0MTQ5OQ&ixlib=rb-4.0.3&q=80&utm_campaign=api-credit&utm_medium=referral&utm_source=unsplash_source&w=100
我认为这对你不起作用,因为你正在使用 read ,它需要一个文件或文件路径,但你正在从重定向中的位置标头传递一个 URL。
这是一个如何实现您所需要的工作示例。我为此使用了示例中的重定向位置 URL。
Scenario: Download image to target and read it test
#make request to api which returns an image/jpeg
Given url "https://images.unsplash.com/photo-1597428963794-aba7182f3abf?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=100&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx8MTcwODU0MTc4MA&ixlib=rb-4.0.3&q=80&utm_campaign=api-credit&utm_medium=referral&utm_source=unsplash_source&w=100"
When method get
Then status 200
#the response is what holds the image, use karate.write to write it to target folder
#writes the bytes of object to a path which will always be relative to the "build" directory (typically target)
Then karate.write(response, 'testing.jpg')
#read back the image to a var and do what you need with it
* def testImage = read('file:' + 'target/testing.jpg')