OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
605 stars 41 forks source link

How to Add Cookie before go to url #105

Closed Kenith closed 6 years ago

Kenith commented 6 years ago

Hello All,

We need to access the url directly without login by pass the cookie. We tried the following. Not working. Any ideas?

const Chromy = require('chromy')

let chromy = new Chromy({visible: true})
chromy.chain()
  .goto('http://example.com')
  .setCookie({name: 'login-token', value: 'e0fe93d7-a26f-41cb-b0a3-6357c5310324%3a94d1709c-8682-4cc9-abfb-988ec6d33481_57b75423e7254e5a%3acrx.default'})
  .end()
  .catch(e => console.log(e))

Thank you, Ken

Kenith commented 6 years ago

I tried set the cookie before go to url, still not working.

const Chromy = require('chromy')

let chromy = new Chromy({visible: true})
chromy.chain()
  .setCookie({name: 'login-token', value: 'e0fe93d7-a26f-41cb-b0a3-6357c5310324%3a94d1709c-8682-4cc9-abfb-988ec6d33481_57b75423e7254e5a%3acrx.default'})
  .goto('http://example.com')
  .end()
  .catch(e => console.log(e))
Kenith commented 6 years ago

Done

const Chromy = require('chromy')
const DOMAIN = 'http://example.com';
const DOMAIN_PATH = DOMAIN + '/';
const COOKIES = [
  {
    "url": DOMAIN,
    "expirationDate": 1528614000,
    "hostOnly": false,
    "httpOnly": false,
    "name": "test2",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "XYZ",
    "id": 1000
  },
  {
    "url": DOMAIN,
    "expirationDate": 1528614000,
    "hostOnly": false,
    "httpOnly": false,
    "name": "test",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "ABCDE",
    "id": 1001
  },
  {
    "url": DOMAIN,
    "expirationDate": 1528614000,
    "hostOnly": false,
    "httpOnly": false,
    "name": "test3",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "FOO",
    "id": 1001
  },
  {
    "url": DOMAIN,
    "expirationDate": 1528614000,
    "hostOnly": false,
    "httpOnly": false,
    "name": "login-token",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "e0fe93d7-a26f-41cb-b0a3-6357c5310324%3a94d1709c-8682-4cc9-abfb-988ec6d33481_57b75423e7254e5a%3acrx.default",
    "id": 1001
  }

]

let chromy = new Chromy({visible: true})
var chromyChain = chromy.chain();

COOKIES.forEach((cookie) => {
   chromyChain.setCookie(cookie);
})

chromyChain
  .goto('http://exaple.com')
  .end()
  .catch(e => console.log(e))
Kenith commented 6 years ago

closed the issue as solution found