Running selenium tests using Behat Drupal Extension inside DDEV containers - selenium

Problem: Get Behat Drupal Extension based testing working inside of ddev containers. This includes adding a separate selenium container to the existing configuration, being able to run Behat tests, and having the be able to reference the web host container.

Prerequisites: have a working ddev instance hosting Drupal. There are examples already to set that up, so I won't repeat that here.
The above task required the following additions:
selenium container: The container running selenium. I used the chrome standalone version. Add the following to your .ddev folder:
File: docker-compose.selenium.yml
version: '3.6'
services:
selenium:
container_name: ddev-${DDEV_SITENAME}-selenium
image: selenium/standalone-chrome-debug:3.13.0-argon
networks:
default:
aliases:
- web
The last bit is critical; the selenium container needs to know about the web container running the drupal instance to connect to it, but as it is a dependency of the web container, you can't use 'links'. You have to use the aliases approach, using the default network.
compose override: A file overriding the defaults for the web container to link the selenium container to it.
File: docker-compose.override.yml
version: '3.6'
services:
web:
depends_on:
- db
- selenium
links:
- db:db
- selenium:selenium
Behat configuration: The following Behat configuration for MinkExtension worked for me:
(modify file behat.yml)
default:
extensions:
"Behat\\MinkExtension":
goutte: null
base_url: 'http://web'
javascript_session: selenium2
selenium2:
browser: "chrome"
wd_host: http://selenium:4444/wd/hub
capabilities:
extra_capabilities:
idle-timeout: 50
base_url and wd_host entries were critical in getting this to work.
For more information on the last, see step 5 in the Behat Drupal Extension docs:
Configure your testing environment by creating a file called behat.yml with the following. Be sure that you point the base_url at the web site YOU intend to test. Do not include a trailing slash:
default:
suites:
default:
contexts:
- FeatureContext
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\DrushContext
extensions:
Drupal\MinkExtension:
goutte: ~
selenium2: ~
base_url: http://seven.l
Drupal\DrupalExtension:
blackbox: ~

Related

Selenium & Docker: I can't load custom chrome user profile

I opened the following issue a cuple days ago but apparently it isn't an issue but my problem persist. I hope someone can help me to understand what is wrong :D
https://github.com/SeleniumHQ/docker-selenium/issues/790
Resume:
I'm trying to run a test using selenium + javascript in a docker machine.
The test run ok but I can't load the user profile adding:
options.addArguments(`user-data-dir=/miCustomProfilePath`);
If I execute my test using the image node-chrome-debug I can check in the configuration that always the path is something like:
/tmp/.org.chromium.Chromium.iBLIx2/Default
Docker compose:
version: "2"
services:
selenium-hub:
image: selenium/hub:latest
container_name: selenium-hub
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome-debug:latest
depends_on:
- selenium-hub
ports:
- "5900:5900"
volumes:
- /dev/shm:/dev/shm
- /Users/osvaldo/profiles/custom_profile:/home/seluser/custom_profile
environment:
- HUB_HOST=selenium-hub
- NODE_MAX_INSTANCES=5
- NODE_MAX_SESSION=5
Javascript code:
let options = new chrome.Options();
options.addArguments(`user-data-dir=/home/seluser/custom_profile`);
driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.usingServer('http://localhost:4444/wd/hub')
.build();
I tried generate the profile from the console and running a test with a new user-data-dir.
Same issue on Debian and Mac.
Have you an idea where is the issue?
Thanks!
I figured out that we should set the capabilities of goog:chromeOptions instead of chromeOptions. It should be done automatically if we have the correct binding. I changed my selenium version to 3.14 and it sets the capabilities to goog:chromeOptions and it loads my custom chrome profile
I found it from this link
https://github.com/elgalu/docker-selenium/issues/201

Getting started testing Javascript interactions with Drupal 7, Behat 3, Selenium2

I have been stuck on how to configure my Drupal 7 site to work with Behat to test Javascript interactions.
This is the setup I am starting with. If someone could help me modify my configuration to support testing Javascript I would greatly appreciate it.
Vagrant 1.8.1 running:
- Ubuntu 14.04.2 LTS
- PHP 5.6.22-1+donate.sury.org~trusty+1 (cli)
- Drupal 7.41
- Selenium: selenium-server-standalone-2.46.0.jar
I started Selenium using the following commands:
java -jar /opt/selenium/selenium-server-standalone.jar -role hub -port 4444
java -jar /opt/selenium/selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register
It looks like Selenium is running: lsof -i -n -P | grep 4444
php 2428 vagrant 6u IPv6 22327 0t0 TCP [::1]:46489->[::1]:4444 (CLOSE_WAIT)
composer.json
{
"require-dev": {
"drush/drush": "7.1.0",
"drupal/coder": "7.2.5",
"phpunit/php-timer": "dev-master",
"pear/Console_Color2": "0.1.2",
"behat/behat": "3.1.0",
"behat/mink": "1.7.1",
"behat/mink-extension": "2.2",
"behat/mink-goutte-driver": "1.2.1",
"behat/mink-selenium2-driver": "1.3.1",
"drupal/drupal-extension": "3.2.1"
},
"config": {
"bin-dir": "bin/"
}
}
behat.yml
# behat.yml
default:
autoload: [ %paths.base%/contexts ]
extensions:
Behat\MinkExtension:
goutte: ~
selenium2: ~
base_url: http://test.dev
Drupal\DrupalExtension:
blackbox: ~
suites:
default:
paths: [ %paths.base%/features ]
filters:
contexts:
- FeatureContext
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\DrushContext
contexts/FeatureContext.php
<?php
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
}
features/test.feature
Feature: Test DrupalContext
In order to prove the Drupal context using the blackbox driver is working properly
As a developer
I need to use the step definitions of this context
#javascript
Scenario: Test the ability to find a heading in a region
Given I am on the homepage
When I click "some link"
Then I should see the heading "Some heading"
When I run test.feature behat features/test.feature I get the following output. I do not know how to get the stack trace to output. If you can tell me how I will add that information to my post.
Feature: Test DrupalContext
In order to prove the Drupal context using the blackbox driver is working properly
As a developer
I need to use the step definitions of this context
#javascript
Scenario: Test the ability to find a heading in a region # features/test.feature:7
Given I am on the homepage # Drupal\DrupalExtension\Context\MinkContext::iAmOnHomepage()
Could not open connection (Behat\Mink\Exception\DriverException)
When I click "some link" # Drupal\DrupalExtension\Context\MinkContext::assertClick()
Then I should see the heading "Some heading" # Drupal\DrupalExtension\Context\MinkContext::assertHeading()
Since you are testing a Drupal 7 site, I would recommend you to use the Drupal Extension that is provided by Behat. Looks like you have already required the necessary extension in your composer file. So no need to update the composer in this case.
However, you can use the 'drupal' driver or 'drush' driver here instead of 'blackbox', since they are much more powerful. If you have a local setup of the site, go ahead with the 'Drupal' driver. 'Drupal' driver is the strongest of the available API drivers.
Please follow the steps below to get your first scenario running:
Modify your behat.yml file as below:
Drupal\DrupalExtension:
api_driver: 'drupal'
drupal:
drupal_root: /path/to/docroot/
2. Modify your FeatureContext file, class file should extend DrupalContext, so that you can use all the ready-made step definitions that Drupal extension provides.
class FeatureContext extends DrupalContext implements SnippetAcceptingContext, Context {
In the command prompt, in your Behat parent directory, type the command:
bin/behat --dl
Don't forget to tag your scenarios or the entire feature file with #api
This would give you a list of all in-built Drupal based step definitions. Let me know how it goes.

How to open a url/webpage in codeception acceptance testing?

Testing framework: Codeception
How to open a url/webpage in codeception acceptance testing ?
You can open an url directly by method: amOnPage
PHP-Browser
If you are using Selenium you can see here: WebDriver
You should have a webdriver enabled in your config for the suite you are using. Usually PhantomJS or Selenium for acceptance testing. You should also set a base URL. i.e.:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://localhost/'
browser: phantomjs
There are two commands:
amOnPage($page) - appends $page to URL and opens
amOnUrl($URL) - opens $URL directly
So with the above config:
$I->amOnPage("works"); //opens http://localhost/works
$I->amOnPage("http://localhost/dontwork"); //error!
$I->amOnUrl("http://localhost/works"); //opens http://localhost/works

acceptance testing with selenium and codeception, browser shows blank page

I'm using codeception with Yii2 and my configuration is as follows:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://ucms.ac.ir/admin/index-test.php/'
browser: chrome
- tests\codeception\common\_support\FixtureHelper
- Yii2
config:
Yii2:
configFile: '../config/backend/acceptance.php'
tests run, and they finish successfully, but nothing appears on the new browser tab opened by selenium. I've seen some tutorials and in those tutorials browser actually shows process of testing. also, when an error occurs and a screenshot is taken by codeception for later reference, it's only a white empty page too.
I'm on ubuntu 14.10, selenium 2.47.1 and chrome 45. it also happens when I use firefox instead of chrome.
I asked same question in codeception's github repo and here's the answer:
Don't use Yii2 and WebDriver in the same suite.
My fault, this is corrected configs:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://ucms.ac.ir/admin/index-test.php/'
browser: chrome
- tests\codeception\common\_support\FixtureHelper

I can't connect to my site using Behat (but a browser is fine)

I've just started playing around with Behat, and I was loving it. Then I shut down my computer and went home for the day and when I came back, nothing worked for me any more. I've been trying to trouble-shoot this, but looks like the configurations vary so widely, I can't find anything that works for me. I'm getting the following error
Given I am on the homepage # Drupal\DrupalExtension\Context\MinkContext::iAmOnHomepage()
Could not open connection: Curl error thrown for http POST to http://localhost:4444/wd/hub/session with params: {"desiredCapabilities":{"tags":["rio","PHP 5.5.9-1ubuntu4.9"],"browser":"firefox","version":"14","ignoreZoomSetting":false,"name":"Behat feature suite","browserName":"firefox"}}
The requested URL returned error: 500 Server Error (Behat\Mink\Exception\DriverException)
I've got my behat.yml file setup as such:
default:
default:
suites:
default:
contexts:
- FeatureContext
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
extensions:
Behat\MinkExtension:
base_url: http://localhost:80/vic
goutte: ~
default_session: selenium2
browser_name: 'firefox'
selenium2:
capabilities: { "browser": "firefox", "version": "14"}
javascript_session: selenium2
Drupal\DrupalExtension:
blackbox: ~
drush:
root: /var/www/html/vic
api_driver: 'drupal'
drupal:
drupal_root: '/var/www/html/vic'
Like I said, this was previously working just fine. Now I don't know why it stopped. The error is weird to me too. the base url doesn't seem to be registering and I have no idea why it's pulling up port 4444 or going to the folder structure wd/hub/session.
Some background. I'm running this on my local machine on a local environment. I'm on a Linux box. The base url works for me in my browser, but Behat can't seem to reach it, and it's adding a weird port (should be 80), and path. It worked the other day, and nothing has changed other than a reboot (so I need to start up a service or something)?
Found the answer. Make sure selenium is running first.
java -jar selenium-server-standalone-2.45.0.jar (make sure your version is right)
Once it's running it'll give you connection info, make sure that matches in your behat.yml
extensions:
Behat\MinkExtension:
base_url: http://localhost:80
goutte: ~
javascript_session: selenium2
default_session: selenium2
browser_name: 'firefox'
selenium2:
capabilities: { "browser": "firefox", "version": "14"}
wd_host: http://127.0.0.1:4444/wd/hub
^^ do that by pointing the wd_host to the selenium instance