Coder Social home page Coder Social logo

Comments (16)

stof avatar stof commented on June 12, 2024

Can you provide the reproducing case (your usage of Mink that was working before and does not work anymore) ?

from mink.

mpdude avatar mpdude commented on June 12, 2024
<?php

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\RawMinkContext;

class MyAppContext extends RawMinkContext ...
{
    /**
     * @BeforeScenario
     */
    public function setUpBeforeScenario(BeforeScenarioScope $scope)
    {
        // Workaround for https://github.com/minkphp/Mink/issues/798:
        $this->getSession()->start();

        // $this->getSession()->setCookie() fails as of minkphp/Mink ^1.8
        if (\function_exists('xdebug_is_debugger_active') && xdebug_is_debugger_active()) {
            $this->getSession()->setCookie('XDEBUG_SESSION', 'behat');
        }
    }

    ....

from mink.

lpeabody avatar lpeabody commented on June 12, 2024

For additional context, see https://travis-ci.com/github/lpeabody/Metis/builds/153721427#L1377 for an example of where this breaks. $this->page evaluates to null here https://gitlab.com/DMore/chrome-mink-driver/-/blob/2.7.0/src/ChromeDriver.php#L1058.

It's broken out more verbosely here https://gitlab.com/DMore/chrome-mink-driver/-/issues/94.

from mink.

aik099 avatar aik099 commented on June 12, 2024

@mpdude , actually I see no issue here.

In Selenium (which emulates user actions) you can't set a cookie on a page prior to that page being visited. Same goes for dmore/chrome-mink-driver driver, which does the same as Selenium, but without Selenium in Google Chrome.

Setting headers (e.g. basic authentication, cookies, etc.) prior to page visiting is only possible in headless drivers, like MinkBrowserKitDriver and MinkZombieDriver.

The solution you came up with is correct way to do this. I'm not sure why it even worked, because you've set cookie right after browser window was opened, but prior to page being visited. In that case there is no way to determine at which URL cookie should apply.

from mink.

mpdude avatar mpdude commented on June 12, 2024

But I should be able to set a request header before requesting a page, right?

from mink.

aik099 avatar aik099 commented on June 12, 2024

But I should be able to set a request header before requesting a page, right?

@mpdude , I guess no. If you can't do this with Selenium you can't do it in dmore/chrome-mink-driver driver, that controls actual web browser.

from mink.

mpdude avatar mpdude commented on June 12, 2024

I am not sure if we're talking about the same thing: The driver needs the start() method to be called so it connects to the browser. But once that's done, you can add the cookies and/or headers, even before visiting the page.

Am I missing something?

from mink.

aik099 avatar aik099 commented on June 12, 2024

If that is how it works for that driver then it's good thing. It doesn't work that way with other drivers.

Maybe driver code needs to be updated to that it auto-starts session, when it's missing and somebody is attempting to set cookies.

from mink.

mpdude avatar mpdude commented on June 12, 2024

@aik099 I cross-posted this as an issue here and over at the driver's repo at https://gitlab.com/DMore/chrome-mink-driver/-/issues/94 because I know too little about the contracts between Mink and the drivers and I was unsure if this needs to be fixed in the driver or here.

/**
* Starts driver.
*
* Once started, the driver should be ready to visit a page.
*
* Calling any action before visiting a page is an undefined behavior.
* The only supported method calls on a fresh driver are
* - visit()
* - setRequestHeader()
* - setBasicAuth()
* - reset()
* - stop()
*
* Calling start on a started driver is an undefined behavior. Driver
* implementations are free to handle it silently or to fail with an
* exception.
*
* @throws DriverException When the driver cannot be started
*/
public function start();

Calling any action before visiting a page is an undefined behavior.
The only supported method calls on a fresh driver are [...]

  • setRequestHeader()

If "fresh" means "not started", then probably the driver should not rely on start() being called until the very last moment before visitPage()?

from mink.

adorin avatar adorin commented on June 12, 2024

@mpdude , I guess no. If you can't do this with Selenium you can't do it in dmore/chrome-mink-driver driver, that controls actual web browser.

Selenium needs the browser to navigate to the cookie domain before being able to set a cookie. The chrome driver can technically set the cookie to base_url before visiting it, as long as ->start() is called before setCookie.

But since calling setCookie() before visit() is undefined behaviour, that doesn't have much practical value.

Maybe driver code needs to be updated to that it auto-starts session, when it's missing and somebody is attempting to set cookies.

It's possible, but is it a good idea? What if mink called stop() specifically and then we just overrule that with autostart. We'd essentially be taking control over start/stop/reset.

from mink.

aik099 avatar aik099 commented on June 12, 2024

Closing as @DorianCMore agreed to fix it on driver side in https://gitlab.com/DMore/chrome-mink-driver/-/issues/94.

from mink.

lpeabody avatar lpeabody commented on June 12, 2024

I'm failing to see where he has explicitly agreed to fix this in chrome-mink-driver?

from mink.

aik099 avatar aik099 commented on June 12, 2024

@lpeabody , here is a screenshot with relevant text selected from GitLab:

GitLab_DMore_chrome-link-driver_issue94_comment

from mink.

xurizaemon avatar xurizaemon commented on June 12, 2024

There's an MR for this at https://gitlab.com/DMore/chrome-mink-driver/-/merge_requests/93 now.

Input / feedback welcome - @aik099 I'd especially welcome your thoughts on the solution, I'm pretty fresh to this!

from mink.

mpdude avatar mpdude commented on June 12, 2024

https://gitlab.com/DMore/chrome-mink-driver/-/merge_requests/93 does not fix the issue:

It fixes that you can set HTTP Basic Auth and/or request headers before calling visit(), but it does not fix calls to setCookie().

You still can call the start() method on the driver yourself and afterwards call setCookie(), even before calling visit().

I find it hard to understand why setCookie() should not be possible when setRequestHeader() is. But that might be because setCookie() is expected to have additional semantics (setting the cookie in the context of a page limits it to... the URL? Domain?) over just a "plain" request header.

from mink.

aik099 avatar aik099 commented on June 12, 2024

@xurizaemon , I've commented on that PR on GitLab website.

from mink.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.