Trying to set behat tests for mobile devices in a Symfony2 Project.
I'm running selenium standalone server with chromedriver
This is my config file behat.yml
default:
suites:
default:
contexts:
- FeatureContext:
simpleArg: '%%kernel.environment%%'
session: '#session'
mink_session: default
mink_javascript_session: selenium_chrome_mobile_session
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://frontend.local
show_cmd: open -a Google\ Chrome %s
sessions:
default:
symfony2: ~
selenium2:
selenium2:
wd_host: http://127.0.0.1:4444/wd/hub
selenium_chrome_mobile_session:
selenium2:
browser: chrome
capabilities:
extra_capabilities:
chromeOptions:
mobileEmulation:
deviceName: "Google Nexus 5"
selenium_chrome_session:
selenium2:
browser: chrome
capabilities:
extra_capabilities:
chromeOptions:
args:
- "--start-maximized"
- "--test-type"
emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension:
name: html
renderer: Twig,Behat2
file_name: behat_report
print_args: true
print_outp: true
loop_break: true
formatters:
html:
output_path: %paths.base%/web
chrome_mobile:
extensions:
Behat\MinkExtension:
default_session: selenium_chrome_mobile_session
chrome:
extensions:
Behat\MinkExtension:
default_session: selenium_chrome_session
The website is adaptative (not responsive) and when I run the mobile features, the response is the one made for desktop browsers.
What I'm missing in my behat.yml?
For some reason running behat with profile doesn't work for chrome sessions
behat -p chrome_mobile -f pretty
Instead I added a tag with the session to execute. Like this:
Feature: This is my feature
#mink:selenium_chrome_session
Scenario: This is first scenario
When I am in homepage
It works now
Related
Attempts to use a sqlite file database in the app/cache/tests directory fail. This is determined by clearing the dev MySQl database and populating the test environment database via the console. Test database being populated was confirmed by an external Sqlite Manager. [Tests performed without the sqlite configuration pass.]
codeception.yml:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: false
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
config:
- Db:
dsn: 'sqlite:./app/cache/test/test.sqlite'
user: ''
password: ''
dump: tests/_data/test.sql
populate: true
cleanup: false
reconnect: true
Edit:
In an Ubuntu VM, adding the Symfony2 configuration to acceptance.yml allows for partial success - the test uses the Sqlite db but does not rewrite from the specified dump (test.sql). In Windows adding the Symfony configuration makes no difference.
acceptance.yml:
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://vol
- \Helper\Acceptance
# adding these lines enables Ubuntu to use sqlite db
- Symfony2:
app_path: ./app
environment: test
You haven't enabled Db module in acceptance.yml
modules:
enabled:
- Db
- PhpBrowser:
url: http://vol
- \Helper\Acceptance
Also don't add PhpBrowser and Symfony2 at the same time,
only one can be used.
I am trying to test a valid login with a SonataUserBundle login form and in a behat feature, i'd like to check if user is redirected on the login_check but it seems never working due to cookies problem.
My question is simple, how to perform a valid functional test with behat to check if an user is logged to a Symfony2 application?
I have this configuration in my behat.yml file:
default:
formatters:
pretty: true
autoload:
'': %paths.base%/features/bootstrap
suites:
test_suite:
type: symfony_bundle
bundle: MyBundle
contexts:
- Acme\MyBundle\Features\Context\FeatureContext:
session: '#session'
output_path: build/behat/output
screen_shot_path: build/behat/screenshot
mink_javascript_session: selenium2
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://<internalURL>/web/app_test.php
sessions:
goutte: # fast, CLI, browser, no javascript support
goutte: ~
selenium2: # fast, CLI, opens up a browser
selenium2: ~
symfony2: # bleeding fast, CLI, no browser
symfony2: ~
Scenario: Clicking on the submit button with good credentials
When I fill in "_username" with "mylogin"
And I fill in "_password" with "mypassword"
Then I press "Login"
And print last response
Then I should be on "/login_check"
When I do a And print last response in my behat feature config file, I have this error:
Your session has timed
out, or you have disabled cookies.
The result of the feature:
Then I should be on "/login_check"
Current page is "/<internalURL>/app_test.php/login",
but "/<internalURL>/app_test.php/login_check" expected.
(Behat\Mink\Exception\ExpectationException)
In order to test this behavior you can create this method :
/**
* #When I restart the browser
*/
public function restartBrowser()
{
$rememberMe = $this->getSession()->getCookie('REMEMBER_ME');
$this->getSession()->restart();
$this->visitPath('/');
$this->getSession()->setCookie('REMEMBER_ME', $rememberMe);
}
Please note that the cookie can be prefix by something like the name of your application if you have the framework.session.name set.
So REMEMBER_ME can be YOURAPP_REMEMBER_ME
So here an exemple of usage :
Scenario: I can remember my login in my browser
Given I fill in "_username" with "username"
And I fill in "_password" with "password"
And I check "Remember me?"
And I press "Login"
Then I should be on my account homepage
When I restart the browser
And I go to "/"
And I should see 1 "body.logged-in" elements
Hope it's useful.
Best regards.
My behat.yml looks like this:
default:
firefox:
context:
parameters:
Browser_Name: firefox
extensions:
Behat\MinkExtension\Extension:
base_url: https://google.com
javascript_session: selenium2
browser_name: firefox
goutte: ~
selenium2: ~
I'm getting this exception:
[Symfony\Component\Yaml\Exception\ParseException] Indentation problem in "D:\\mypgms\\behat.yml" at line 7 (near " extensions:").
How can I resolve this issue?
The indentation of your YAML file is inconsistent.
Please see the YAML spec's chapter indentation spaces.
Either use 2 or 4 spaces indentation consistently across your YAML config file to resolve the issue.
example with 2-space indentation:
default:
extensions:
Behat\MinkExtension\Extension:
base_url: "https://base-url.com"
# ...
context:
parameters:
class: Your\Custom\Context
base_url: "https://context-base-url.com"
# ...
I followed this blog as an example and read the ParallerRunner info. When I call bin/behat command, one browser window opens and runs all the tests successfully with the setting below.
symfony/behat.yml
default:
context:
class: Site\CommonBundle\Features\Context\FeatureContext
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://symfony.local/app_test.php/'
javascript_session: selenium2
browser_name: firefox
goutte: ~
selenium2: ~
paths:
features: %behat.paths.base%/src
bootstrap: %behat.paths.features%/Context
I modified the behay.yml (as shown below) to run some tests in one browser window and some in another window, however it doesn't do that. What it does is, it opens two browser windows but they both run same tests! How can I overcome this issue?
symfony/behat.yml
default:
context:
class: Site\CommonBundle\Features\Context\FeatureContext
parameters:
output_path: %behat.paths.base%/build/behat/output/
screen_shot_path: %behat.paths.base%/build/behat/screenshot/
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://symfony.local/app_test.php/'
files_path: %behat.paths.base%/build/dummy/
javascript_session: selenium2
browser_name: firefox
goutte: ~
selenium2: ~
shvetsgroup\ParallelRunner\Extension:
process_count: 2
paths:
features: %behat.paths.base%/src
bootstrap: %behat.paths.features%/Context
F1:
filters:
tags: "#backend"
F2:
filters:
tags: "#frontend"
BEHAT TESTS:
This should run in one window:
#frontend
Feature: User Login
#javascript
Scenario: I can login to the system
Given I am on "/login"
And I login as "user"
This should run in another window:
#backend
Feature: Admin Login
#javascript
Scenario: I can login to the system
Given I am on "/login"
And I login as "admin"
I setup parallel test execution with GNU Parallel and xargs.
Also implement consolidated report for all executed features.
Details in my article here:
http://parallelandvisualtestingwithbehat.blogspot.com/p/blog-page.html
find ./features -name "*.feature" |
parallel --gnu --halt-on-error=0 -j 3 --keep-order vendor/bin/behat -c src/my_directory/behat.yml
--halt-on-error possibilities are :
0 Do not halt if a job fails. Exit status will be the number of jobs
failed. This is the default.
1 Do not start new jobs if a job fails, but complete the running
jobs including cleanup. The exit status will be the exit status from
the last failing job.
2 Kill off all jobs immediately and exit without cleanup. The exit
status will be the exit status from the failing job.
-j 3 : Run 3 jobs in parallel
It work perfectly with selenium.
I installed a Symfony2 extension for Behat Mink from here
When I run test, an error occurs
[ReflectionException] Class AppKernel does not exist.
What am I doing wrong?
behat.yml
default:
extensions:
Behat\MinkExtension\Extension:
base_url: http://localhost/behat
goutte: ~
selenium2: ~
Behat\Symfony2Extension\Extension: ~
First run these commands to install the dependencies:
composer require behat/behat
composer require behat/symfony2-extension
composer require behat/mink
composer require behat/mink-browserkit-driver
composer require behat/mink-extension
composer require behat/mink-goutte-driver
composer require behat/mink-selenium2-driver
composer require emuse/behat-html-formatter
composer require coduo/php-matcher
Now say your symfony applcation you want to host as localhost.behat, then add the vhost configuration to httpd_vhost.conf:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/behat/web"
ServerName lochost.behat
DirectoryIndex app_dev.php
<Directory "C:/xampp/htdocs/behat/web">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now your behat.yml file should be at app/config/behat.yml with belo content :
default:
formatters:
html:
output_path: web/behat
extensions:
Behat\Symfony2Extension: ~
# Irs\BehatPopupExtension\Extension: ~
Behat\MinkExtension:
base_url: http://lochost.behat/app_dev.php/
javascript_session: selenium2
sessions:
symfony2:
symfony2: ~
selenium2:
selenium2:
browser: chrome
emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension:
name: html
renderer: Twig,Behat2
file_name: index
print_args: true
print_outp: true
loop_break: true
suites:
api:
type: symfony_bundle
bundle: NameSpaceYourBundle
#mink_session: symfony2
mink_session: selenium2
contexts:
- NameSpace\YourBundle\Features\Context\FeatureContext:
baseUrl: http://lochost.behat/app_dev.php/
screenCapturePath: web/behat
# Add "-p firefox" parameter to behat command to run tests with Firefox browser
firefox:
extensions:
Behat\MinkExtension\Extension:
browser_name: firefox
# Add "-p chrome" parameter to behat command to run tests with Chrome browser
chrome:
extensions:
#Behat\MinkExtension\Extension:
Behat\MinkExtension:
browser_name: chrome
# Add "-p safari" parameter to behat command to run tests with Safari browser
safari:
extensions:
Behat\MinkExtension\Extension:
browser_name: safari
All you go, now add your feature and context file at namespace : NameSpace\YourBundle\Features\Context\FeatureContext
and run the below commands from command prompt/git bash(preferrable):
bin/behat -v --suite=api #NameSpaceYourBundle/your.feature --config=app/config/behat.yml -f pretty
or if you want to save the output as html at web/behat directory then:
bin/behat -v --suite=api #NameSpaceYourBundle/your.feature --config=app/config/behat.yml
Let me know if it works
Note:
I am not sure if you are running selenium server or standalone server, hence adding the step to run selenium standalone server:
Download selenium and chromedriver and place the selenium server .exe and chromedriver.exe in same folder
check with "java -version" and it should not be < 1.6.x
you may need to download compatible selenium server and chrome driver
open command prompt and run the below commands:
cd C:\<folder contains selenium server .exe and chromedriver.exe>
java -Dwebdriver.chrome.driver="chromedriver.exe" -jar selenium-server-standalone-x.xx.0.jar