Last active
June 13, 2017 19:18
-
-
Save sauceaaron/63369083e61c66fec0e8db09885810e7 to your computer and use it in GitHub Desktop.
check the size of a downloaded file in a test using webdriverio and superagent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var should = require("chai").should(); | |
| var request = require("superagent"); | |
| var page = { | |
| url: "http://saucelabs.github.io/training-test-page/", | |
| title: "I am a page title - Sauce Labs", | |
| logo: { selector: "img:first-of-type", filename: "saucelogo.png", size: 9666 } | |
| }; | |
| describe("download file", function() { | |
| it("should be the right size", function() { | |
| // use webdriverio to get the url of the file to download | |
| browser.url(page.url); | |
| var title = browser.getTitle(); | |
| title.should.equal(page.title); | |
| var logo_url = browser.getAttribute(page.logo.selector, "src"); | |
| logo_url.should.contain(page.logo.filename); | |
| // use superagent to download and verify the file size | |
| // you can check the content length without actually downloading the file with a HEAD request | |
| request.head(logo_url).end(function(error, response) { | |
| var contentLength = parseInt(response.headers["content-length"]); | |
| contentLength.should.equal(page.logo.size); | |
| }) | |
| // you can also download the full file contents to inspect with a GET request | |
| request.get(logo_url).end(function(error, response) { | |
| var contentLength = parseInt(response.headers["content-length"]); | |
| var size = response.body.length; | |
| contentLength.should.equal(size); | |
| }) | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment