Unprocessable entity using Capybara, Rspec, Selenium-webdriver - ruby

Issue:
I am not able to login a web application using Capybara, rspec and selenium webdriver.
I am able to fill the username and password to the respective fields, but when i tried to click on login button, application is not logging in. Instead the application returns 'Unprocessable entity' (tried enabling and disabling the cookies). Using Headless chromium 64 Linux.
lib/abcd.rb
def click_login user, password
visit "https://www.******.com/users/sign_in"
fill_in 'user[email]', :with => user
fill_in 'user[password]', :with => password
click_button 'Login'
end
Test case:
require_relative 'lib/*****.rb'
describe 'Visit Websites', type: :feature, driver: :selenium_chrome_headless do
it "TC001_Test case 1" do
click_login "user#account.com", "password123"
expect(page).to have_title "Welcome to home page"
end
output:// application stays in the same page
it "TC002_Test case 2" do
find(:xpath,".account menu").click
expect(page).to have_title "Account details page"
end
driver setup - Approach 1
def setup_driver
Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium_chrome_headless
end
end
def driver_options
options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')
arguments = %w[--headless --disable-gpu --window-size=1280x1696
--disable-application-cache --disable-infobars --no-sandbox
--hide-scrollbars --enable-logging --log-level=0
--single-process --ignore-certificate-errors --homedir=/tmp]
arguments.each do |argument|
options.add_argument(argument)
end
options
end
driver setup - Approach 2
def setup_driver
Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium_chrome_headless
end
end
def driver_options
options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')
arguments = %w[--headless --disable-gpu --window-size=1280x1696
--disable-application-cache --disable-infobars --no-sandbox
--hide-scrollbars --enable-logging --log-level=0
--single-process --ignore-certificate-errors --homedir=/tmp]
arguments.each do |argument|
options.add_argument(argument)
end
options
end

You haven't explained exactly what you're trying to do here (test a local rails app, test a remote app, etc) but overall everything sounds like it is working exactly as expected in a default setup. By default every RSpec test is expected to be fully independent of every other test (you should be able to run any test singly and/or run them all in a random order), so everything gets reset between each test (it block). This means you would need to log in for every test (before blocks can help DRY that up). Also feature tests are different from normal unit tests, you should be testing whole user flows/behaviors rather than just single expectations - otherwise your tests will end up taking hours to run.
For the login issue it sounds like you haven't actually created the account in the system you're testing. If this is a local app you're testing, where you have direct connectivity to the DB you should look into fixtures or factories to help with setting up the correct state for a test. Check your logs to confirm but my guess is you'll see the login is actually failing due to invalid username/password combo.

Related

Ruby Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset Failure/Error

I am continually getting flakey errors on CI where my Selenium session is not restarting. I'm not having this issue when I run tests locally. It only ever happens on my CI server.
I'm using Capybara 2.18.0, rspec 3.7.0, slenium-webdriver 3.9.0 and site_prism 2.13. Is there
Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset
Failure/Error: raise Capybara::ExpectationNotMet.new('Timed out waiting for Selenium session reset') if (Capybara::Helpers.monotonic_time - start_time) >= 10
Capybara::ExpectationNotMet:
Timed out waiting for Selenium session reset
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/selenium/driver.rb:145:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/session.rb:127:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara.rb:314:in `block in reset_sessions!'
my spec_helper.rb looks like this:
require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.save_path = "#{Dir.pwd}/screenshots"
Capybara.default_driver = :selenium
Capybara.app_host = ENV['url']
if ENV['timeout']
Capybara.default_max_wait_time = ENV['timeout'].to_i
else
Capybara.default_max_wait_time = 15
end
RSpec.configure do |config|
config.full_backtrace = true
config.before(:each) do
config.include Capybara::DSL
end
config.after(:each) do |example|
if example.exception
name = example.example_group.to_s
name.slice!('RSpec::ExampleGroups::')
name.gsub!('::', '#')
whole_page.save_screenshot("#{name}##{example.description.tr(' ', '_')}-#{Time.now.strftime('%H_%M_%S')}.png")
end
end
end
When Capybara resets the driver, it tells the browser to go to about:blank and then waits up to 10 seconds for there to be no elements on the page matching CSS '/html/body/*'. In your case that isn't happening within 10 seconds.
One reason for this could be you having onunload handlers that are doing something that could take longer than 10 seconds on your CI hardware (or opening alert messages, etc)?? If that's the case a workaround is to have the test visit a page that doesn't have an onunload handler and check for something visible on that page at the end of the test (potentially in an after block to keep the test clean). Another thing to verify is that the versions of Chrome and chromedriver are the same between local and CI.

All SauceLabs tests run with Capybara result in "Unnamed Ruby job" and no metadata

I'm setting up a standalone RSpec/Capybara test suite integrated with SauceLabs, but the instructions in the documentation don't seem to be working for me.
Here are the relevant parts of my spec_helper.rb:
require 'capybara'
require 'capybara/rspec'
require 'sauce/capybara'
Sauce.config do |config|
config[:browsers] = [
[ "OSX 10.10", "Safari", "8" ]
]
end
Capybara.default_driver = :sauce
And here's the feature (not_found_spec.rb):
feature 'Enroll: 404', :sauce => true do
before :each do
#nonexistent_curriculum = FactoryGirl.build :curriculum
#enroll = Enroll.new
end
context 'When I visit a page that does not exist' do
scenario 'I see a Not Found message' do
#enroll.go #nonexistent_curriculum
expect(#enroll.not_found).to be_visible
end
end
end
When I then run rspec, the specs run and pass, but no metadata of any kind is recorded. All I see on SauceLabs is "Unnamed Ruby job".
What am I missing?
When you run
bundle exec rake sauce:install:spec
it creates a sauce_helper.rb which is then typically required from the end of your rails_helper.rb or spec_helper.rb depending on what *_helper.rb files you have. It looks like you copied the Sauce config part from sauce_helper into your spec_helper but you haven't shown that you have
require "sauce"
in there which is in the generated sauce_helper. Without requiring "sauce" it may be that sauce/rspec/rspec.rb is not getting required which is where all the hooks into rspec for tests with sauce: true are set up.

Firefox is not navigating to the URL and TestWise is showing timeout errror (Rspec, Selenium WebDriver)

I am learning rspec with selenium webdriver using testwise. I created a project and given a url and created a new test case and tried to run it. While running it is opening Firefox automatically.but not navigating to the given url(gmail.com)
Here's my code
load File.dirname(__FILE__) + '/test_helper.rb'
describe "Test Suite" do
include TestHelper
before(:all) do
#browser = $browser = Selenium::WebDriver.for(browser_type)
#browser.navigate.to(site_url)
end
after(:all) do
#browser.quit unless debugging?
end
it "New Test Case" do
# Test Steps go here
end
end

Can Watir-Webdriver with PhantomJS access an external URL that has basic auth?

I am using watir-webdriver with cucumber and ruby to build up automated regression tests for a website. Cucumber and watir-webdriver are outside of the rails application that is used for the actual site. To get my tests to run on Jenkins, I would like to start to use phantomjs to run the tests headlessly. In order to access the the pre-production environments of the website, I need to get past the site's basic authentication.
The issue I am running in to is: when phantomjs attempts to access an external URL that has basic auth, it is getting hung up and not getting past the basic auth username/password.
Any thoughts on how to get phantomjs to recognize a URL like the following:
https://admin:password#test.website.com"
env.rb:
BASE_URL = Configuration["base_url"]
require "watir-webdriver"
case ENV['BROWSER']
when 'chrome'
profile = Selenium::WebDriver::Chrome::Profile.new
browser = Watir::Browser.new :chrome, :profile => profile
when 'firefox'
profile = Selenium::WebDriver::Firefox::Profile.new
browser = Watir::Browser.new :firefox, :profile => profile
when 'phantomjs'
browser = Watir::Browser.new :phantomjs
end
end
config.yml
website:
base_url: "https://admin:password#test.website.com"
Running cucumber:
cucumber -p website BROWSER=phantomjs
The result:
Scenario: User Logs In
Given a logged out user
timed out after 30 seconds, waiting for {foobar...}
caps = { 'phantomjs.page.settings.userName' => 'admin', 'phantomjs.page.settings.password' => 'password' }
driver = Selenium::WebDriver.for(:phantomjs, :desired_capabilites => caps)
browser = Watir::Browser.new(driver)
browser.goto 'https://test.website.com'

Launch different browsers using Capybara/Cucumber

I wish to be able to run my tests against different browsers. I have written the following method to do this and this is in my env file.
def startbrowser()
if BROWSER == "ff"
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :firefox )
end
else
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome )
end
end
session = startbrowser()
session.visit(#base_url)
The above should launch firefox if ff is supplied but should default to chrome as this is the browser I use for most of my testing. So the command I would use in the terminal would be: cucumber --tags #tests BROWSER=ff.
However this does not work. It does not give me an error, but it always launches firefox even if I dont supply 'BROWSER = ff' part. In theory it should default to chrome. I can successfully launch chrome browser if I don't have the command in the method, but I wish to be able to switch between browsers and run different jobs from jenkins. Anybody got any idea what im doing wrong here?
Thanks!
The problem is that you're trying to access an environment variable incorrectly. You should change the following line:
if BROWSER == "ff"
...to...
if ENV['BROWSER'] == "ff"

Resources