crossbrowsertesting / selenium-php-behat3

A quick guide to using Behat3 with CBT
2 stars 2 forks source link

How to work w/ Mink Extension? #2

Open bartonhammond opened 6 years ago

bartonhammond commented 6 years ago

My project works w/ https://github.com/Behat/MinkExtension. Here is my yml:

  extensions:
    Behat\MinkExtension:
      browser_name: 'chrome'
      goutte: ~
      javascript_session: selenium2
      selenium2:
        wd_host: http://0.0.0.0:4444/wd/hub
        capabilities: { "browser": "chrome", "version": "", 'chrome': {'switches':['--start-maximized']}}
      #base_url: https://www.twlw.dev
      base_url: twlw.wpengine.com

How would I update this to work w/ CBT?

bartonhammond commented 6 years ago

I found where the various product's drivers are: https://github.com/Behat/MinkExtension/tree/master/src/Behat/MinkExtension/ServiceContainer/Driver

I assume we would add a CrossBrowserTestingFactory.php

bartonhammond commented 6 years ago

I created a new vendor/behat/mink-extension/src/Behat/MinkExtension/ServiceContainer/Driver/CrossBrowserTestingFactory.php.

And I updated the vendor/behat/mink-extension/src/Behat/MinkExtension/ServiceContainer/MinkExtension.php appropriately.

CrossBrowserTestingFactory.php

<?php

/*
 * This file is part of the Behat MinkExtension.
 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
class CrossBrowserTestingFactory extends Selenium2Factory
{
    /**
     * {@inheritdoc}
     */
    public function getDriverName()
    {
        return 'cross_browser_testing';
    }
    /**
     * {@inheritdoc}
     */
    public function configure(ArrayNodeDefinition $builder)
    {
        $builder
            ->children()
                ->scalarNode('user')->defaultValue(getenv('CROSSBROWSERTESTING_USERNAME'))->end()
                ->scalarNode('key')->defaultValue(getenv('CROSSBROWSERTESTING_ACCESS_KEY'))->end()
                ->scalarNode('server')->defaultValue('crossbrowsertesting.com')->end()            
                ->scalarNode('browser')->defaultValue('chrome')->end()
                ->append($this->getCapabilitiesNode())
            ->end()
        ;
    }
    /**
     * {@inheritdoc}
     */
    public function buildDriver(array $config)
    {
        $config['wd_host'] = sprintf('https://%s:%s@%s/wd/hub', $config['user'], $config['key'], $config['server']);
        return parent::buildDriver($config);
    }
    protected function getCapabilitiesNode()
    {
        $node = parent::getCapabilitiesNode();
        $node
            ->children()
                ->scalarNode('name')->end()
                ->scalarNode('build')->end()            
                ->booleanNode('record_video')->end()
                ->booleanNode('record_network')->end()
                ->scalarNode('max_duration')->end()
                ->scalarNode('os_api_name')->end()
                ->scalarNode('browser_api_name')->end()
                ->scalarNode('resolution')->end()
            ->end()
        ;
        return $node;
    }
}

And I created new behat-cbt.yml

default:
  suites:
    default:
      paths: ["%paths.base%/features/temp/home.feature"]
      local_screenshots: false      
      contexts: [ThinkWellContext]

  extensions:
    Behat\MinkExtension:
      default_session: my_session
      base_url: https://twlw.wpengine.com
      browser_name: 'chrome'      
      sessions:
        my_session:
          cross_browser_testing:
            server: "crossbrowsertesting.com"
            user: "<clip>"
            key: "<clip>"
            capabilities: 
              name: "ThinkWell"
              build: "1.0.0"
              record_video: true
              record_network: true
              max_duration: "14400"
              os_api_name: "Mac10.12"
              browser_api_name: "Chrome62x64"
              resolution: "1920x1200"

    SensioLabs\Behat\PageObjectExtension:
      namespaces:
        page: [Page]
        element: [Page]

And output from run:

~/projects/thinkwell-livewell/wp-content/themes/jumpoff (master)$ vendor/bin/behat --config behat-cbt.yml
Feature: Home
  In order to navigate the site
  As a visitor
  I want to test navigation

  Background:                           # features/temp/home.feature:6
    Given I reset "all" data            # ThinkwellContext::iResetData()
      │ Reset Post Data
      │ Reset User Program Data
      │ Reset User Data
      │ 
    Given I am a visitor                # ThinkwellContext::iAmAVisitor()
    When I go to the "Home" page        # ThinkwellContext::iGoToThePage()
      Expected to be on "https://twlw.wpengine.com/" but found "" instead (SensioLabs\Behat\PageObjectExtension\PageObject\Exception\UnexpectedPageException)
    And I take a screenshot "home page" # ThinkwellContext::iTakeAScreenshot()

  @javascript
  Scenario: I can see multiple links            # features/temp/home.feature:13
    Then I should see "Programmes" link         # ThinkwellContext::iShouldSeeLink()
    And I should see "How It Works" link        # ThinkwellContext::iShouldSeeLink()
    And I should see "Self-Help/Resources" link # ThinkwellContext::iShouldSeeLink()
    And I should see "Try It Free" link         # ThinkwellContext::iShouldSeeLink()
    And I take a screenshot "Home page"         # ThinkwellContext::iTakeAScreenshot()

--- Failed scenarios:

    features/temp/home.feature:13

1 scenario (1 failed)
9 steps (2 passed, 1 failed, 6 skipped)

I see no evidence this reaches CBT. I tried WireShark and don't see any requests.

Can you help me? I'm stuck.

bartonhammond commented 6 years ago

I now have a Mink Extension Project that demonstrates my problem. It contains

behat.yml that demonstrates it works locally. behat-cbt.yml that attempts to run on CrossBrowserTesting.

I forked MinkExtension and add the Factory Driver: CrossBrowserTestingFactory.php - see https://github.com/bartonhammond/MinkExtension/tree/master/src/Behat/MinkExtension/ServiceContainer/Driver

To replicate the problem, just do following steps:

composer install composer selenium-server composer behat or composer behat-cbt The behat-cbt requires user and key from CBT free trial

I'd appreciate help.

deviantintegral commented 4 years ago

I've been able to run tests with Edge using the following config. I don't think there's any need for custom code.

  extensions:
    Behat\MinkExtension:
      browser_name: edge
      base_url: http://local
      sessions:
        default:
          # wd_host is specified as an environment variable. It is not
          # specified here because the config overrides the environment. For
          # example:
          # export BEHAT_PARAMS='{"extensions" : {"Behat\\MinkExtension" : {"sessions" : {"default" : {"selenium2" : {"wd_host" : "https://user%40example.com:AUTHKEY@crossbrowsertesting.com/wd/hub"}}}}}}'
          # If you get "Invalid configuration for path "testwork.mink.sessions.default": You cannot set multiple driver types for the same session"
          # after setting this, it's because you're trying to run another driver
          # (like the chromedriver) while this environment variable is defined.
          selenium2:
            # wd_host: "https://user%40example.com:AUTHKEY@crossbrowsertesting.com/wd/hub"
            capabilities:
              name: "Behat"
              extra_capabilities:
                build: "1.0.0"
                record_video: "true"
                record_network: "true"
                max_duration: 300