chrome-cookie-implant

Chrome extension that implants cookies and Java client library to use the extension with Selenium

License

License

Categories

Categories

Ant Build Tools
GroupId

GroupId

com.github.mike10004
ArtifactId

ArtifactId

chrome-cookie-implant
Last Version

Last Version

1.5.16
Release Date

Release Date

Type

Type

jar
Description

Description

chrome-cookie-implant
Chrome extension that implants cookies and Java client library to use the extension with Selenium
Project URL

Project URL

https://github.com/mike10004/chrome-cookie-implant
Source Code Management

Source Code Management

https://github.com/mike10004/chrome-cookie-implant

Download chrome-cookie-implant

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mike10004/chrome-cookie-implant/ -->
<dependency>
    <groupId>com.github.mike10004</groupId>
    <artifactId>chrome-cookie-implant</artifactId>
    <version>1.5.16</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mike10004/chrome-cookie-implant/
implementation 'com.github.mike10004:chrome-cookie-implant:1.5.16'
// https://jarcasting.com/artifacts/com.github.mike10004/chrome-cookie-implant/
implementation ("com.github.mike10004:chrome-cookie-implant:1.5.16")
'com.github.mike10004:chrome-cookie-implant:jar:1.5.16'
<dependency org="com.github.mike10004" name="chrome-cookie-implant" rev="1.5.16">
  <artifact name="chrome-cookie-implant" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mike10004', module='chrome-cookie-implant', version='1.5.16')
)
libraryDependencies += "com.github.mike10004" % "chrome-cookie-implant" % "1.5.16"
[com.github.mike10004/chrome-cookie-implant "1.5.16"]

Dependencies

compile (7)

Group / Artifact Type Version
com.google.guava : guava jar 28.1-jre
org.slf4j : slf4j-api jar 1.7.25
com.google.code.gson : gson jar 2.8.5
org.seleniumhq.selenium : selenium-chrome-driver jar 3.141.59
org.seleniumhq.selenium : selenium-support jar 3.141.59
com.google.code.findbugs : jsr305 jar 3.0.1
com.github.mike10004 : crxtool-core jar 0.16

test (3)

Group / Artifact Type Version
com.github.mike10004 : xvfb-testing jar 0.19
io.github.bonigarcia : webdrivermanager jar 3.7.1
org.slf4j : slf4j-jdk14 jar 1.7.25

Project Modules

There are no modules declared in this project.

Travis build status AppVeyor build status Maven Central

Chrome Cookie Implant

This is a Chrome extension that allows you to execute an HTTP GET request to add cookies to your Chrome profile. The request is made to a chrome-extension:// URL whose host is the the extension ID and whose query parameters contain URL-encoded JSON objects that contain the cookie data.

This is useful in web testing because the Chrome Extensions API for cookie management is more powerful than the WebDriver cookies API. See the Chrome cookies API for a list of properties a cookie can have.

The companion Java client library uses Selenium to make the GET request to implant cookies.

Maven

The Java client library (including the extension):

<dependency>
    <groupId>com.github.mike10004</groupId>
    <artifactId>chrome-cookie-implant</artifactId>
    <version>1.5.15</version>
</dependency>

If you want just the Chrome extension:

<dependency>
    <groupId>com.github.mike10004</groupId>
    <artifactId>chrome-cookie-implant</artifactId>
    <version>1.5.15</version>
    <type>crx</type>
</dependency>

Using the extension

You can use the provided Java client library to install the extension and implant cookies, or you can use the extension in any language by grabbing the CRX artifact, installing it, and making your own HTTP requests.

With the Java client library

import java.io.*;
import java.util.Collections;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.mike10004.chromecookieimplant.*;

// ...

File crxFile = File.createTempFile("chrome-cookie-implant", ".crx");
ChromeCookieImplanter implanter = new ChromeCookieImplanter();
try (OutputStream out = new FileOutputStream(crxFile)) {
    implanter.copyCrxTo(out);
}
ChromeOptions options = new ChromeOptions();
options.addExtensions(crxFile);
ChromeDriver driver = new ChromeDriver(options);
ChromeCookie cookie = ChromeCookie.builder("https://www.example.com/")
        .name("my_cookie_name")
        .value("my_cookie_value")
        .build();
CookieImplantResult result = implanter.implant(Collections.singleton(cookie), driver).get(0);
System.out.println(result);
driver.quit();

With Selenium in another language

You can grab the CRX artifact from the Maven repository and provide it to a webdriver in your preferred environment.

The src/test/js directory contains this NodeJS example using the Selenium JavaScript bindings. See package.json for a declaration of dependencies.

const {Builder, By, until} = require('selenium-webdriver');
require('chromedriver');
const crxData = require('fs').readFileSync('/path/to/chrome-cookie-implant.crx');
const crxBase64 = new Buffer(crxData).toString('base64');
const driver = new Builder()
    .forBrowser('chrome')
    .withCapabilities({
        'browserName': 'chrome',
        'chromeOptions': {
            extensions: [crxBase64]
        }
    }).build();

const cookie = {
    url: "https://www.example.com/",
    domain: ".www.example.com",
    path: "/",
    name: "my_cookie_name",
    value: "my_cookie_value",
    expirationDate: (new Date().getTime() / 1000) + (24 * 60 * 60) // expires tomorrow
};
const encodedCookieJson = encodeURIComponent(JSON.stringify(cookie));
const extensionId = "kaoadjmhchcekjlnhdmeennkgjeacdio";
const cookieImplantUrl = "chrome-extension://" + extensionId + "/manage.html?implant=" + encodedCookieJson;
driver.get(cookieImplantUrl);
driver.findElement(By.id('output'))
    .then(outputElement => driver.wait(until.elementTextContains(outputElement, 'all_implants_processed')))
    .then(outputElement => outputElement.getText())
    .then(text => {
        const output = JSON.parse(text);
        const implant = output.implants[0];
        console.log('success', implant.success);
        if (implant.success) {
            console.log('cookie', implant.savedCookie);
        }
        return implant.savedCookie;
    }).then(() => driver.quit());

The Java client library makes things a bit easier because the extension file is an embedded resource and you don't have to know the extension ID beforehand in order to construct the appropriate URL.

Acknowledgements

Icon by Adèle Foucart, CC 3.0 via The Noun Project.

Versions

Version
1.5.16
1.5.15
1.5.14
1.5.12
1.5.11
1.5.10
1.5.9