Karate框架从URL直接下载图片至目标文件夹的技术问询
Karate中实现图片下载到目标文件夹的方法
问题背景
在使用Karate进行图片下载测试时,遇到以下问题:
- 初始请求返回302重定向,可捕获图片真实URL,但直接用
read()读取该URL会报错,因为Karate会将URL当作本地文件路径处理 - 直接请求图片URL返回200状态码,但无法获取到响应体中的图片内容
测试代码示例
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
遇到的具体问题
请求图片URL返回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
尝试直接读取重定向URL时,Karate会将其解析为本地文件路径,触发文件未找到错误:
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
解决方案
使用Karate的karate.write()方法结合HTTP请求的二进制响应体,可直接将图片写入目标文件夹,后续通过本地路径读取。
修改后的测试代码
Feature: Photo Download Test Background: * configure continueOnStepFailure = true * configure followRedirects = false Scenario: # 获取图片的重定向URL * url 'https://source.unsplash.com' Given path 'user/c_v_r/100x100' When method GET Then status 302 * def imageUrl = karate.response.header('Location') # 解析文件名与存储路径 * def photoName = imageUrl.split('\\?')[0].split('/')[3] * def imageType = imageUrl.split('\\?')[1].split('&')[3].split('=')[1] * def fileName = photoName + '.' + imageType * def targetPath = 'target/' + fileName # 请求图片并写入本地文件 * url imageUrl When method GET Then status 200 * karate.write(response, targetPath) # 验证文件可正常读取 * def downloadedImage = read(targetPath) * print '图片已成功下载并可读取'
关键说明
karate.write(response, targetPath):Karate处理二进制响应时会自动转为字节数组,该方法可直接将字节数组写入指定路径的文件read(targetPath):文件写入完成后,直接通过本地路径即可读取内容,无需额外前缀- 若
target目录不存在,可手动添加创建步骤:* java.io.File('target').mkdirs()
内容的提问来源于stack exchange,提问作者mike
相关产品推荐
相关产品推荐

