1

I have

openjdk version "1.8.0_181"
ChromeDriver 2.42.591071
Google Chrome 69.0.3497.100
Selenium build info: version: '3.14.0'

And I am running a test with the current code snippet:

<?php

namespace Facebook\WebDriver;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
ini_set("display_errors", "on");
error_reporting(E_ALL);
require_once('vendor/autoload.php');

// start Chrome with 5 second timeout
$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::chrome();
$options = new ChromeOptions();
$options->addArguments(array(
      '--headless',
      '--no-sandbox',
      '--window-size=1920,1080',
      'user-data-dir=/usr/share/nginx/html/browser/tmp/profile',
    ));
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

$driver->get('http://example.com/browser/cookies.php');

$driver->takeScreenshot('/usr/share/nginx/html/browser/tmp/screen.png');

// close the browser
$driver->quit();
echo "<img src='tmp/screen.png' />";

The code in cookies.php is:

<?php
if(isset($_COOKIE['test'])){
    echo "It is set";
}else{
    setcookie("test", "testCookie");
    echo "Setting it ...";
}

I am expecting the cookie to be re-used on the second visit and so I will get "It is set", instead I am getting "setting it ..." regardless of how many time I request the page.

I checked the path for the profile, it was created successfully.

I also tried the page in my browser and it is working properly, so what am I missing here?

Thanks

AL-Kateb
  • 2,914
  • 3
  • 20
  • 24

1 Answers1

1

You browser is created on every run from scratch. $_COOKIE is stored in browser as you know. So you cant reach unless you save cookies on exit and load on start.

Check example at : Laravel Dusk - Reuse browser with its session and cookies

And don't preload cookies before initial navigation. That doesn't work. First you should navigate to the url then load cookies. Then reload page. You will see second pageload will have cookies.

Erdinç Çorbacı
  • 1,187
  • 14
  • 17