Skip to content
This repository was archived by the owner on Feb 10, 2020. It is now read-only.

Added API tests for file upload and file download #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<artifactId>saucerest</artifactId>
<version>1.0.39</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/io/cucumber/FileHandlerStepDefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.cucumber;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.restassured.response.Response;
import org.junit.Assert;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.post;

public class FileHandlerStepDefs
{
private String filePath;
private Response webResponse;

@Given("I have a File")
public void iHaveAFile() {
filePath = "https://the-internet.herokuapp.com/download/some-file.txt";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use RestAssured's given().baseUri(url) and when().get(path) with Cucumber

}

@When("I download the file")
public void iDownloadTheFile() {
webResponse = get(filePath);
}

@Then("The file is successfully downloaded")
public void theFileIsSuccessfullyDownloaded() {
webResponse.then().statusCode(200);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time, you don't want to download the file -- just check that it can be downloaded:

Instead of get(url).statusCode(200);
you can do head(url).statusCode(200); and save bandwidth.

Unless, of course, you're inspecting the file, as you're doing here. But do you really want to inspect the file, or is this just a contrived example?

}

@Then("The file is the correct type")
public void theFileIsTheCorrectType()
{
Assert.assertTrue(webResponse.getHeader("Content-Disposition").contains("some-file.txt"));
}

@Given("I have not attached any file")
public void iHaveNotAttachedAnyFile() {
//do nothing
}

@When("I upload the file")
public void iUploadTheFile() {
webResponse = post("https://the-internet.herokuapp.com/upload");
}

@Then("The file upload process fails")
public void theFileUploadProcessFails() {
webResponse.then().statusCode(500);
}
}
6 changes: 4 additions & 2 deletions src/test/java/io/cucumber/StepDefinitions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.net.URL;
import java.util.stream.IntStream;



public class StepDefinitions {
private WebDriver driver;
private String sessionId;
Expand All @@ -32,7 +34,7 @@ public class StepDefinitions {
private final String BASE_URL = "https://www.saucedemo.com";
private SauceUtils sauceUtils;

@Before
@Before("not @api")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why separate API/Not API. I think the most common use case would be to use the API in conjunction with Selenium -- you sign up, navigate to the download page, and then get the download URL in Selenium, then use the HTTP client to do the actual download.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if I don't do this then a VM session is created for every single one of my API tests. I don't need a VM for my API test.

public void setUp(Scenario scenario) throws MalformedURLException {
//Set up the ChromeOptions object, which will store the capabilities for the Sauce run
ChromeOptions caps = new ChromeOptions();
Expand Down Expand Up @@ -61,7 +63,7 @@ public void setUp(Scenario scenario) throws MalformedURLException {
sauceUtils = new SauceUtils(sauceREST);
}

@After
@After("not @api")
public void tearDown(Scenario scenario){
driver.quit();
sauceUtils.updateResults(!scenario.isFailed(), sessionId);
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/io/cucumber/features/FileDownload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@api
Feature: File download

Scenario: Download file
Given I have a File
When I download the file
Then The file is successfully downloaded

Scenario: Download correct file type
Given I have a File
When I download the file
Then The file is the correct type
7 changes: 7 additions & 0 deletions src/test/resources/io/cucumber/features/FileUpload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@api
Feature: File upload

Scenario: Upload button doesn't work with no file attached
Given I have not attached any file
When I upload the file
Then The file upload process fails