Skip to content

Instantly share code, notes, and snippets.

@fijiaaron
Last active March 13, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijiaaron/8eb62e0eb2647cc2133e9dd3899c69f8 to your computer and use it in GitHub Desktop.
Save fijiaaron/8eb62e0eb2647cc2133e9dd3899c69f8 to your computer and use it in GitHub Desktop.
Demonstrate how to check that a file can be downloaded
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class BookDownload_StepDefs
{
// STUB
class BookStoreAPI {
public BookStoreAPI login(String username, String password) {return this;}
public BookStoreAPI addItem(String book) {return this;}
public BookStoreAPI purchase(String paymentMethod) {return this;}
public Response send() { return null; }
public String getAuthToken() { return ""; }
}
BookStoreAPI bookstoreAPI = new BookStoreAPI();
WebDriver driver = new ChromeDriver();
Response response;
String downloadURL;
byte[] pdfFile;
@Given("I have paid for my book")
public void pay_for_book()
{
// USING A REST API TO AVOID SELENIUM STEPS
response = bookstoreAPI.login("username", "password")
.addItem("book")
.purchase("paymentMethod")
.send();
}
@When("I go to download page")
public void go_to_download_page()
{
driver.manage().addCookie(new Cookie("authToken", bookstoreAPI.getAuthToken()));
driver.get("downloadURL");
}
@When("I download the PDF version of the book")
public void download_book() throws Exception
{
downloadURL = driver.findElement(By.id("downloadPDFLink")).getAttribute("href");
}
@Then("the file should be downloadable")
public void check_download()
{
// USE A REST CLIENT TO CHECK THAT A FILE CAN BE DOWNLOADED
response = RestAssured.head(downloadURL);
assertThat(response.getStatusCode(), is(equalTo(200)));
assertThat(response.getContentType(), is(equalTo("application/pdf")));
}
@Then("I should be able to read the PDF file")
public void read_pdf() throws IOException
{
// USE A REST CLIENT TO ACTUALLY DOWNLOAD A FILE
Response response = RestAssured.get(downloadURL);
if (response.statusCode() != 200) { throw new RuntimeException("Book did not download"); }
assertThat(response.getContentType(), is(equalTo("application/pdf")));
pdfFile = response.asInputStream().readAllBytes();
assertThat(pdfFile.length, is(greaterThan(0)));
}
}
Feature: Download book
Scenario:
Given I have paid for my book
When I go to the download page
And I download the PDF version of the book
Then the file should be downloadable
And I should be able to read the PDF file
@mmerrell-sauce
Copy link

I don't think you need the "throws Exception" on line 53, do you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment