Search in sources :

Example 56 with Cookie

use of org.openqa.selenium.Cookie in project selenium_java by sergueik.

the class VisualTest method addCookieforTopBanner.

// utils
// Add Cookie to suppress top banner animation
public void addCookieforTopBanner() {
    // Set cookie expiration to Next Month Last Date
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date nextMonthLastDay = calendar.getTime();
    // provide name, domain, path, expiration date of the cookie
    Cookie topBannerCloseCookie = // Name
    new Cookie.Builder("AA-kobiBannerClosed", "4").domain(baseURL.replaceAll("(?:https?://)([^/]+)(?:/.*)?$", "$1")).path("/").expiresOn(nextMonthLastDay).build();
    driver.manage().addCookie(topBannerCloseCookie);
}
Also used : Cookie(org.openqa.selenium.Cookie) Calendar(java.util.Calendar) Date(java.util.Date)

Example 57 with Cookie

use of org.openqa.selenium.Cookie in project webmagic by code4craft.

the class SeleniumDownloader method download.

@Override
public Page download(Request request, Task task) {
    checkInit();
    WebDriver webDriver;
    try {
        webDriver = webDriverPool.get();
    } catch (InterruptedException e) {
        logger.warn("interrupted", e);
        return null;
    }
    logger.info("downloading page " + request.getUrl());
    webDriver.get(request.getUrl());
    try {
        Thread.sleep(sleepTime);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    WebDriver.Options manage = webDriver.manage();
    Site site = task.getSite();
    if (site.getCookies() != null) {
        for (Map.Entry<String, String> cookieEntry : site.getCookies().entrySet()) {
            Cookie cookie = new Cookie(cookieEntry.getKey(), cookieEntry.getValue());
            manage.addCookie(cookie);
        }
    }
    /*
		 * TODO You can add mouse event or other processes
		 * 
		 * @author: bob.li.0718@gmail.com
		 */
    WebElement webElement = webDriver.findElement(By.xpath("/html"));
    String content = webElement.getAttribute("outerHTML");
    Page page = new Page();
    page.setRawText(content);
    page.setHtml(new Html(content, request.getUrl()));
    page.setUrl(new PlainText(request.getUrl()));
    page.setRequest(request);
    webDriverPool.returnToPool(webDriver);
    return page;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) Site(us.codecraft.webmagic.Site) Cookie(org.openqa.selenium.Cookie) PlainText(us.codecraft.webmagic.selector.PlainText) Html(us.codecraft.webmagic.selector.Html) Page(us.codecraft.webmagic.Page) WebElement(org.openqa.selenium.WebElement) Map(java.util.Map)

Example 58 with Cookie

use of org.openqa.selenium.Cookie in project ghostdriver by detro.

the class CookieTest method modifyingACookie.

@Test
public void modifyingACookie() {
    server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
    goToPage();
    driver.manage().addCookie(new Cookie("test", "newValue", "localhost", "/", null, false));
    Cookie[] cookies = getCookies();
    assertEquals(2, cookies.length);
    assertEquals("test", cookies[1].getName());
    assertEquals("newValue", cookies[1].getValue());
    assertEquals(".localhost", cookies[1].getDomain());
    assertEquals("/", cookies[1].getPath());
    assertEquals(false, cookies[1].isSecure());
    assertEquals("test2", cookies[0].getName());
    assertEquals("test2", cookies[0].getValue());
    assertEquals(".localhost", cookies[0].getDomain());
    assertEquals("/", cookies[0].getPath());
    assertEquals(false, cookies[0].isSecure());
}
Also used : Cookie(org.openqa.selenium.Cookie) Test(org.junit.Test)

Example 59 with Cookie

use of org.openqa.selenium.Cookie in project ghostdriver by detro.

the class CookieTest method gettingAllCookies.

@Test
public void gettingAllCookies() {
    server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
    goToPage();
    Cookie[] cookies = getCookies();
    assertEquals(2, cookies.length);
    Cookie cookie = driver.manage().getCookieNamed("test");
    assertEquals("test", cookie.getName());
    assertEquals("test", cookie.getValue());
    assertEquals(".localhost", cookie.getDomain());
    assertEquals("/", cookie.getPath());
    assertTrue(cookie.getExpiry() != null);
    assertEquals(false, cookie.isSecure());
    Cookie cookie2 = driver.manage().getCookieNamed("test2");
    assertEquals("test2", cookie2.getName());
    assertEquals("test2", cookie2.getValue());
    assertEquals(".localhost", cookie2.getDomain());
    assertEquals("/", cookie2.getPath());
    assertEquals(false, cookie2.isSecure());
    assertTrue(cookie2.getExpiry() == null);
}
Also used : Cookie(org.openqa.selenium.Cookie) Test(org.junit.Test)

Example 60 with Cookie

use of org.openqa.selenium.Cookie in project ghostdriver by detro.

the class CookieTest method addingACookieWithDefaults.

@Test
public void addingACookieWithDefaults() {
    server.setHttpHandler("GET", EMPTY_CALLBACK);
    goToPage();
    long startTime = new Date().getTime();
    driver.manage().addCookie(new Cookie("newCookie", "newValue"));
    Cookie[] cookies = getCookies();
    assertEquals(1, cookies.length);
    assertEquals("newCookie", cookies[0].getName());
    assertEquals("newValue", cookies[0].getValue());
    assertEquals(".localhost", cookies[0].getDomain());
    assertEquals("/", cookies[0].getPath());
    assertEquals(false, cookies[0].isSecure());
    assertEquals(false, cookies[0].isHttpOnly());
    // expiry > 19 years in the future
    assertTrue(startTime + 599184000000L <= cookies[0].getExpiry().getTime());
}
Also used : Cookie(org.openqa.selenium.Cookie) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Cookie (org.openqa.selenium.Cookie)73 Test (org.junit.Test)26 WebElement (org.openqa.selenium.WebElement)16 WebDriver (org.openqa.selenium.WebDriver)14 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)9 Date (java.util.Date)8 App (com.coveros.selenified.application.App)7 IOException (java.io.IOException)7 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)7 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)6 Test (org.testng.annotations.Test)6 Collectors (java.util.stream.Collectors)5 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)5 AuthorizeClient (org.gluu.oxauth.client.AuthorizeClient)5 Matchers (org.hamcrest.Matchers)5 Page (org.jboss.arquillian.graphene.page.Page)5 Assert (org.junit.Assert)5 AdminRoles (org.keycloak.models.AdminRoles)5