Manage cookies

For one of the biggest beer brewers of the world it is required to enter your birthdate, before you get access the main website. This functionality is made in flash and selenium WebDriver can not interact with it. After entering your birthdate it gets validated and a cookie will be set. The good thing of Selenium WebDriver is that you are able to set cookies to your browser session.

<code lang="java">
  public void addCookieToBypassLogin() throws Exception {
    driver.get("http://www.heineken.com/");
    Cookie cookie = new Cookie("AgeGatewayDob", "dob=1986-11-22");
    driver.manage().addCookie(cookie);
    driver.get("http://www.heineken.com/nl/Home.aspx");
    assertThat(driver.findElement(
        By.cssSelector("a.hdp-header-logo")).isDisplayed(),
        Matchers.equalTo(true));
  }
</code>
<code lang="java">
  public void clearCookies() {
    driver.get("http://techblog.polteq.com/testshop");
    driver.manage().deleteAllCookies();
  }
</code>

Keep in mind that it is sometimes required to reload the web application. This depends on the web application you are testing.

Leave a Reply

Your email address will not be published. Required fields are marked *