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.
Related
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
I have the following code:
require 'rubygems'
require 'rspec/core'
require 'touch_action'
require 'appium_lib'
require 'rspec/expectations'
require "selenium"
require "selenium/client"
require "selenium-client"
RSpec.configure do |config|
config.include RSpec::Matchers
config.include Selenium
config.before(:example) do |example|
desired_caps = {
caps: {
platformName: 'Android',
versionNumber: '6.0.1',
deviceName: 'Galaxy S6',
device: 'Android',
takesScreenshot: true,
screenshotWaitTimeout: 20,
app: '../app-mockBLE_SK.apk'
}
}
#driver = Appium::Driver.new(desired_caps).start_driver
#driver.manage.timeouts.implicit_wait = 120
end
Appium::Logger.level = Logger::INFO
config.after do |result|
a = Selenium::RSpec::Reporting::SystemCapture.capture_system_state(#driver, self)
puts a.to_s
driver.save_screenshot '../Reports/1.png'
#driver.remove_app 'com.medtronicndt.envisionpro.mock'
#driver.quit
end
end
But when I run my TC I see "uninitialized constant Selenium::RSpec" error. Why it doesn't allows me to call "capture_system_state" method? The reason of I am trying call this method is to determine when TC fails and do screenshot.
I'm trying to get this to loop while a specific element exists on the page. The code runs and grabs the urls I want, however, when the next button is no longer on the page it wont break out of the loop and throws the following error.
/Users/someone/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-expectations-3.2.0/lib/rspec/matchers.rb:926:in `method_missing': undefined method `each' for nil:NilClass (NoMethodError)
from /something/something/something.rb:30:in `block in <top (required)>'
from /something/something/something.rb:28:in `open'
from /something/something/something.rb:29:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'</code>
Brand new to Ruby, so please be gentle ;)
require 'capybara/poltergeist'
require 'capybara/dsl'
require 'csv'
require 'rspec'
include RSpec::Matchers
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app,timeout: 60, :phantomjs_options => ['--debug=no', '--load-images=yes', '--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'], :debug => false)
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 20
Capybara.ignore_hidden_elements = true
Capybara.current_session.driver.resize(1200, 1000)
visit('site.com')
while page.find(:xpath, 'html/body/div[4]/div[6]/div[1]/div[2]/div[2]/div[1]/div[2]/button[1]') do
page.find(:xpath, 'html/body/div[4]/div[6]/div[1]/div[2]/div[2]/div[1]/div[2]/button[1]').click
urls = page.all('.author-name>a').map { |a| a['href'] }.uniq
puts urls
end
puts urls
f = File.open("profiles.txt", "a") do |f|
urls.each { |element| f.puts(element) }
end
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
I create standalone rspec test script to testing existing api framework. It works pretty well, but I found problem where in the Rakefile I need to assign some value from YAML file (uri link, email) either CONSTANT or $global_var the code in the Rakefile looks like this:
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'rspec/core/rake_task'
task :default => :spec
desc 'Running rspec test'
task :spec, :option do |t, opt|
choice = opt[:choice]
if choice == "production"
puts 'Test running on production'
VAR = YAML::load(File.read(File.expand_path("../config/prod_variable.yml", __FILE__)))
elsif choice == "development"
puts 'Test running on development'
VAR = YAML::load(File.read(File.expand_path("../config/dev_variable.yml", __FILE__)))
end
puts VAR['URI'] #=> print out the value correctly
RSpec::Core::RakeTask.new do |task|
test = Rake.application.original_dir
task.fail_on_error = false
task.rspec_opts = '--format documentation --color'
end
end
When I run the rake command on the terminal, the rspec failed find the VAR constant value. Here is the error message from rspec
Failures:
1) ApiTest Testing API platform for GET request
Failure/Error: #var = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_get_spec.rb:8:in `block (2 levels) in <top (required)>'
2) ApiTest Testing API platform for POST request
Failure/Error: #zat = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_post_spec.rb:7:in `block (2 levels) in <top (required)>'
Is there any idea how to get this works? I need to get value from VAR constant or global variable, but seems ruby failed to assign the value.
If opt[:choice] is neither "production" nor "development", VAR is undefined in your code.