minkphp / MinkGoutteDriver

Goutte driver for Mink framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow to set user agent for goutte driver in behat

kaystrobach opened this issue · comments

After digging in the code, i found no easy solution to set the user agent of the behat browser.
With selenium tests i can simply use a real browser, but sadly some applications block unknown browsers ... so please add an option for that ;)

commented

In fact there is:

# behat.yml
default:
  extensions:
    Behat\MinkExtension\Extension:
      base_url: http://192.168.33.120/
      javascript_session: zombie
      goutte:
        guzzle_parameters:
          curl.options:
            CURLOPT_HTTPHEADER: ["Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4;"]
      selenium2: ~
      zombie: ~

Since User-Agent is basically a HTTP header, then you can set headers like so.

nice i tried this way:

    /**
     * @Given /^(?:|I )am using "(?P<userAgent>[^"]+)" browser$/
     *
     */
    public function iAmUsingBrowser($userAgent) {
        if(get_class($this->getSession()->getDriver()) === 'Behat\Mink\Driver\GoutteDriver') {
            /** @var \Behat\Mink\Driver\GoutteDriver $driver */
            $driver = $this->getSession()->getDriver();
            if(array_key_exists($userAgent, $this->userAgents)) {
                $driver->setRequestHeader('User-Agent', $this->userAgents[$userAgent]);
            }
        }
    }

where i also had a list of browsers in the protected variable, your solution is better.

Perhaps you should add this to the behat docs then ;)

commented

There is some CLI argument that will show all possible yaml options to you in fact.

ok, not afaik i searched for something similar for quite a while.

bin/behat --help
Usage:
 behat [--init] [-f|--format="..."] [--out="..."] [--lang="..."] [--[no-]ansi] [--[no-]time] [--[no-]paths] [--[no-]snippets] [--[no-]snippets-paths] [--[no-]multiline] [--[no-]expand] [--story-syntax] [-d|--definitions="..."] [--name="..."] [--tags="..."] [--cache="..."] [--strict] [--dry-run] [--stop-on-failure] [--rerun="..."] [--append-snippets] [--append-to="..."] [features]

Arguments:
 features             Feature(s) to run. Could be:
                      - a dir (features/)
                      - a feature (*.feature)
                      - a scenario at specific line (*.feature:10).
                      - all scenarios at or after a specific line (*.feature:10-*).
                      - all scenarios at a line within a specific range (*.feature:10-20).

Options:
 --init               Create features directory structure.

 --format (-f)        How to format features. pretty is default.
                      Default formatters are:
                      - pretty: Prints the feature as is.
                      - progress: Prints one character per step.
                      - html: Generates a nice looking HTML report.
                      - junit: Generates a report similar to Ant+JUnit.
                      - failed: Prints list of failed scenarios.
                      - snippets: Prints only snippets for undefined steps.
                      Can use multiple formats at once (splitted with ",")
 --out                Write formatter output to a file/directory
                      instead of STDOUT (output_path).
 --lang               Print formatter output in particular language.
 --ansi               Whether or not to use ANSI color in the output.
                      Behat decides based on your platform and the output
                      destination if not specified.
 --no-ansi
 --time               Whether or not to show timer in output.
 --no-time
 --paths              Whether or not to print sources paths.
 --no-paths
 --snippets           Whether or not to print snippets for undefined steps.
 --no-snippets
 --snippets-paths     Whether or not to print details about undefined steps
                      in their snippets.
 --no-snippets-paths
 --multiline          Whether or not to print multiline arguments for steps.
 --no-multiline
 --expand             Whether or not to expand scenario outline examples
                      tables.

 --no-expand
 --story-syntax       Print *.feature example.
                      Use --lang to see specific language.
 --definitions (-d)   Print all available step definitions:
                      - use -dl to just list definition expressions.
                      - use -di to show definitions with extended info.
                      - use -d 'needle' to find specific definitions.
                      Use --lang to see definitions in specific language.

 --name               Only execute the feature elements which match
                      part of the given name or regex.
 --tags               Only execute the features or scenarios with tags
                      matching tag filter expression.

 --cache              Cache parsed features into specified path.
 --strict             Fail if there are any undefined or pending steps.
 --dry-run            Invokes formatters without executing the steps & hooks.
 --stop-on-failure    Stop processing on first failed scenario.
 --rerun              Save list of failed scenarios into new file
                      or use existing file to run only scenarios from it.
 --append-snippets    Appends snippets for undefined steps into main context.
 --append-to          Appends snippets for undefined steps into specified class.
 --help (-h)          Display this help message.
 --verbose (-v)       Increase verbosity of exceptions.
 --version (-V)       Display this behat version.
 --config (-c)        Specify config file to use.
 --profile (-p)       Specify config profile to use.
commented

@stof mentioned it somewhere, but I can't seem to find exact task on GitHub.

hi, maybe you mean

bin/behat --config-reference

image
I'm using the stable 2.4 branch, there seems to be no such command ...

@kaystrobach that's because you use Behat 2.x. Only Behat 3.x has this option

I'm using the stable 2.4 branch

2.4 is not the stable branch. It is the version which is not maintained anymore since 2 years. the 2.x stable branch is 2.5

so what version do you suggest?