Skip to content

Instantly share code, notes, and snippets.

View slawekradzyminski's full-sized avatar

Slawek Radzyminski slawekradzyminski

View GitHub Profile
private Predicate<Fluent> ajaxCallCompleted = new Predicate<Fluent>() {
@Override
public boolean apply(Fluent fluent) {
return (Boolean) ((JavascriptExecutor) getDriver()).executeScript("return (window.jQuery != null) && (jQuery.active === 0);");
}
}
private static final String URL = "https://resttesttest.com/";
private static final String SUCCESS_TEXT = "HTTP 200 OK";
@Test
public void downloadNoSelenium() throws URISyntaxException, IOException {
Request request = new Request();
String linkToCheck = "http://www.developsense.com/courses/RapidSoftwareTesting.pdf";
request.setURIToCheck(linkToCheck);
assertThat(request.getHTTPStatusCodeFromResponse()).isEqualTo(SC_OK);
}
class Request {
private URI linkToCheck;
private WebDriver driver;
Request() {
}
void setURIToCheck(String linkToCheck) throws URISyntaxException {
this.linkToCheck = new URI(linkToCheck);
public class FileDownloadTest extends FluentTestNg {
private static final String FILE_TO_DL_SELECTOR = "ul li a";
private static final String URL = "http://www.developsense.com";
@Test
public void statusCode200() throws IOException, URISyntaxException {
Request request = prepareRequest();
assertThat(request.getHTTPStatusCodeFromResponse()).isEqualTo(SC_OK);
}
Request(WebDriver driver) {
this.driver = driver;
}
private void addCookies(BasicHttpContext httpContext) {
BasicCookieStore cookies = getCurrentDriverCookies(driver.manage().getCookies());
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookies);
}
private BasicCookieStore getCurrentDriverCookies(Set<Cookie> cookies) {
@Test
public void getDownload() throws Exception {
Request request = prepareRequest();
File downloadedFile = request.downloadFile();
assertThat(downloadedFile).isNotNull();
}
File downloadFile() throws Exception {
File downloadedFile = File.createTempFile("download", ".tmp");
HttpResponse fileToDownload = getHttpResponse();
FileUtils.copyInputStreamToFile(fileToDownload.getEntity().getContent(), downloadedFile);
return downloadedFile;
}
private static final String EXPECTED_MD5 = "c3fb273e2843808968d68120121f2c74";
@Test
public void getDownloadPlusMd5() throws Exception {
Request request = prepareRequest();
File downloadedFile = request.downloadFile();
assertThat(calculateMd5(downloadedFile)).isEqualTo(EXPECTED_MD5);
}
private String calculateMd5(File downloadedFile) throws IOException {
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import static org.apache.http.HttpStatus.SC_OK;
import static org.assertj.core.api.Assertions.assertThat;
public class DownloadNoSelenium {
@Test