Codeception AcceptanceTester::loadSessionSnapshot is undefined - codeception

I am trying to test a project but unable to check the login page due to this error:
[RuntimeException] Call to undefined method AcceptanceTester::loadSessionSnapshot
This is my code:
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('Login');
$I->amOnPage('/');
if($I->loadSessionSnapshot('loggedin')) exit();
$I->dontSee('NotFound');
//$I->dontSee('Error');
$csrf = $I->grabCookie('_token');
$I->submitForm('.form',array('login'=>array(
'username'=>'username',
'password'=>'*******'
)
));
$I->saveSessionSnapshot('loggedin');
$I->see('username');
And my config is like this
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://myweb.com
- \Helper\Acceptance
I generated this using the commandline command
codecept.bat generate:cept acceptance loginTest

There is no such method in PhpBrowser module,
loadSessionSnapshot method is only provided by
WebDriver.
Don't use exit() in tests, it kills Codeception too.
Use skip method instead.
if($I->loadSessionSnapshot('loggedin')) {
$scenario->skip('Already logged in');
}

Related

How do I correct this Selenium initialisation command deprecation warning?

Using Rails 6 I'm trying to set up selenium in headless mode for system tests, I'm using this statement in application_system_test_case.db:
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
(according to Agile Web Dev Rails 6 tutorial)
but it gives me this deprecation warning:
Selenium [DEPRECATION] [:browser_options] :options as a parameter for driver initialization is deprecated. Use :capabilities with an Array of value capabilities/options if necessary instead.
I've done some searching in Selenium docs but I my basic code skills still leave me unclear as to how I should correct this. Can anyone advise how I can correct this?
(My amateur guesswork trials of things like:
driven_by :selenium, :capabilities['headless_chrome', 'screen_size: 1400, 1400']
all result in errors)
Updated version for the new warning with options instead of capabilities
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
end
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
end
Capybara.default_driver = :chrome
In Selenium 4, the options must be passed in array capabilities:
def selenium_options
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options
end
# optional
def selenium_capabilities_chrome
Selenium::WebDriver::Remote::Capabilities.chrome
end
def driver_init
caps = [
selenium_options,
selenium_capabilities_chrome,
]
Selenium::WebDriver.for(:chrome, capabilities: caps)
end
driver = driver_init
I stumbled upon this thread a couple of times already. What was bothering to me were not only the deprecated messages, but also the puma server logs while launching the test suite. I've ended up fixing the deprecation warning and shutting the puma logs. Here's my current setup:
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# provides devise methods such as login_session
include Devise::Test::IntegrationHelpers
# removes noisy logs when launching tests
Capybara.server = :puma, { Silent: true }
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[headless window-size=1400,1000])
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
end
Capybara.register_driver(:chrome) do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[window-size=1400,1000])
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
end
ENV['HEADLESS'] ? driven_by(:headless_chrome) : driven_by(:chrome)
end
So you can launch tests, for instance, with:
HEADLESS=1 rails test:all
Suppress the warning
One line fix:
# rails_helper.rb
Selenium::WebDriver.logger.ignore(:browser_options)
Suggested here
OR
(probably) Any version of Capybara > 3.36.0
Edit: #silvia96 has 3.38.0 and still getting warning
It's quite a confusing bug because if you look at the Capybara driver registration you can see it already knows about using capabilities. The actual bug is because the Gem version test is set as ~ instead of >=. The fix is in main and any version of Capybara after 3.36.0 will, likely, fix it.
Combining the useful solutions from others, here's what mine looks like.
Remove puma logs, make headless chrome, ignore browser options error:
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome
Capybara.server = :puma, { Silent: true }
Selenium::WebDriver.logger.ignore(:browser_options)
end

:driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead

2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
visit in hook, after scenario +1s, #1s
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
I've been using the below code snippet to init a session with old 'selenium-webdriver' v3.6.0 and got the above error:
Updated on June 23rd.
if Capybara.default_driver == :selenium
Capybara.register_driver :selenium do |app|
# In the block, we build up an `options` hash to pass to
# Capybara::Selenium::Driver.new(app, options)
# which in turn calls
# Selenium::WebDriver.for(options[:browser], options)
browser = Configuration.fetch('browser.type', :firefox)
options = {
browser: browser, # chrome
}
if Configuration.fetch('options.webdriver.use_hub', false)
{...}
elsif browser == :firefox
{...}
elsif browser == :chrome
chrome_logpath = "../chromedriver.log"
options[:service] = ::Selenium::WebDriver::Service.chrome(
args: {
verbose: true,
log_path: chrome_logpath,
}
)
chrome_options = Selenium::WebDriver::Chrome::Options.new
chrome_options.add_argument("user-agent='QA Test'")
chrome_options.add_option('w3c',false)
options[:options] = chrome_options
end
Capybara::Selenium::Driver.new(app, options)
end
end
After bumped that gem to v3.142.0 then I got that error. Tracing back to the Selenium Webdriver Changelog in https://github.com/SeleniumHQ/selenium/blob/master/rb/CHANGES then i found the following description, which might break the current code
3.141.592 (2019-04-18)
Chrome:
Added support for instantiating service class directly and moved all driver executable configuration there (command-line arguments, port, etc.)
Passing driver_opts, driver_path and port to driver initializer is now deprecated
so use Selenium::WebDriver::Service.chrome instead,
which allows to customize executable behavior in similar way.
Once initialized, this object can be passed as :service keyword
during driver initialization.
* Deprecated Chrome.driver_path= in favor of Service::Chrome.driver_path=
Googling for a while i found some results and workaround like using 'webdriver' gem but i don't like it that much.
So wonder if something that i can change my snippet above to adapt with that selenium-webdriver ver 3.142.0 and onwards? I'm using Capybara v3.18.0 at the moment.
Thanks everyone,
That's not an error, it's a deprecation warning. It's telling you that you will need to change your code before selenium-webdriver v4.0 is released. If you feel you must update your code today it would be something like
elsif browser == :chrome
options[:service] = ::Selenium::WebDriver::Service.chrome(
args: {
verbose: true,
log_path: "../chromedriver.log",
}
)
chrome_options = Selenium::WebDriver::Chrome::Options.new
# add user agent to that class options
chrome_options.add_argument("user-agent='QA Test'")
options[:options] = chrome_options
end
The other stuff you show
Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)
is something different and isn't coming from any of the code you're showing.

Geb test ignoring GebConfig.groovy file launched in IntelliJ

Running in IntelliJ IDEA.
GebConfig.groovy is in /src/test/resources.
I am using the Chrome driver.
When I type
System.setProperty("webdriver.chrome.driver", "my/path")
inside my spec file, and I right click and select run, the test works, meaning it opens Chrome and loads the page.
When I don't do that in the spec file, but just leave it in the GebConfig.groovy file, I get the error message "the page to the driver executable must be set".
There's an air-gap, so I can't copy-paste; I'll type as much as I can here:
GebConfig.groovy:
import org.openqa.selenium.chrome.ChromeDriver
...
environments {
chrome {
System.setProperty("webdriver.chrome.driver", "my/path")
driver = {new ChromeDriver()}
}
}
The spec file is really simple, like the example on GitHub
import LoginPage
import geb.spock.GebReportSpec
class LoginSpec extends GebReportSpec
{
// Works when I put this here, but I should not have to do this!
System.setProperty("webdriver.chrome.driver", "my/path")
def "user can log in" () {
when: "log in as me"
def loginPage = to LoginPage
loginPage.login("me")
then:
....
}
}
To fix your problem if you want to keep the path in the geb config, setting the path outside of the environment section like so should work:
import org.openqa.selenium.chrome.ChromeDriver
System.setProperty("webdriver.chrome.driver", "my/path")
//You can also set the driver up here as a default and running with an environment set will override it
driver = {new ChromeDriver()}
environments {
chrome {
driver = {new ChromeDriver()}
}
}
Personally I would avoid adding the driver path to the geb config and create a run configuration in intelliJ for running locally.
Right click the spec file > Click "Create 'nameOfMySpec'".
Now add your driver path to the VM parameters:
-Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path
It's also worth considering a shell script that could then also be called via Jenkins etc:
mvn test -Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path

How to pass command line arguments to the browser driver being used

I'm currently using protractor for some tests. Unfortunately, I can't figure out a way to pass command line arguments to the actual driver being used.
For example, chromedriver.exe accepts '--whitelisted-ips' as a command line argument. Is there any way, in my protractor config, that I can pass this to chromedriver.exe?
Another example is, with MicrosoftWebDriver.exe, it has a flag called '--package' which allows me to pass the package id of the app to target. How would I get protractor launch the driver with those arguments?
I thought that maybe I could launch the standalone selenium server with an argument to launch the driver with those arguments, but from my investigation I couldn't find a way to make that happen.
Just to clarify, I'm not asking to pass command line arguments into protractor to use in my tests. I want the browser drivers being that are running (chromedriver.exe, firefoxdriver.exe, MicrosoftWebDriver.exe) to be run with specific command line arguments.
Add the arguments to your config file as capabilities. This is a driver-specific property.
For Chrome/Chromedriver:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['./tmp/specs/*.spec.js'],
capabilities: {
'browserName' : 'chrome',
'goog:chromeOptions' : {
args: ['--start-maximized']
}
}
}
For Firefox/Geckodriver (only changes shown):
capabilities: {
'browserName' : 'firefox',
'moz:firefoxOptions' : {
args: ['-headless']
}
}
MDN has a (very short) list of vendor-specific capabilities.
See https://sites.google.com/a/chromium.org/chromedriver/capabilities for more.

No code coverage driver is available with a remote XDebug + Nebeans

I installed Netbeans 8.2 (on Fedora 24) and a web server with PHP7+XDebug .
The debugger works well with Netbeans but when I execute a test generated by Netbeans, I have this message :
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-json" "/tmp/nb-phpunit-log.json" "--coverage-clover" "/tmp/nb-phpunit-coverage.xml" "/home/karima/netbeans-dev-201608060002/php/phpunit/NetBeansSuite.php" "--" "--run=/home/karima/git/App/tests/selenium"
PHP Fatal error: Class 'WebDriverCapabilityType' not found in /home/karima/git/App/tests/selenium/htdocs/indexTest.php on line 22
PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
Error: No code coverage driver is available
Done.
Here the simple test :
class indexTest extends PHPUnit_Framework_TestCase {
/**
* #var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://app/', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'Test'
$this->assertContains('Test', $this->webDriver->getTitle());
}
}
Howto install the coverage driver in netbeans on linux (Fedora 24) for a remote server ? (and the framework of Selenium ?)
Or have you a good doc (step by step) ?
UPDATE 1 : The file /tmp/nb-phpunit-coverage.xml is empty... I created a bug report.
Thanks
I found.
There are 3 problems.
Install the basic tools of Netbeans
First, Netbeans didn't install the necessary tools for its interfaces. So, you have to install manually PHPUnit and the other tools (for inspect/format, etc.).Here the best method for me :
I installed composer. After, I installed the Netbeans's tools in my environment of development. :
sudo dnf install composer
# necessary tools for Netbeans
composer global require friendsofphp/php-cs-fixer
composer global require phpmd/phpmd
composer global require phpunit/phpunit
...
Sometimes Netbeans detect these tools. When it cannot find the good path, in the options of Netbeans, you have to precise the good path.
Composer pushes these tools in "~/.config/composer/vendor/".
After, the problem with PHPunit disappears.
Secondly, the problems of Selenium.
Here, my objective will execute the first test generated by Netbeans.
You need to follow these steps:
Step 1: Install the webdriver
# driver for selenium/php
composer global require facebook/webdriver
Step 2: Change the code of test in Netbeans. You need to replace the include path.
set_include_path('/home/karima/.config/composer');
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
class MyFirstTest extends PHPUnit_Framework_TestCase {
/**
* #var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = DesiredCapabilities::firefox();
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
}
}
Step 3: Download and uncompressed the last release of geckodriver
Step 4: Move geckodriver in a system's path
mv geckodriver /usr/bin/.
Step 5: Download the JAR of Selenium Standalone Server (I tested with 3.0.0-beta2)
Step 6: Start the selenium server :
java -jar /home/karima/Téléchargements/selenium-server-standalone-3.0.0-beta2.jar
Step 7: You can now run the test in Netbeans without error but...
No code coverage driver is available...
I search again...
I hope it will help others.