This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Examples
Christian Dräger edited this page Jun 19, 2017
·
1 revision
public class ExampleUsageTest extends PageContentTester {
private static final String GITHUB_URL = "https://github.com/christian-draeger";
private static final String GOOGLE_URL = "http://www.google.de";
@Test
@FetchPage(GITHUB_URL)
public void fetch_url_via_annotation() {
assertThat(page.get().getElementCount("h1"), is(1));
}
@Test
@Fetch(protocol = Fetch.Protocol.HTTPS, urlPrefix = "en", url = "wikipedia.org")
public void fetch_page_via_annotation_and_build_url() {
assertThat(page.get().getUrl(), equalTo("https://en.wikipedia.org"));
}
@Test
@FetchPage(GITHUB_URL)
@FetchPage(GOOGLE_URL)
public void fetch_multiple_pages_via_annotation_and_get_pages_by_url_snippet() {
FetchedPage github = page.get("github");
// use some convenient methods of the page-content-tester to assert behaviours
assertThat(github.isElementPresent("h1"), is(true));
assertThat(github.getUrl(), equalTo(GITHUB_URL));
assertThat(github.getElement("h1").text(), containsString("christian-draeger"));
assertThat(github.isElementPresent("img.avatar"), is(true));
assertThat(github.getElement("img.avatar").attr("src"), containsString("githubusercontent.com"));
assertThat(github.getCookieValue("logged_in"), equalTo("no"));
assertThat(github.getCookies(), hasEntry("logged_in", "no"));
FetchedPage google = page.get("google");
assertThat(google.isElementPresent("#footer"), is(true));
}
@Test
@FetchPage(GITHUB_URL)
@FetchPage(GOOGLE_URL)
public void fetch_multiple_pages_via_annotation_and_get_pages_by_index() {
FetchedPage github = page.get(0);
FetchedPage google = page.get(1);
assertThat(github.isElementPresent("h1"), is(true));
assertThat(google.isElementPresent("#footer"), is(true));
assertThat(google.getUrl(), equalTo(GOOGLE_URL));
}
@Test
public void fetch_multiple_pages_in_test_method() {
FetchedPage github = fetchPage(GITHUB_URL);
FetchedPage google = fetchPage(GOOGLE_URL);
assertThat(github.isElementPresent("h1"), is(true));
assertThat(github.getUrl(), equalTo(GITHUB_URL));
assertThat(google.isElementPresent("#footer"), is(true));
assertThat(google.getUrl(), equalTo(GOOGLE_URL));
}
@Test
@Fetch(url = "http://whatsmyuseragent.org/", device = MOBILE)
public void fetch_page_and_emulate_mobile_device_by_annotation() {
String ua = page.get().getElement("p.intro-text").text();
assertThat(ua, containsString(page.get().getConfig().getUserAgent(MOBILE)));
}
@Test
public void do_post_request_and_check_response() throws Exception {
FetchedPage somePost = call("http://httpbin.org/post", POST, Collections.emptyMap());
JSONObject responseBody = somePost.getJsonResponse();
assertThat(responseBody.get("url"), equalTo("http://httpbin.org/post"));
}
@Test
@Fetch( url = "http://www.html-kit.com/tools/cookietester/",
setCookies = @Cookie(name = "page-content-tester", value = "wtf-666"))
public void can_set_cookie_via_annotation() {
assertThat(page.get().getPageBody(), both(containsString("page-content-tester"))
.and(containsString("wtf-666")));
}
@Test
@Fetch( url = "http://www.html-kit.com/tools/cookietester/",
setCookies = { @Cookie(name = "page-content-tester", value = "wtf-666"),
@Cookie(name = "some-other-cookie", value = "666-wtf")})
public void can_set__multiple_cookies_via_annotation() {
String body = page.get().getPageBody();
assertThat(body, both(containsString("page-content-tester"))
.and(containsString("wtf-666")));
assertThat(body, both(containsString("some-other-cookie"))
.and(containsString("666-wtf")));
}
}
To see the PageContentTester in action you can checkout or fork the corresponding example project. it will give an overview of what and how things can be done. You will also get a feeling how long a test run will take using the PageContentTester (analysing and checking dom elements within ±1000 tests that are fetching an url is getting done in less than 10sec with a proper internet connection)