-
Notifications
You must be signed in to change notification settings - Fork 19
Added API tests for file upload and file download #13
base: master
Are you sure you want to change the base?
Changes from all commits
f89ec76
c4666a3
90e1907
e9de42d
dbdcbc4
4f6b9f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; | ||
} | ||
|
||
@When("I download the file") | ||
public void iDownloadTheFile() { | ||
webResponse = get(filePath); | ||
} | ||
|
||
@Then("The file is successfully downloaded") | ||
public void theFileIsSuccessfullyDownloaded() { | ||
webResponse.then().statusCode(200); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
import java.net.URL; | ||
import java.util.stream.IntStream; | ||
|
||
|
||
|
||
public class StepDefinitions { | ||
private WebDriver driver; | ||
private String sessionId; | ||
|
@@ -32,7 +34,7 @@ public class StepDefinitions { | |
private final String BASE_URL = "https://www.saucedemo.com"; | ||
private SauceUtils sauceUtils; | ||
|
||
@Before | ||
@Before("not @api") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -61,7 +63,7 @@ public void setUp(Scenario scenario) throws MalformedURLException { | |
sauceUtils = new SauceUtils(sauceREST); | ||
} | ||
|
||
@After | ||
@After("not @api") | ||
nadvolod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void tearDown(Scenario scenario){ | ||
driver.quit(); | ||
sauceUtils.updateResults(!scenario.isFailed(), sessionId); | ||
|
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 | ||
nadvolod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
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 |
There was a problem hiding this comment.
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)
andwhen().get(path)
with Cucumber