Skip to content

Commit

Permalink
Add basic support for reading and clearing cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Goodwin committed Jun 24, 2024
1 parent 464a7a2 commit be2ff9a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nz.geek.goodwin.melinoe.framework.api.web;

import java.util.Date;

/**
* @author Goodie
*/
public interface Cookie {
static Cookie create() {
return new CookieImpl();
}

String getName();

Cookie setName(String name);

String getValue();

Cookie setValue(String value);

String getPath();

Cookie setPath(String path);

String getDomain();

Cookie setDomain(String domain);

Date getExpiry();

Cookie setExpiry(Date expiry);

boolean isSecure();

Cookie setSecure(boolean secure);

boolean isHttpOnly();

Cookie setHttpOnly(boolean httpOnly);

String getSameSite();

Cookie setSameSite(String sameSite);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package nz.geek.goodwin.melinoe.framework.api.web;

import java.util.Date;
import java.util.Objects;

/**
* @author Goodie
*/
public final class CookieImpl implements Cookie {
private String name;
private String value;
private String path;
private String domain;
private Date expiry;
private boolean isSecure;
private boolean isHttpOnly;
private String sameSite;

public CookieImpl() {
}

public CookieImpl(org.openqa.selenium.Cookie cookie) {
this.name = cookie.getName();
this.value = cookie.getValue();
this.path = cookie.getPath();
this.domain = cookie.getDomain();
this.expiry = cookie.getExpiry();
this.isSecure = cookie.isSecure();
this.isHttpOnly = cookie.isHttpOnly();
this.sameSite = cookie.getSameSite();
}

@Override
public String getName() {
return name;
}

@Override
public Cookie setName(String name) {
this.name = name;
return this;
}

@Override
public String getValue() {
return value;
}

@Override
public Cookie setValue(String value) {
this.value = value;
return this;
}

@Override
public String getPath() {
return path;
}

@Override
public Cookie setPath(String path) {
this.path = path;
return this;
}

@Override
public String getDomain() {
return domain;
}

@Override
public Cookie setDomain(String domain) {
this.domain = domain;
return this;
}

@Override
public Date getExpiry() {
return expiry;
}

@Override
public Cookie setExpiry(Date expiry) {
this.expiry = expiry;
return this;
}

@Override
public boolean isSecure() {
return isSecure;
}

@Override
public Cookie setSecure(boolean secure) {
isSecure = secure;
return this;
}

@Override
public boolean isHttpOnly() {
return isHttpOnly;
}

@Override
public Cookie setHttpOnly(boolean httpOnly) {
isHttpOnly = httpOnly;
return this;
}

@Override
public String getSameSite() {
return sameSite;
}

@Override
public Cookie setSameSite(String sameSite) {
this.sameSite = sameSite;
return this;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other instanceof Cookie otherCookie) {
return name.equals(otherCookie.getName());
} else {
return false;
}
}

@Override
public int hashCode() {
return name.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
Expand All @@ -24,6 +25,14 @@ public interface WebDriver {

List<WebElement> findElements(By by);

void clearCookie(String name);

void clearCookie(Cookie cookie);

void clearAll();

Map<String, Cookie> cookies();

void verify(final List<WebValidator> validators);

default void verify(final WebValidator... validators) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import nz.geek.goodwin.melinoe.framework.api.Session;
import nz.geek.goodwin.melinoe.framework.api.log.Logger;
import nz.geek.goodwin.melinoe.framework.api.web.By;
import nz.geek.goodwin.melinoe.framework.api.web.Cookie;
import nz.geek.goodwin.melinoe.framework.api.web.CookieImpl;
import nz.geek.goodwin.melinoe.framework.api.web.Navigate;
import nz.geek.goodwin.melinoe.framework.api.web.validation.ValidationResult;
import nz.geek.goodwin.melinoe.framework.api.web.WebDriver;
Expand All @@ -21,6 +23,8 @@
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -120,6 +124,33 @@ public List<WebElement> findElements(By by) {
return new WebElementListImpl(remoteWebDriver, screenshotTaker, pageCheckStatus, logger, by);
}

@Override
public void clearCookie(String name) {
remoteWebDriver.manage().getCookieNamed(name);
}

@Override
public void clearCookie(Cookie cookie) {
remoteWebDriver.manage().getCookieNamed(cookie.getName());
}

@Override
public void clearAll() {
remoteWebDriver.manage().deleteAllCookies();
}

@Override
public Map<String, Cookie> cookies() {
if(!pageCheckStatus.check()) {
throw new MelinoeException("Please check run at least one validator before interacting with a pages cookies.");
}

return remoteWebDriver.manage().getCookies()
.stream()
.map(CookieImpl::new)
.collect(Collectors.toMap(CookieImpl::getName, Function.identity()));
}

@Override
public void verify(List<WebValidator> validators) {
Logger verifyingLogger;
Expand Down

0 comments on commit be2ff9a

Please sign in to comment.