Getting Frozen Error-Can't modify frozen string - ruby

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.

Related

how to generate allure reports using page-object and watir

I have used the configuration mentioned in the allure-ruby-commons in the below link
https://github.com/allure-framework/allure-ruby/tree/master/allure-ruby-commons
But am unable to generate allure reports using the same. Can any one please help me on that.
How to generate allure reports using watir and page-object gem
require 'watir'
require 'page-object'
class GithubPage
include PageObject
link(:sign_in, text: /Sign in/)
text_field(:user_id, id: 'login_field')
text_field(:password, id: 'password')
button(:login_btn, value: 'Sign in')
link(:notification, href: '/notifications')
list_items(:name_count, xpath: "//p[text()='Repositories']/following-sibling::ul/li")
span(:select_all, text: /Select all/)
button(:select_remaining, text: /Select all/)
button(:done_all, css: "div[class*='notifications-mark-all-actions'] button[title='Done']")
button(:done, css: "div[class*='notifications-mark-selected-actions'] button[title='Done']")
h3(:no_notifications, text: 'All caught up!')
def login(username, password)
sign_in
self.user_id = username
self.password = password
login_btn
end
def check_and_read_notifications
notification_element.when_visible.click
if no_notifications?
puts 'There are no Notifications left'
else
name_count_elements.each do |name|
puts name.text
end
select_all_element.click
if select_remaining?
select_remaining
done_all
else
done
end
sleep 1
notification
end
end
end
require_relative 'github_page.rb'
require 'allure-ruby-commons'
browser = Watir::Browser.new :chrome, headless: true
browser.window.resize_to(1920, 1080)
github = GithubPage.new(browser)
github.navigate_to 'http://github.com'
github.login('test', 'tester')
github.check_and_read_notifications
Allure.configuration
Allure.configure do |config|
config.results_directory = "./Results"
config.clean_results_directory = true
config.logging_level = Logger::INFO
end

How do I reference the Capybara driver for creating my pageobjects/specs.

I am attempting to use pageobjects along with with my Capybara specs but can't seem to properly reference the driver. Basically I want to be able to use the PageObjects to define the fields on the page (this is login_page.rb), but when I try to create the object in the spec, it is throwing errors with saying that the object is nil.
spec_helper.rb:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'page-object'
# loading page object files
page_paths = File.join(Dir.pwd, 'spec', 'pages', '**', '*.rb')
puts 'foo'
Dir.glob(page_paths).each { |file| puts file}
Dir.glob(page_paths).each { |file| require file }
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.app_host = *********** #redacted
Capybara.default_max_wait_time = 5
RSpec.configure do |config|
config.before(:all) do
#browser = Capybara::Selenium::Driver
end
config.before(:each) do
config.include Capybara::DSL
end
end
login_page.rb
class LoginPage
include Capybara::DSL
include PageObject
text_field(:username, id: 'username')
text_field(:password, id: 'password')
button(:login, id: 'loginButton')
def initialize(driver)
#driver = driver
end
end
login_spec.rb
require 'spec_helper'
describe 'On Login page' do
context 'using proper password' do
before(:each) do
visit('/')
end
it 'logs in as foo' do
login_page = LoginPage.new(#browser)
login_page.username = 'foo'
login_page.password = 'bar'
login_page.login
end
end
end
Assuming you're talking about the page-object gem - https://github.com/cheezy/page-object - it doesn't support Capybara, it supports watir-webdriver/watir and selenium-webdriver. Additionally Capybara::Selenium::Driver is a class not an object instance. As shown in the page-object readme you need to pass an object instance into your page objects constructor
#browser = Selenium::WebDriver.for :firefox
If you want a page object framework that supports Capybara you may want to look at something like site-prism instead.

capybara +rspec: Post "/api/albums" not working

So I am trying to create an item using POST method. But it doesn't seem to post the item.
When I do get /api/albums seems to respond fine and my test passes.
/album_spec.rb:
describe AlbumController do
before :each do
#user =FactoryGirl.create(:person)
#album = FactoryGirl.create(:album)
end
it "creates first story on login", :js => true do
login(#user)
post "/api/albums/", :format => :api_v1,:album =>#album
response.should be_success
end
it "gets album", :js=> true do
get "api/albums/"
response.should be_success
end
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require "capybara-screenshot"
require "factory_girl_rails"
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
#Capybara.defualt_wait_time = 10
if ENV['BROWSER_TEST']
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
else
Capybara.javascript_driver = :selenium
end
RSpec.configure do |config|
config.include Capybara::DSL
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
config.include RequestHelpers, :type => :request
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
I found the solution for myself. In the album factory, I had to add more attributes to make sure that the #user and album.user match.
#album = FactoryGirl.create(:album,person_id=>#user.id)
And then my test case looked like this
it "creates first story on login"do
login_as #user
post "/api/albums/", :format =>":api_v1",:album =>#album.attributes
response.should be_success
a = Album.last
a.person_id.should == #user.id #verifying user_ids match end
end

Issue with Shoes setup

When i am running shoes.rb file, which contains code to install gem, it throws error.
Undefined method setup for Shoes:Class
Code:
Shoes.setup do
gem 'activerecord' # install AR if not found
require 'active_record'
require 'fileutils'
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:dbfile => 'shoes_app'
)
# create the db if not found
unless File.exist?("shoes_app.sqlite3")
ActiveRecord::Schema.define do
create_table :notes do |t|
t.column :message, :string
end
end
end
end
class ShoesApp < Shoes
require 'note'
url '/', :index
def index
para 'Say something...'
flow do
#note = edit_line
button 'OK' do
Note.new(:message => #note.text).save
#note.text = ''
#result.replace get_notes
end
end
#result = para get_notes
end
def get_notes
messages = []
notes = Note.find(:all, :select => 'message')
notes.each do |foo|
messages << foo.message
end
out = messages.join("n")
end
end
Shoes.app :title => 'Notes', :width => 260, :height => 350
The problem was using Shoes4, where the setup method was unimplemented.
Shoes4 now implements Shoes.setup for backwards compatibility reasons but you don't really need it, so it doesn't do anything except for printing a warning that you should rather do gem install gem_name instead of using Shoes.setup.

Why am I getting FactoryGirl wrong number of arguments error?

Having strange behavior from FactoryGirl in non-rails app. getting wrong number of arguments error...don't know why.
gideon#thefonso ~/Sites/testing (master)$ rspec spec
/Users/gideon/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:6:in `define': wrong number of arguments (1 for 0) (ArgumentError)
from /Users/gideon/Sites/testing/spec/factories.rb:1:in `<top (required)>'
Here are the files involved...
login_user_spec.rb
require_relative '../spec_helper'
require_relative '../lib/login_user'
describe "Existing User Log in Scenario", :type => :request do
before :all do
#user = FactoryGirl(:user)
end
xit "should allow user to select login from top nav" do
visit "/"
within("#main-header")do
click_link 'Log In'
end
page.should have_content('Log in to your account')
end
it "and fill in login form" do
visit "/login"
within("#login-form")do
fill_in 'user-email', :with => #user.email
fill_in 'user-password', :with => #user.password
end
#FIXME - the design crew will make this go away
within("#login-form") do
click_link '#login-link' #this gives false failing test...geek query...why?
end
page.should have_content('Manage courses')
end
end
Factories.rb
FactoryGirl.define :user do |u|
u.email "joe#website.com"
u.password "joe009"
end
user.rb
class User
attr_accessor :email, :password
end
spec_helper.rb
require 'rubygems'
require 'bundler/setup'
require 'capybara'
require 'rspec'
require 'capybara/rspec'
require 'json'
require 'capybara/dsl'
# Capybara configuration
Capybara.default_driver = :selenium
Capybara.app_host = "http://www.website.com"
require 'factory_girl'
# give me ma stuff
FactoryGirl.find_definitions
require "rspec/expectations"
include Capybara::DSL
include RSpec::Matchers
ANSWER: :user is a reserved word..changed it to :stuff...works fine now.
Try to use new syntax from readme
FactoryGirl.define do
factory :user do
email "joe#website.com"
password "joe009"
end
end
If this is a non-rails app you will have to create a Userclass first. I'm not sure if you have to have instance vars with name, password and email but you definitely will have to have that class defined somewhere.
See the Getting Started file on Github for more on that.
Try this
FactoryGirl.define do
factory :user do |u|
u.email "joe#website.com"
u.password "joe009"
end
end

Resources