wrong number of arguments (given 1, expected 0) when use expect method - ruby

here is my code
describe "my page", :type => :feature, :js => true do
before(:each) do
visit EnvConfig.get :app_url
end
it { expect(page).to match_expectation }
end
Trying to add gem and use it https://github.com/rprt/rspec-page-regression
Error
Failures:
1) my page should match expectation
Failure/Error: it { expect(page).to match_expectation }
ArgumentError:
wrong number of arguments (given 1, expected 0)
# ./spec/features/login_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 3.24 seconds (files took 0.90062 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/login_spec.rb:7 # my page should match expectation
spec_helper.rb file has next requires
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'rspec'
require 'rspec/page-regression'
require 'selenium-webdriver'
require 'site_prism'
require 'env_config'
require 'active_support/all'
and also config.javascript_driver = :selenium

Related

Getting Frozen Error-Can't modify frozen string

Getting frozen error while running below mentioned cod. Also find the helpers files. Below is the main file:
require 'spec_helper.rb'
require 'rspec_capybara_assignment_helper.rb'
describe 'Open the automationpractices site' , :type => :request do
it 'Test: Page Content', :page do
visit "http://automationpractice.com/index.php"
expect(page).to have_xpath(".//img[contains(#src,'automationpractice.com/img/logo')]")
expect(page).to have_xpath(".//input[#id='search_query_top']")
expect(page).to have_link("Contact us")
expect(page).to have_link("Sign in")
within('div#block_top_menu') do
expect(page).to have_link("Women")
expect(page).to have_link("Dresses")
expect(page).to have_link("T-shirts")
end
within('div#center_column') do
expect(page).to have_link("Popular")
expect(page).to have_link("Best Sellers")
end
expect(page).to have_content("Automation Practice Website")
end
end
Spec_Helper file:-spec_helper.rb
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require "selenium-webdriver"
require "capybara"
require 'capybara/rspec/matchers'
RSpec.configure do |config|
config.include Capybara::DSL , :type => :request # to include capybara DSL methods
config.include Capybara::RSpecMatchers,:type => :request # to include Rspec matchers available.
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium # which driver to use (selenium / chrome /internet explorer)
config.default_selector = :css #(by default css selector) # if user want to use xpath, he has to write find(:xpath, <xpath>).click
config.app_host = "http://automationpractice.com/index.php" # host app
config.default_max_wait_time = 5 # wait for 4 seconds.
config.app_host = #url
config.ignore_hidden_elements = true # will ignore hidden elements on page.
config.match =:prefer_exact # check for exact match.
end
Rspec Capybara Assignmnet Helper File:
rspec_capybara_assignment_helper.rb
def click_on(value)
find(value).click
end
def link_click(value)
click_link(value)
end
def button_click(value)
click_button(value)
end
def login_application
link_click('Sign in')
fill_in('email', :with => 'test.test#testing.com')
fill_in('passwd', :with => 'testtest')
button_click('SubmitLogin')
end
def sign_out
link_click('Sign out')
end
def search_product(value)
fill_in('search_query_top', :with => value)
button_click('Search')
sleep(2)
end
def add_product(value)
page.find(:css,'.product-image-container').hover
sleep(2)
first("a", :text => "More").click
sleep(4)
fill_in('quantity_wanted', :with => '2', visible: false)
select('M', :from => 'group_1')
find('button.exclusive').click
sleep(2)
first("span", :text => "Continue shopping", wait: 3).click
sleep(2)
end
def check_locator(value)
expect(page).to have_selector(value)
end
Error as below:
Open the automationpractices site
Test: Page Content (FAILED - 1)
Failures:
Open the automationpractices site Test: Page Content
Failure/Error: visit "http://automationpractice.com/index.php"
FrozenError:
can't modify frozen String
./spec/tests/rspec_capybara_assignment.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.7789 seconds (files took 3.61 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/tests/rspec_capybara_assignment.rb:5 # Open the automationpractices site Test: Page Content
This appears to be related to selenium-webdriver, not capybara itself.
There's an issue about this at https://github.com/SeleniumHQ/selenium/issues/7365
To fix, specify in your gemfile that you want 3.142.4 as a minimum: gem 'selenium-webdriver', '~> 3.142.4'
And then run bundle update selenium-webdriver
That should get this specific issue resolved.

Errors are shown when I am trying to run mobile automation TC using Appium + Capybara + Rspec + Ruby

I am trying to adjust environment for mobile TC running using Appium + Capybara + Rspec + Ruby. I've created two files:
1) The first file is capybara_spec.rb:
require 'rspec'
require 'capybara/rspec'
require 'appium_capybara'
require 'site_prism'
require 'rspec/expectations'
require 'capybara-screenshot/rspec'
require 'launchy'
require '../specs/capybara_test_spec'
RSpec.configure do |config|
# config.include CapybaraTest
config.include RSpec::Matchers
config.include Capybara::DSL
Capybara.default_driver = :appium
Capybara.register_driver(:appium) do |app|
desired_caps = {
caps: {
platformName: 'Android',
versionNumber: '6.0.1',
deviceName: 'Galaxy S6',
device: 'Android',
app: '../app-mockBLE_SK.apk'
# browserName: 'Chrome'
}
}
Appium::Capybara::Driver.new(app, desired_caps)
end
Capybara.save_path = "../Reports" # path where screenshots are saved
config.after(:each) do |result|
CapybaraScreenshot.save_and_open_page_path if result.exception
Capybara.current_session.driver.browser.remove_app 'com.medtronicndt.envisionpro.mock'
Capybara.current_session.driver.browser.quit
end
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//, '')}"
end
end
Capybara.default_max_wait_time = 15
2) the second file with TC capybara_test_spec.rb:
require_relative '../specs/capybara_spec'
describe 'First ' do
context 'Mobile ' do
it 'Test:' do
a = Capybara.current_session.driver.browser.app_installed?('com.medtronicndt.envisionpro.mock')
puts "App Status: " + a.to_s
expect(a).to be true
end
end
end
After running I see the following errors:
1) First Mobile Test:
Failure/Error: if e = error()
Selenium::WebDriver::Error::NoSuchDriverError:
2.1) Failure/Error: a = Capybara.current_session.driver.browser.app_installed?('com.medtronicndt.envisionpro.mock')
Selenium::WebDriver::Error::NoSuchDriverError:
# ./capybara_test_spec.rb:6:in `block (3 levels) in <top (required)>'
2.2) Failure/Error: CapybaraScreenshot.save_and_open_page_path if result.exception
NoMethodError:
undefined method `save_and_open_page_path' for CapybaraScreenshot:Class
# ./capybara_spec.rb:34:in `block (2 levels) in <top (required)>'
2.3) Failure/Error: if e = error()
Selenium::WebDriver::Error::NoSuchDriverError:
Why these error are displayed? I see that app is started, also expect in TC returns true. But report is not created and TC is failed.

Error in Ruby/RSpec/WebDriver > expected respond to 'has_content?'

Can anyone explain this error?
My spec_helper.rb requires capybara, rspec, and selenium-webdriver.
My test_spec.rb file contains the following:
require_relative 'spec_helper'
#browser = Selenium::WebDriver.for :firefox
#browser.get "http://www.google.com"
describe 'ErrorCheck' do
it 'should log in to Trialnet' do
expect(#browser).to have_content('Search')
end
end
My error:
expected to respond to `has_content?`
./spec/webdriver3_spec.rb:9:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Any idea why this expectation is failing? Is it returning a Boolean without the proper syntax to accept it?
This is happening because the #browser instance variable is not available to the it statement. Notice that your error message doesn't have a reference to the object that it is performing an expectation on (i.e. expected to respond to 'has_content?'.
Here's a contrived demonstration that shows it fail:
require 'rspec'
#x = 1
describe 'One' do
it 'should print 1' do
expect(#x).to eq 1
end
end
Failures:
1) One should print 1
Failure/Error: expect(#x).to eq 1
expected: 1
got: nil
And--by moving the instance variable into the it statement to available--the example passes:
require 'rspec'
describe 'One' do
it 'should print 1' do
#x = 1
expect(#x).to eq 1
end
end
1 example, 0 failures
And--if you use let--you can created a memoized variable that can be shared across examples:
require 'rspec'
describe 'One' do
let(:x) { 1 }
it 'should print 1' do
expect(x).to eq 1
end
it 'should print 2' do
expect(x+1).to eq 2
end
end
Based on your example code, you could use a before block for setup and then use subject, which is probably more appropriate than let (NOTE: snippet below is untested, and the difference between let and subject is covered in other SO answers, various blog posts, and rdoc):
describe 'ErrorCheck' do
before :all do
#browser = Selenium::WebDriver.for :firefox
#browser.get "http://www.google.com"
end
subject(:browser) {#browser} # or let(:browser) {#browser}
it 'should log in to Trialnet' do
expect(#browser).to have_content('Search')
end
end

uninitialized constant error with SitePrism page

I've added this to my spec_helper:
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
And my page is this:
class AboutPage < SitePrism::Page
end
My rspec is this:
require_relative 'spec_helper'
describe 'About Page' do
it "test" do
about = AboutPage.new
end
end
The error I keep getting is:
Failures:
1) About Page test
Failure/Error: about = AboutPage.new
NameError:
uninitialized constant AboutPage
# ./about_spec.rb:6:in `block (2 levels) in '
If AboutPage is not in the root path of controller, you must call it with a namespace like:
about = SomeNameSpace::AboutPage.new
It looks like I needed to add to my spec_helper.rb:
require_relative about_page.rb
All pages you are using in your spec files have to be required before the can be used.
If all of your site prism objects are in spec/page_objects, using rspec, you could add this line to rails_helper.rb
Dir[Rails.root.join('spec/page_objects/**/*.rb')].each { |f| require f }
This will automatically require about_page.rb and any other page objects, including subfolders.

How to skip SSL certificate verification with Capybara

I'm trying to test a webapp that is served over HTTPS, but I'm not able to skip certificate verification:
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "capybara"
require "capybara/dsl"
require "capybara-webkit"
Capybara.run_server = false
Capybara.register_driver :webkit do |app|
Capybara::Driver::Webkit.new(app, :ignore_ssl_errors => true)
end
Capybara.current_driver = :webkit
Capybara.app_host = "https://foo.bar.com"
module Test
class Net
include Capybara::DSL
def get_results
visit('/index.jsp')
fill_in "#UserId", :with => "sheldon"
fill_in "#Pwd", :with => "cooper"
click_button "Enter"
page.save_screenshot('screenshot.png')
end
end
end
spider = Test::Net.new
spider.get_results
I get:
net.rb:10:in `initialize': wrong number of arguments (2 for 0) (ArgumentError)
from net.rb:10:in `new'
from net.rb:10:in `block in <main>'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `call'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `driver'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:197:in `visit'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
from net.rb:20:in `get_results'
from net.rb:31:in `<main>'
How can I skip it?
I believe the API has changed and that you need to do:
Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app).tap {|d| d.browser.ignore_ssl_errors }
end

Resources