Search in sources :

Example 1 with Cookie

use of de.otto.jlineup.config.Cookie in project jlineup by otto-de.

the class BrowserTest method shouldDoAllTheScreenshotWebdriverCalls.

@Test
public void shouldDoAllTheScreenshotWebdriverCalls() throws Exception {
    // given
    final Long viewportHeight = 500L;
    final Long pageHeight = 2000L;
    UrlConfig urlConfig = new UrlConfig(ImmutableList.of("/"), 0f, ImmutableList.of(new Cookie("testcookiename", "testcookievalue")), ImmutableMap.of(), ImmutableMap.of("key", "value"), ImmutableMap.of("key", "value"), ImmutableList.of(600, 800), 5000, 0, 0, 0, 3, "testJS();", 5);
    Config config = configBuilder().withBrowser(FIREFOX).withUrls(ImmutableMap.of("testurl", urlConfig)).withWindowHeight(100).build();
    testee.close();
    testee = new Browser(parameters, config, fileService, browserUtilsMock);
    ScreenshotContext screenshotContext = ScreenshotContext.of("testurl", "/", 600, true, urlConfig);
    ScreenshotContext screenshotContext2 = ScreenshotContext.of("testurl", "/", 800, true, urlConfig);
    when(webDriverMock.executeScript(JS_DOCUMENT_HEIGHT_CALL)).thenReturn(pageHeight);
    when(webDriverMock.executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL)).thenReturn(viewportHeight);
    when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File(getFilePath("screenshots/http_url_root_ff3c40c_1001_02002_before.png")));
    when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_SIZE_CALL)).thenReturn(3L);
    when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_STATUS_LOADED_CALL)).thenReturn(false).thenReturn(true);
    // when
    testee.takeScreenshots(ImmutableList.of(screenshotContext, screenshotContext2));
    // then
    verify(webDriverWindowMock, times(2)).setSize(new Dimension(600, 100));
    verify(webDriverWindowMock, times(2)).setSize(new Dimension(800, 100));
    verify(webDriverMock, times(2)).executeScript(JS_SCROLL_TO_TOP_CALL);
    verify(webDriverMock, times(2)).executeScript("testJS();");
    verify(webDriverMock, times(10)).executeScript(JS_DOCUMENT_HEIGHT_CALL);
    verify(webDriverMock, times(5)).get("testurl/");
    verify(webDriverMock, times(2)).executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL);
    verify(webDriverOptionsMock, times(2)).addCookie(new org.openqa.selenium.Cookie("testcookiename", "testcookievalue"));
    verify(webDriverMock, times(2)).executeScript(String.format(JS_SET_LOCAL_STORAGE_CALL, "key", "value"));
    verify(webDriverMock, times(2)).executeScript(String.format(JS_SET_SESSION_STORAGE_CALL, "key", "value"));
    verify(webDriverMock, times(8)).executeScript(String.format(JS_SCROLL_CALL, 500));
}
Also used : Cookie(de.otto.jlineup.config.Cookie) UrlConfig(de.otto.jlineup.config.UrlConfig) UrlConfig(de.otto.jlineup.config.UrlConfig) Config(de.otto.jlineup.config.Config) Dimension(org.openqa.selenium.Dimension) File(java.io.File) Browser(de.otto.jlineup.browser.Browser) Test(org.junit.Test)

Example 2 with Cookie

use of de.otto.jlineup.config.Cookie in project jlineup by otto-de.

the class Browser method setCookiesPhantomJS.

void setCookiesPhantomJS(List<Cookie> cookies) {
    if (cookies == null)
        return;
    for (Cookie cookie : cookies) {
        StringBuilder cookieCallBuilder = new StringBuilder(String.format("document.cookie = '%s=%s;", cookie.name, cookie.value));
        if (cookie.path != null) {
            cookieCallBuilder.append("path=");
            cookieCallBuilder.append(cookie.path);
            cookieCallBuilder.append(";");
        }
        if (cookie.domain != null) {
            cookieCallBuilder.append("domain=");
            cookieCallBuilder.append(cookie.domain);
            cookieCallBuilder.append(";");
        }
        if (cookie.secure) {
            cookieCallBuilder.append("secure;");
        }
        if (cookie.expiry != null) {
            cookieCallBuilder.append("expires=");
            SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            String asGmt = df.format(cookie.expiry.getTime()) + " GMT";
            cookieCallBuilder.append(asGmt);
            cookieCallBuilder.append(";");
        }
        cookieCallBuilder.append("'");
        ((JavascriptExecutor) getWebDriver()).executeScript(cookieCallBuilder.toString());
    }
}
Also used : Cookie(de.otto.jlineup.config.Cookie) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with Cookie

use of de.otto.jlineup.config.Cookie in project jlineup by otto-de.

the class Browser method setCookies.

void setCookies(List<Cookie> cookies) {
    if (cookies == null)
        return;
    for (Cookie cookie : cookies) {
        org.openqa.selenium.Cookie.Builder cookieBuilder = new org.openqa.selenium.Cookie.Builder(cookie.name, cookie.value);
        if (cookie.domain != null)
            cookieBuilder.domain(cookie.domain);
        if (cookie.path != null)
            cookieBuilder.path(cookie.path);
        if (cookie.expiry != null)
            cookieBuilder.expiresOn(cookie.expiry);
        cookieBuilder.isSecure(cookie.secure);
        getWebDriver().manage().addCookie(cookieBuilder.build());
    }
}
Also used : Cookie(de.otto.jlineup.config.Cookie) org.openqa.selenium(org.openqa.selenium)

Example 4 with Cookie

use of de.otto.jlineup.config.Cookie in project jlineup by otto-de.

the class BrowserTest method shouldSetCookiesThroughJavascript.

@Test
public void shouldSetCookiesThroughJavascript() throws Exception {
    // given
    Cookie cookieOne = new Cookie("someName", "someValue", "someDomain", "somePath", new Date(10000L), true);
    Cookie cookieTwo = new Cookie("someOtherName", "someOtherValue", "someOtherDomain", "someOtherPath", new Date(100000067899L), false);
    // when
    testee.setCookiesPhantomJS(ImmutableList.of(cookieOne, cookieTwo));
    // then
    verify(webDriverMock).executeScript("document.cookie = 'someName=someValue;path=somePath;domain=someDomain;secure;expires=01 Jan 1970 00:00:10 GMT;'");
    verify(webDriverMock).executeScript("document.cookie = 'someOtherName=someOtherValue;path=someOtherPath;domain=someOtherDomain;expires=03 Mar 1973 09:47:47 GMT;'");
}
Also used : Cookie(de.otto.jlineup.config.Cookie) Date(java.util.Date) Test(org.junit.Test)

Example 5 with Cookie

use of de.otto.jlineup.config.Cookie in project jlineup by otto-de.

the class BrowserTest method shouldSetCookies.

@Test
public void shouldSetCookies() {
    // given
    ArgumentCaptor<org.openqa.selenium.Cookie> cookieCaptor = ArgumentCaptor.forClass(org.openqa.selenium.Cookie.class);
    Cookie cookieOne = new Cookie("someName", "someValue", "someDomain", "somePath", new Date(10000L), true);
    Cookie cookieTwo = new Cookie("someOtherName", "someOtherValue", "someOtherDomain", "someOtherPath", new Date(10000000000L), false);
    // when
    testee.setCookies(ImmutableList.of(cookieOne, cookieTwo));
    // then
    verify(webDriverOptionsMock, times(2)).addCookie(cookieCaptor.capture());
    List<org.openqa.selenium.Cookie> capturedCookies = cookieCaptor.getAllValues();
    Assert.assertEquals("someName", capturedCookies.get(0).getName());
    Assert.assertEquals("someValue", capturedCookies.get(0).getValue());
    Assert.assertEquals("someDomain", capturedCookies.get(0).getDomain());
    Assert.assertEquals("somePath", capturedCookies.get(0).getPath());
    Assert.assertEquals(new Date(10000L), capturedCookies.get(0).getExpiry());
    Assert.assertTrue(capturedCookies.get(0).isSecure());
    Assert.assertEquals("someOtherName", capturedCookies.get(1).getName());
    Assert.assertEquals("someOtherValue", capturedCookies.get(1).getValue());
    Assert.assertEquals("someOtherDomain", capturedCookies.get(1).getDomain());
    Assert.assertEquals("someOtherPath", capturedCookies.get(1).getPath());
    Assert.assertEquals(new Date(10000000000L), capturedCookies.get(1).getExpiry());
    Assert.assertFalse(capturedCookies.get(1).isSecure());
}
Also used : Cookie(de.otto.jlineup.config.Cookie) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Cookie (de.otto.jlineup.config.Cookie)5 Test (org.junit.Test)3 Date (java.util.Date)2 Browser (de.otto.jlineup.browser.Browser)1 Config (de.otto.jlineup.config.Config)1 UrlConfig (de.otto.jlineup.config.UrlConfig)1 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 org.openqa.selenium (org.openqa.selenium)1 Dimension (org.openqa.selenium.Dimension)1