Verifying File Download using Rest Assured.
In the previous post, we studied about finding broken links using Rest Assured. In this post, we will learn about verifying file download using Rest Assured.
We will first download the file for which we will perform the verification of size. We will download rest-assured-3.0.5-dist.zip file from Rest Assured Download Section.
We will save the file inside our eclipse project.
Now let us look at an example of verifying file download using Rest Assured:
import org.testng.Assert; import org.testng.annotations.Test; import java.io.File; import static io.restassured.RestAssured.*; public class GetRequestTest { @Test public void verifyFileDownload () { int fileSize; File filePath = new File (System.getProperty("user.dir")+"/resources/rest-assured-3.0.5-dist.zip"); fileSize = (int)filePath.length(); System.out.println("The actual value is "+fileSize); byte [] expectedValue = given () .get("http://dl.bintray.com/johanhaleby/generic/rest-assured-3.0.5-dist.zip") .asByteArray();
System.out.println("The expected value is "+expectedValue.length); Assert.assertEquals(fileSize, expectedValue.length); } }
In the above verifyFileDownload method, first, we store the size of the file in fileSize variable. Then we start with given method and in get method, we specify the Url of the file which we need to verify (it is the same file which is present in our project resource folder) and then read the byte array and store it in the expectedValue variable.
Now we will just assert using the TestNG assertion whether the file size matched with the actual file. If file size matches then the test will pass.
Output:
Comments
Post a Comment