how to generate allure reports using page-object and watir - ruby

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

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.

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.

Selenium Does Click on Firefox but PhantomJS doesn't (Ruby)

i'm really new to Selenium WebDriver. I write a simple code for out of curiousity. I made an spam bot to open a page, shuffle in online users list and click everyone of them in order. Send some message, close the new window and repeat.
I made this work on Selenium Firefox driver. It seems work good. But i want to do it silent, not opening firefox everytime. So i found out i can do that by PhantomJS.
Here is my working code for firefox:
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :firefox
#reklam = 'Some testing message.'
end
def run
setup
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
x.click
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
But when i switch Webdriver from Firefox to PhantomJS, x.click method doesn't work. I made some tests and program doesnt go further from click method. After a while program ends with (Net::ReadTimeout) error.
Here is my last try to work it proper;
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :phantomjs
#reklam = 'http://peyloride.com siteme beklerim.'
end
def teardown
#driver.quit
end
def run
setup
#driver.manage.window.resize_to(1920, 1080)
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
puts "Shuffleandı"
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
puts "click'e geldik"
#driver.save_screenshot "phantomjs.png"
x.click
#driver.save_screenshot "phantomjs2.png"
puts "click yaptı sonunda aq"
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
As you can see i tried to take a screenshot to understand the problem but screenshot seems really fine. Here it is:
EDIT: Versions:
ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
gem list
...
selenium-webdriver (2.48.1)
...
phantomjs -v
1.9.0

Javascript trigger when using poltergiest/capybara outside of rails

I'm using Capybara, rspec and poltergeist, outside of rails, to run some headless integrations tests. The scenario is that, there are 2 select fields. If I select a value in the first select field, the 2nd select field is populated based on the value of the first select field. If I run my spec using poltergeist in Mac OSX, the spec works. But in ubuntu, it fails, it seems that the 2nd select field is not populated. I also have js: true on my specs.
Here's my spec_helper.rb:
require 'capybara/poltergeist'
require 'capybara'
require 'capybara/rspec'
require 'pry'
require 'support/session_helper'
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::Poltergeist
config.include SessionHelper
Capybara.run_server = false
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = "http://vps-staging.dropmysite.com"
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
EDIT Adding failing spec
require 'spec_helper'
feature 'vps-staging', js: true do
background do
visit '/'
end
let(:timestamp) { Time.now.strftime('%Y-%m-%d_%H-%M-%S') }
feature 'create private server' do
background do
sign_in 'blahblah#blahblah.com'
end
it 'successfully creates server' do
find(:xpath, "//a[#href='/en/private_servers/new']").click
directory_field = all(".tagit-new")[0].find("input")
select 'Ubuntu', from: 'distribution'
select '15.04', from: 'version'
fill_in 'private_server_profile_name', with: "Auto Test #{timestamp}"
directory_field.set "/home"
click_button 'Save'
visit '/en/dashboard'
expect(page.text).to have_content "Auto Test #{timestamp}"
end
end
end
EDIT another thing I found out is that, this bug only happens on phantomjs version 1.9.8 and below. 2.0 works fine.
As Tom Walpole pointed out, updating poltergeist from 1.6.0 to 1.7.0 fixed most of my problem but I still need to tweak a bit to fully solve my problem. What I did is to create a js file to hold the polyfill and edit my spec_helper.rb and tell poltergeist to use the options, extensions and include the polyfill.js thus changing this code from
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
to
options = { js_errors: false, extensions: ["spec/support/polyfill.js"] }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end

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

Resources