ruby test case failure in teamcity - ruby

I am new to Ruby and currently working on a JAVA project that has its behavioural test cases written in ruby(CUCUMBER BDD).When I tried to take a build of my project through TeamCity it shows 6 test case failures with
"Watir::Exception::NoMatchingWindowFoundException: Unable to locate window,"
or
"Spec::Expectations::ExpectationNotMetError:"
errors.No changes have been made to these test cases .Any suggestions as to where I should look ?
THis is my env.rb file ..do I need to change anything here .
require 'watir'
require 'open-uri'
require 'spec'
require 'win32ole'
require 'rake'
require 'cucumber/rake/task'
require 'net/http'
require 'uri'
require 'mysql'
require 'active_record'
require 'features/db/mysqldb'
require 'features/db/order'
require 'features/db/listener_state'
def clear_all_mock_requests
open 'http://xxxxxx:8080/httpmockserver/soapResponse!clearAllRequests.action'
end
Mysqldb.connect("xxx", "xxx", "xx", "xxx", "xxxxx")
clear_all_mock_requests()
Watir::Browser.default = ENV['browser'] == 'firefox' ? 'firefox' : 'ie'
#Watir::Browser.default = 'firefox'
BROWSER = Watir::Browser.new
WIN32OLE.class_eval do ||
def visible?;
return false if self.style.invoke('display').downcase == 'none';
return true;
end
def type
{:text_field=>'text', :radio=>'radio', :select_list => 'select-one', :checkbox => 'checkbox'}.each do |method, name|
return name if BROWSER.method(method).call(:name, self.name).exist?
end
return nil
end
def exist?
return true
end
end
NilClass.class_eval do ||
def exist?
return false
end
end
at_exit do
BROWSER.close
end

Watir::Exception::NoMatchingWindowFoundException: Unable to locate window usually means that you're trying to latch onto a browser that does not exist. Check your browser.open functions.

Related

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.

Cucumber and ruby: print every step executed

With the function below, I can receive a print of the last execution of the test, however I want to learn how to receive a print at every step executed by the automation.
How can this be done?
env.rb
# encoding: utf-8
require 'watir'
require 'rspec'
hooks.rb
# coding: utf-8
require 'json'
require 'magic_encoding'
require 'win32console'
require 'watir'
require 'rspec'
browser = Watir::Browser.new
browser.driver.manage.window.maximize
Before do
#browser
#browser = browser
end
After do |_scenario|
browser.screenshot.save 'screenshot.png'
embed 'screenshot.png', 'image/png'
end
login.rb
given("que estou na tela de login") do
#browser.goto "url"
#I want a screenshot of this step
end
There is an AfterStep hook if you want to do an action after each step - eg:
AfterStep do
browser.screenshot.save 'screenshot.png'
end

Setting Sessions Using Rspec/ Rack::Test in Sinatra Tests

I am trying to test a Sinatra application that is using oauth that has the following code being run before every route for the callback:
before do
unless session.has_key?(:oauth_token) || request.path == '/auth/callback'
access_url = oauth_client.auth_code.authorize_url(redirect_uri: ENV['CALLBACK'])
puts "Redirecting to #{access_url}"
redirect "#{access_url}"
end
end
For my tests, I simply just want to set the session[:oauth_token] to anything so that I get past this block and move onto the test. However, after hours of searches and experimentation, I haven't been able to figure it out.
I've tried Rack::Test to try and set it this way:
describe "Visit home page", js: true do
before { get '/', {}, { 'rack.session' => { oauth_token: 'blahblahblah' } } }
it "has a list of products" do
get "/"
expect(page).to have_link("Clear & Mild Foam Handwash Refill, Fragrance-Free, 1250mL Refill, 3/Carton")
expect(page).to have_link("Coffee Portion Packs, 1.5oz Packs, Hazelnut Crème, 24/Carton")
end
end
end
and my spec_helper.rb looks like this:
require File.expand_path '../../server.rb', __FILE__
require 'rspec'
require 'capybara/rspec'
require 'rack/test'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
set :environment, :test
Capybara.app = Sinatra::Application
ENV['RACK_ENV'] = 'test'
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
def setup_session(session = {})
Rack::Session::Abstract::SessionHash.stub(:new).and_return(session)
end
end
RSpec.configure do |c|
c.include RSpecMixin
end
What is the best way to go about actually setting a session before every route?
Try this:
env "rack.session", { oauth_token: 'blahblahblah' }
get '/'
Taken from here.

Ruby Page object Gem - Unable to pick a platform for the provided browser (RuntimeError)

I get this error on running my feature file.
Unable to pick a platform for the provided browser (RuntimeError)
Help required, please.
Here is the code;
class GooglePage
include PageObject
def self.visitor
visit("http://www.google.com")
end
end
env.rb
require 'selenium-webdriver'
require 'page-object'
require 'rubygems'
require 'page-object/page_factory'
World (PageObject::PageFactory)
#browser = Selenium::WebDriver.for :firefox
Step-Definitions
require_relative 'GooglePage'
Given(/^I am on the Google home page$/) do
visit(GooglePage)
# visit('http://www.google.com')
on(GooglePage).visitor
end
This won't work:
visit(GooglePage)
because you haven't called page_url in GooglePage (ln 4 below)
class GooglePage
include PageObject
page_url "http://www.google.com"
def self.visitor
visit("http://www.google.com")
end
end
Move the line #browser = Selenium::WebDriver.for :firefox to the Before method in hooks.rb
Before do
#browser = Selenium::WebDriver.for :firefox
end
After do
#browser.close
end
what if you make your rake file pass on tags and BROWSER and URL,e.g.
rake my_task BROWSER=chrome URL=http://google.com.au
and hooks will check if the BROWSER is chrome, then will use the specific Webdriver for chrome. But how to pass the URL in the PageObject?
Currently from hooks I have the ff:
when "chrome" then
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps.version = "40.0.2214.115 m"
caps.native_events = false
caps.javascript_enabled= true
# This is for increasing the default timeout to 180
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180
browser = Selenium::WebDriver.for :chrome
browser.manage.window.maximize
end
Before do
browser.cookies.clear
#browser = browser
end
After do
unless ENV["BROWSER"].eql? "ie"
browser.close
sleep 2
end
end
Then on my HomePage pageobject I have the ff:
class HomePage
include PageObject
page_url("#{ENV['URL']}")
#opens the url defined in ENV['URL']
def goto_homepage
visit(HomePage)
end
end
Using Watir, the URL got opened, so trying this out in Selenium and it won't work.

Unable to pass ARGV through Test Unit (ruby)

I'm running automation tests with watir-webdriver.
I am not able to pass command line arguments ARGV through test unit, I get an ArgumentError.
require 'rubygems'
require 'watir-webdriver'
require 'test/unit'
ARGV.each do |arg|
if arg.downcase.include? 'chrome'
$browser = 'chrome'
elsif arg.downcase.include? 'firefox'
$browser = 'firefox'
elsif arg.downcase.include? 'ff'
$browser = 'firefox'
elsif arg.downcase.include? 'ie'
$browser = 'ie'
end
end
class TEST_SITE < Test::Unit::TestCase
def setup
if $browser == 'chrome'
$b = Watir::Browser.new :chrome
elsif $browser == 'firefox'
$b = Watir::Browser.new :ff
elsif $browser == 'ie'
$b = Watir::Browser.new :ie
end
end
end
Is there another option or somehow override the test unit class?
Test/Unit seems to have logic around how it is handling the values in ARGV, though not exactly sure what values it is checking for. However, if you make your arguments more parameter like, they get ignored by Test/Unit and your tests should run.
Try running the following from command line (you should not need to change your code):
ruby filename.rb -browser=ff

Resources