I ran Behat test on PhantomJS without issue. I was starting it with this:
bin/phantomjs --webdriver=8643
It works, but I want to run a Chrome headless instead of PhantomJS. To do that I tried this:
google-chrome --headless --remote-debugging-port=8643
But Behat doesn't seem to start anything on this Chrome. I found a lot of docs for Chrome with Selenium but I wanted to know if it's possible to run it like I was running PhantomJS with the Selenium driver, but without Selenium server?
default:
suites:
default:
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
extensions:
Behat\MinkExtension:
base_url: 'http://myurl.com/'
sessions:
default:
selenium2:
wd_host: 'http://localhost:8643'
To run your tests on Google Chrome, you will need chromedriver
Then you can use the port chromedriver is listening to (9515 by default) instead of PhantomJs 8643. You don't need Selenium anymore then.
Finally, you pass the --headless flag to chrome so you don't need xfvb.
A config example:
# behat.yml
default:
extensions:
# ...
Behat\MinkExtension:
base_url: 'http://myurl.com/'
sessions:
default:
selenium2:
browser: chrome
# Note: I'm not totally sure you still need the /wd/hub path
wd_host: http://localhost:9515/wd/hub
capabilities:
chrome:
switches:
- "--headless"
More documentation: https://developers.google.com/web/updates/2017/04/headless-chrome
This worked for me on Ubuntu 16.04 LTS
Installed latest version of chromedriver via https://medium.com/caendra-tech/acceptance-tests-with-behat-and-chrome-headless-2824aaa31fd7
Start chromedriver in the background on default port chromedriver &
Update behat.yml to
default:
extensions:
Behat\MinkExtension:
sessions:
default_session:
selenium2:
browser: chrome
wd_host: http://127.0.0.1:9515
capabilities:
chrome:
switches:
- "--headless"
- "--disable-gpu"
Related
I am developing a project which requires --disable-web-security for testing. I use Karma with the following settings:
browsers: [
"ch"
],
customLaunchers: {
"ch": {
"base": "Chrome",
"flags": ["--disable-web-security"]
}
},
and
if (process.env.TRAVIS)
options.customLaunchers.ch.flags.push("--no-sandbox");
This works properly on localhost with Chrome v69.0.3497.100 win7 x64.
I am trying to run the same code on Travis (by pushing the changes to GitHub) with the following yml:
language: node_js
node_js:
- "9"
before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sleep 5
I guess we are talking about 2 different browsers with the same engine here, since Chromium != Chrome, but I am not sure. Anyways, I got an error message by trying to build on Travis:
Blocked a frame with origin "http://localhost:9876" from accessing a cross-origin frame.
That clearly indicates that web security is enabled. Is there any way to disable web security on Travis?
Solution:
Using Trusty with real Chrome solved it:
language: node_js
node_js:
- "9"
dist: trusty
addons:
chrome: stable
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sleep 5
According to this documentation you can run Chrome headless by writing the following in your .travis.yml config file
dist: trusty
addons:
chrome: stable
before_install:
- # start your web application and listen on `localhost`
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
As for your Karma configuration file, check this page. It indicates that you have to add one more flag.
For security reasons, Google Chrome is unable to provide sandboxing when it is running in the container-based environment.
To use Chrome in the container-based environment, pass the --no-sandbox flag to the chrome executable.
module.exports = function(config) {
config.set({
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessNoSandbox'],
// you can define custom flags
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
}
})
}
Be aware that I have Never done this before. I am just pointing you to the correct documentation.
Question
Does headless Chrome work with self-signed certificates via the Selenium Webdriver on macOS?
Info
I am attempting to get Rails system tests driven by headless Chrome over SSL.
I have a locally self-signed certificate that I pass the the ruby Puma application server to terminate SSL requests. In order to allow drivers to ignore SSL warnings on the locally signed certificate, I am using the acceptInsecureCerts flag to configure the driver capabilities.
I'm led to believe by this ticket in Chromium that this flag should be recognized as of Chrome 64+.
I can get tests to succeed with Chrome, Firefox, and headless Firefox. Tests do not pass under headless Chrome. I am using (at the time of this writing) what I believe to be the latest versions of Chrome and its variants.
Though folks in the Chromium ticket appear to be successfully running headless Chrome over locally-signed SSL with the Selenium webdriver, I have not found this to work with the setup described here. If my configuration is correct, then I am unsure if there is a limitation in headless Chrome on macOS, in Selenium webdriver ruby gem, or something else I haven't considered. If anyone has something similar working with Rails on macOS, I'd be interested to learn about your setup.
Source
Here is some code to show how I am configuring and running my RSpec/Capybara tests.
Test setup
# rails_helper.rb
# ... standard rspec rails helper setup omitted ...
Capybara.register_driver(:headless_chrome) do |app|
options = Selenium::WebDriver::Chrome::Options.new(
args: %w[--headless --disable-gpu --no-sandbox --disable-web-security]
)
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
acceptInsecureCerts: true,
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options,
desired_capabilities: capabilities
)
end
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :headless_firefox
end
end
module SystemTestHelpers
def key_file_path
Rails.root.join("config", "ssl", "ssl-lvh.me.key")
end
def cert_file_path
Rails.root.join("config", "ssl", "ssl-lvh.me.crt")
end
def using_app_host(host)
original_host = Capybara.app_host
Capybara.app_host = host
Capybara.server = :puma, {
Host: "ssl://#{Capybara.server_host}?key=#{key_file_path}&cert=#{cert_file_path}"
}
yield
ensure
Capybara.app_host = original_host
end
end
RSpec.configure do |config|
config.include SystemTestHelpers, type: :system
end
Sample test
# spec/system/welcome_spec.rb
require 'rails_helper'
RSpec.feature "Welcome", :js, type: :system do
scenario "Visit homepage" do
using_app_host('https://subdomain.lvh.me') do
visit "/"
expect(page).to have_content('Welcome')
expect(page).to have_content('Your domain: subdomain.lvh.me')
expect(page).to have_content('Your protocol: https://')
end
end
end
Page content:
<div>
<h2>Welcome!</h2>
<p>Your protocol: <%= request.protocol %></p>
<p>Your domain: <%= request.host %></p>
</div>
If I swap out the driver for headless Firefox, configured as below, the tests will pass.
Capybara.register_driver(:headless_firefox) do |app|
options = Selenium::WebDriver::Firefox::Options.new(args: %w[--headless])
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(
acceptInsecureCerts: true,
)
Capybara::Selenium::Driver.new(
app,
browser: :firefox,
options: options,
desired_capabilities: capabilities
)
end
The complete source code for an app that reproduces the issue, and includes the code above is located here: https://bitbucket.org/rossta/system-test-demo.
Debug output
Here's a link to some debug output from running the test in either headless Chrome or headless Firefox: https://gist.github.com/rossta/b160204baa87a520e7888c19c8b1ed98.
Note in the output that the session response does not include the 'acceptInsecureCerts' capability for Chrome (test-headless-chrome.log, line 15) while in Firefox we do see the session include the flag (test-headless-firefox.log, line 22).
System
MacOS 10.13.6
Rails 5.2.1
Ruby 2.4.1
capybara (gem) 3.5.1
selenium-webdriver (gem) 3.14.0
chromdriver-helper (gem) 2.34
Chrome 68.0.3440.106. Have also tried
Google Chrome 70.0.3524.0 canary
Chromium 70.0.3525.0
From your log it shows that it's starting chromedriver v2.34. acceptInsecureCerts support wasn't added until 2.35 and you should really be running the latest (2.41 currently). Update your version of chromedriver and things should work.
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps['acceptInsecureCerts'] = true
Please help. I use windows to run my tests. I run selenium with chrome driver:
java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.4.0.jar
My behat_dev.yml :
default:
suites:
default:
mink_session: selenium2
javascript_session: selenium2
extensions:
Behat\MinkExtension:
base_url: http://en.wikipedia.org
default_session : selenium2
javascript_session: selenium2
browser_name : chrome
goutte: ~
selenium2 :
wd_host : http://127.0.0.1:4444/wd/hub
browser: chrome
capabilities: { "browserName": "chrome", "browser": "chrome", "version": "", 'chrome': {'switches':['--no-sandbox']} }
sessions:
default:
selenium2: ~
My scenario :
Feature: Home page
I am on a home page
#javascript
Scenario: Searching for a page with autocompletion
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Behavior Driv"
And I wait for the suggestion box to appear
Then I should see "Behavior-driven development"
The selenium is starting whitout problems, but when I try to run the tests nothing happen. The browser is not open. Please help me !!!!!! Thx in advance and sorry for my english.
You don't have to set wd_host for local run unless you are using a different port.
It should work with some simple config like:
default:
suites:
default:
contexts:
- FeatureContext
extensions:
Behat\MinkExtension:
base_url: 'http://en.wikipedia.org'
selenium2:
browser: chrome
Make sure you keep the indentation in yml, suites is under first default, not at the same level.
For extra verbosity use a -vvv at the end.
I am trying to run my scripts using Google chrome ..
In behat.yml
default:
extensions:
Behat\MinkExtension\Extension:
base_url: http://downingtown.calendar.comcast.net/prefs
javascript_session: selenium
browser_name: googlechrome
goutte: ~
selenium: ~
I am running the selenium like
java -jar selenium.jar -Dwebdriver.chrome.driver=D:\mypgms\NewMng\chrome\chromeDriver.exe
But i am getting SSL certificate Error . Then I could not able to proceed further .
1.Am i missing anything ?
2.Can i disable SSL certificate ?
In behat.yml
default:
extensions:
Behat\MinkExtension\Extension:
base_url: http://downingtown.calendar.comcast.net/prefs
javascript_session: selenium2
selenium2:
browser: chrome
goutte: ~
For more information, look at Extension.php
I get the following errors in random when running test on chrome using codeception in selenium server:
WebDriver\Exception\UnknownError: The current platform is not supported: LINUX
WebDriver\Exception\UnknownError: Unable to find executable for product Opera Desktop
WebDriver\Exception\UnknownError: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http:\//phantomjs.org/download.html
When given firefox, it works fine.
command for running selenium:
java -jar selenium-server-standalone-2.31.0.jar -browser googlechrome
Following is the acceptance.yml content
class_name: WebGuy
modules:
enabled:
- Selenium2
- WebHelper
config:
Selenium2:
url: 'http://www.google.com'
browser: googlechrome
delay: 350
To run codeception with Chrome, use the following acceptance.suite.yml:
class_name: WebGuy
modules:
enabled:
- Selenium2
- WebHelper
config:
Selenium2:
url: 'http://www.google.com'
browser: chrome
delay: 350
Note that i changed your browser in the config from googlechrome to chrome.
Also, start Selenium2 by using this command:
java -Dwebdriver.chrome.driver=./chromedriver -jar selenium-server-standalone-2.31.0.jar
Note that you don't need to specify the browser when starting selenium.
You can download chromedriver here. You should also update your selenium standalone server; you can get the latest version from here.