Behat with selenium 2 fail to connect to localhost - php

Im trying to setup my behat tests in selenium enviroment. For now i got selenium starting with dockers,
Thats my docker-compose.yml config:
hub:
image: selenium/hub
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome
links:
- hub
And i got my behat.yml configurated:
default:
suites:
default:
paths:
features: 'tests/features'
bootstrap: 'tests/features/bootstrap'
contexts:
- FeatureContext: ~
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://localhost
selenium2: ~
And when i launch my tests with #javascript (i suppose this launch selenium env) i get error:
Could not open connection: Curl error thrown for http POST to http://localhost:4444/wd/hub/session with params: {"desiredCapabilities":{"tags":["cli","PHP 7.0.15-1+deb.sury.org~trusty+1"],"browser":"firefox","ignoreZoomSetting":false,"name":"Behat feature suite","browserName":"firefox"}}
Failed to connect to localhost port 4444: Connection refused (Behat\Mink\Exception\DriverException)
When I get on http://localhost:4444/wd/hub/sessions i get:
{"status":13,"value":{"message":"Session [(null externalkey)] not available and is not among the last 1000 terminated sessions.\nActive sessions are[]","class":"org.openqa.grid.common.exception.GridException","stackTrace":[{"fileName":"ActiveTestSessions.java","className":"org.openqa.grid.internal.ActiveTestSessions","methodName":"getExistingSession","lineNumber":110},{"fileName":"Registry.java","className":"org.openqa.grid.internal.Registry","methodName":"getExistingSession","lineNumber":404},{"fileName":"RequestHandler.java","className":"org.openqa.grid.web.servlet.handler.RequestHandler","methodName":"getSession","lineNumber":232},{"fileName":"RequestHandler.java","className":"org.openqa.grid.web.servlet.handler.RequestHandler","methodName":"process","lineNumber":117},{"fileName":"DriverServlet.java","className":"org.openqa.grid.web.servlet.DriverServlet","methodName":"process","lineNumber":83},{"fileName":"DriverServlet.java","className":"org.openqa.grid.web.servlet.DriverServlet","methodName":"doGet","lineNumber":61},{"fileName":"HttpServlet.java","className":"javax.servlet.http.HttpServlet","methodName":"service","lineNumber":687},{"fileName":"HttpServlet.java","className":"javax.servlet.http.HttpServlet","methodName":"service","lineNumber":790},{"fileName":"ServletHolder.java","className":"org.seleniumhq.jetty9.servlet.ServletHolder","methodName":"handle","lineNumber":808},{"fileName":"ServletHandler.java","className":"org.seleniumhq.jetty9.servlet.ServletHandler","methodName":"doHandle","lineNumber":587},{"fileName":"SessionHandler.java","className":"org.seleniumhq.jetty9.server.session.SessionHandler","methodName":"doHandle","lineNumber":221},{"fileName":"ContextHandler.java","className":"org.seleniumhq.jetty9.server.handler.ContextHandler","methodName":"doHandle","lineNumber":1127},{"fileName":"ServletHandler.java","className":"org.seleniumhq.jetty9.servlet.ServletHandler","methodName":"doScope","lineNumber":515},{"fileName":"SessionHandler.java","className":"org.seleniumhq.jetty9.server.session.SessionHandler","methodName":"doScope","lineNumber":185},{"fileName":"ContextHandler.java","className":"org.seleniumhq.jetty9.server.handler.ContextHandler","methodName":"doScope","lineNumber":1061},{"fileName":"ScopedHandler.java","className":"org.seleniumhq.jetty9.server.handler.ScopedHandler","methodName":"handle","lineNumber":141},{"fileName":"HandlerWrapper.java","className":"org.seleniumhq.jetty9.server.handler.HandlerWrapper","methodName":"handle","lineNumber":97},{"fileName":"Server.java","className":"org.seleniumhq.jetty9.server.Server","methodName":"handle","lineNumber":499},{"fileName":"HttpChannel.java","className":"org.seleniumhq.jetty9.server.HttpChannel","methodName":"handle","lineNumber":310},{"fileName":"HttpConnection.java","className":"org.seleniumhq.jetty9.server.HttpConnection","methodName":"onFillable","lineNumber":257},{"fileName":"AbstractConnection.java","className":"org.seleniumhq.jetty9.io.AbstractConnection$2","methodName":"run","lineNumber":540},{"fileName":"QueuedThreadPool.java","className":"org.seleniumhq.jetty9.util.thread.QueuedThreadPool","methodName":"runJob","lineNumber":635},{"fileName":"QueuedThreadPool.java","className":"org.seleniumhq.jetty9.util.thread.QueuedThreadPool$3","methodName":"run","lineNumber":555},{"fileName":"Thread.java","className":"java.lang.Thread","methodName":"run","lineNumber":745}]}}
I suppose its a problem with configuration of behat, but im not sure, since im just started using behat and selenium. If someone got any idea what is wrong i would be very pleased to get some help.

You are missing the wd_host and capabilities options
try something like:
default:
suites:
default:
paths:
features: 'tests/features'
bootstrap: 'tests/features/bootstrap'
contexts:
- FeatureContext: ~
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
selenium2:
browser: "chrome"
wd_host: http://hub:4444/wd/hub
Also your hub, needs to be able to access your localhost.....where the tests are running.
First, try to checkout your apache/nginx etc of your server, to see if you are getting any access/visits from the selenium machine.
something like tail -f /var/log/apache/access.log
or wherever you are saving your files....if you get nothing, then obviously selenium is not accessing your machine
A different approach would be, to deploy your code for example of a test env, then you can do something in your behat.yml like:
Behat\MinkExtension:
base_url: http://my_test_host.com

Related

symfony + behat, how to run #javascripts tests in APP_ENV=test

I run /vendor/bin/behat
and tests without #javascript are running in test database, but tests in chrome, are running in dev enviroment, instead of test enviroment.
Where can I configure and run it in APP_ENV=test ?
I use symfony 5.4
My behat.yml
default:
suites:
default:
contexts:
- App\Tests\Behat\FormContext
extensions:
DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~
FriendsOfBehat\SymfonyExtension:
bootstrap: tests/bootstrap.php
Behat\MinkExtension:
browser_name: chrome
base_url: http://localhost:8000
default_session: symfony
javascript_session: chrome
sessions:
symfony:
symfony: ~
chrome:
chrome:
api_url: http://127.0.0.1:9222
validate_certificate: false
show_auto: false

How to merge two or more environments

I'm writing an acceptance test suite and I have to make the environment easily configurable, but for this to work I need to be able to merge two or more environments (geoLocation, browser, os, URLs).
If I run: .\codecept run tests\acceptance\acceptanceTestCest --env firefox the tests start running without issues with any environment I choose.
Now my problem is that if I try to merge the environments: .\codecept run tests\acceptance\acceptanceTestCest --env firefox,US I get the following message:
In Configuration.php line 306:
[Codeception\Exception\ConfigurationException]
Suite tests\acceptance\acceptanceTestCest.php was not loaded
And if I try to merge more than two environments I get this message:
Too many arguments, expected arguments "command" "suite" "test".
This is how my acceptance.suite.yml looks like:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://localhost
- REST:
depends: PhpBrowser
url: 'http://localhost/api/'
- \Helper\Acceptance
config:
WebDriver:
host: 'user:key#hub.browserstack.com'
port: 3214
browser: chrome
capabilities:
os: Windows
os_version: 10
browserstack.local: true # for local testing
env:
AU:
modules:
config:
WebDriver:
capabilities:
browserstack.geoLocation: AU
US:
modules:
config:
WebDriver:
capabilities:
browserstack.geoLocation: US
CA:
modules:
config:
WebDriver:
capabilities:
browserstack.geoLocation: CA
firefox:
modules:
config:
WebDriver:
browser: firefox
safari:
modules:
config:
WebDriver:
browser: safari
I looked everywhere for this issue and I did not find anyone that even encountered it. The closest thing I found was that tests would be ran only with the second environment.
I'm new to Codeception and probably I am missing something.
I read the documentation on https://codeception.com/docs/07-AdvancedUsage#Environments and I don't seem to do anything wrong from there.
Codeception version is 2.4.5
PHP version is 7.2.8
Operating system: Windows Server 2012
Installation type: Composer 1.7.2
Things I tried:
Put the environments in _envs folder.
Put the environments in codeception.yml
Also I have the project on Jenkins as well and merging environments works perfectly there, but if I try to download the exact same project and run it locally it still doesn't work.

Running behat tests with selenium2 driver

I'm new to behat and php. I want to run some tests using behat, selenium grid and Selenium2 driver and I have some issues.
I am trying to start session like this:
$mink = new Mink([
'selenium2' => new \Behat\Mink\Session(
new Selenium2Driver('chrome', null, 'http://172.28.128.11:4444/wd/hub'))
]);
$this->gui = $mink->getSession('selenium2');
and then visit page:
$this->gui->visit("url");
My behat configuration looks like this:
default:
suites:
default:
contexts:
- FeatureContext
#- Behat\MinkExtension\Context\MinkContext
extensions:
Behat\MinkExtension:
base_url: url
browser_name: chrome
default_session: goutte
javascript_session: selenium2
goutte: ~
# https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
selenium2:
browser: chrome
wd_host: 'http://172.28.128.11:4444/wd/hub'
capabilities: { browser: chrome, browserVersion: 66.0.3359.117, version: 66.0.3359.117}
and I am still getting an error:
"Could not open connection: Error forwarding the new session cannot
find : Capabilities {browser: firefox, browserName: chrome,
browserVersion: 9, deviceOrientation: portrait, deviceType: tablet,
name: Behat Test, selenium-version: 2.31.0, version: 9}"
Can someone help? :)
Don't know if it would help, but my behat.yml content is:
selenium2:
capabilities: { "browserName": "chrome", "browser": "chrome", 'chrome': {'switches':['--no-sandbox']}}
browser: chrome

Behat multiple profile

Im having behat.yml which looks like
machine:
suites:
machine:
contexts:
- MachineContext:
location:
suites:
location:
contexts:
- LocationContext:
And when I run behat -c behat.yml --profile machine it run all my scenarios correct, but when I run behat -c behat.yml --profile location it run scenarios rom location.feature and adds scenariose form machine.feature and tells me that Im missing then
You can inspire from this example. Adjust it for your needs.
default:
extensions:
Behat\MinkExtension:
sessions:
default:
symfony2: ~
Behat\Symfony2Extension:
kernel:
env: test
debug: true
machine:
extensions:
Behat\MinkExtension:
base_url: 'http://machine.domain.com/app_test.php/'
suites:
default:
contexts:
- Path/To/Your/Context/Machine1Context:
- 'constructor parameter if you have any'
- 'and another one'
- 'Path/To/Your/Context/Machine2Context'
type: symfony_bundle
bundle: Whatever1YourBundle
location:
extensions:
Behat\MinkExtension:
base_url: 'http://location.domain.com/app_test.php/'
suites:
default:
contexts:
- 'Path/To/Your/Context/LocationContext'
type: symfony_bundle
bundle: Whatever2YourBundle
Or you can just use suites version like here: Installing behat3 with composer.json
I found out that I needed to add paths in my profile configuration, now my behat.yml looks like
machine:
suites:
machine:
paths:
- %paths.base%/features/machine.feature
contexts:
- MachineContext:
location:
suites:
location:
paths:
- %paths.base%/features/location.feature
contexts:
- LocationContext:
works for me by adding following command :
behat -c behat.yml --profile machine --suite=machine
behat -c behat.yml --profile machine --suite=location

Running Behat with Selenium2Driver in combination with Homestead

I have a Homestead installation running with Behat to test my Laravel project. I am running into some problems when using the #javasccript tag to run my tasks as Selenium.
I have downloaded Selenium standalone server on my Windows host machine. Its version is 2.42.2
In my behat.yml i have got the following configuration:
default:
extensions:
Behat\MinkExtension:
base_url: http://my.app:8000/
sessions:
default_session:
goutte: ~
selenium_session:
selenium2:
wd_host: http://x.x.x.x:4444/wd/hub/static/resource/hub.html
suites:
functional:
paths: [ %paths.base%/features/functional ]
contexts: [ LaravelFeatureContext ]
However when I run my scenarios I get the following error:
Given I am on the "/users/create" page #
LaravelFeatureContext::iAmOnThePage()
Could not open connection: Curl error thrown for http POST to http://x.x.x.x:4444/wd/hub/static/resource/hub.html/session with
params: {"desiredCapabilities":{"tags":["homestead","PHP
5.6.0-1+deb.sury.org~trusty+1"],"browserName":"firefox","version":"21","platform":"ANY","browserVersion":"9","browser":"firefox","ignoreZoomSetting":"false","name":"Behat
feature suite","deviceOrientation":"portrait","deviceType":"tablet"}}
Failed to connect to 127.0.0.1 port 4444: Connection refused (Behat\Mink\Exception\DriverException)
When i go to http://x.x.x.x:4444/wd/hub/static/resource/hub.html I see that the selenium hub is up and running.
Could the issue here be that I am running Behat from the homestad terminal through SSH and that does not resolve to the IP address of the hub? Because the ip address of the host is localhost
How can I solve this?
I figured out the answer myself.
In behat.yml i had to change the config of wd_hub to http://192.168.178.26:4444/wd/hub and then it works.

Categories