I am quite new to Selenium. Currently i am using selenium driver for writing test cases in ruby on rails. In the application, I am required to login and Logout for each test cases. So, I exported each test cases from Selenium IDE to rails 3. I am required to use same browser session for multiple test cases. So I am calling Login test before required test case to be executed in a single ruby file. Is it possible to maintain browser session for consecutive next tests in rails 3 either with Selenium Client or Selenium Webdriver?
I got a solution for this issue for Selenium Client/Webdriver in Ruby on Rails 3. To maintain a session from test script to another script, the only thing you need is session variable. Since I have written different Login script and this script is being called in different other script files, so from this another file I have to access the session variable from Login script.
To access variable from Login script, the code inside is being converted to module.
This is how it worked for me:
----Start-----
------Login.rb-----
module ModuleName
def methodName
----Write particular Login code required through selenium---#
#variable = SELENIUM::CLIENT:DRIVER.new \ #---Create browser instance and store in a variable ---#
return #variable #--Return back the variable from this method
end
end
------File1.rb-----
require Login.rb #---Specify with path---#
include ModuleName
Class File < TestCase #--Extending resp. Test Class
def setup
#local = ModuleName.methodName #--Access Module's method that will return session object.
end
def test_file
--Access this #local in rest of the code--#
end
end
-----End----
This was the tweak to access variable from file1 to file2.
I hope this may help somebody.........
Related
I'm new to Ruby on Rails and am trying to access my site's database. I generated and set up a model and controller called Machine, and noticed that in places like the Machine view I could iterate through all the machines in my database simply using #machines.each. However, this doesn't appear to be universal, as when I created a new Ruby file directly in my project's outermost directory, both #machines.each and the attempted assignment #machines = Machine.all threw errors (a NoMethodError and NameError respectively). Here's an example of code I could try to run:
#machines = Machine.all
#machines.each do |machine|
puts machine.created_at
end
Perhaps I need some kind of import statement?
If you are writing a script in plain Ruby -- then yes, you'll have to import everything manually, establish a connection to the DB, etc.
The code would roughly look like this:
require 'active_support'
require 'active_record'
your_db_config = {
# your DB config goes here
}
ActiveSupport::Dependencies.autoload_paths += File.join(__dir__, "app/models")
ActiveRecord::Base.establish_connection(your_db_config)
machines = Machine.all
Consider creating a task if you want Rails to take care of all that and don't want to be doing all that stuff manually.
When you start a rails server (or a rails console) it preloads your Rails application so that your models, constants, etc. are automatically in scope. If you want to access your application's resources from a separate script you still need to load the app. The simplest way to do that is with the rails runner command, which loads your app and then executes a script. So if your script above is in lib/show_machines you'd run:
$ bin/rails runner lib/show_machines
If you like self-executing scripts you can also use runner as a 'shebang' line:
#!/usr/bin/env <your_project_path>/rails/runner
#machines = Machine.all
#machines.each do |machine|
puts machine.created_at
end
i am doing automated testing using calabash-ios. I want to be able to run cucumber once and have it run x times for x user names and run through the gamut of test scenarios.
i want to use this:
Given I login as [#{country-name}] user using id [#{Login-name}] and pwd "PASSWORD"
and have a global variable that can store the values for both country and user name.
i had hoped to use scripts to run cucumber x times and set the value for the global variables each time. is this possible? and if so, could someone point me in the right direction?
i tried using :
##Loginname=value
but got this error:
features/step_definitions/common.rb:1: warning: class variable access from toplevel
uninitialized class variable ##Login in Object (NameError)
failing which, will it be possible to access data stored in a xml or css file using calabash?
If you want to run the same cucumber run many times with some different variables you can just use environment variables.
Given I login as "ENV['COUNTRY_NAME']" user using id "ENV['LOGIN_NAME']" and pwd "PASSWORD"
And then when you run the tests
LOGIN_NAME='login name' COUNTRY_NAME=country bundle exec cucumber
And then of course you can put all of the lines you want to run into a bat or sh script.
One thing to be careful of is to use the environment variables or another one to change the path for the outputs so you don't overwrite them.
However, a more elegant solution would be to handle it with a rake task that ran all of the other tasks. The most efficient way to write that would depend on how many different runs you need.
task :all => [:task1, :task2, :task3]
EDIT: To make your scenarios more readable, you should use a generic placeholder in the scenario and hide the environment variables in the step definition.
Given I login as a user
Might have a step definition that looked like:
Given /^I login as a user$/ do
... set up your page object here ...
login_page.login(ENV['COUNTRY_NAME'], ENV['LOGIN_NAME'])
end
I have a unit test method in my test/unit/model_test.rb :
def valid_sendRequestXML(account)
Hpricot.XML <<-XML
<ticket>#{account.api_token}</ticket>
XML
end
And I'm trying to manually run some things in my console to see what is failing where. Is it possible to somehow call this method within console? If so, how?
You can require unit test files just like any other Ruby file. The unit tests are methods of a class, so you can instantiate the testcase and run the methods you'd wish to run.
Also consider using pry. Just put binding.pry in your unit test, run it will open a console for you to inspect all the local variables (and other stuff) in.
When using the Rails console, there's a handy reload! function which reloads models and such. How do I call this from another part of my program?
Edit I've been asked for the use case a bit. It was that I had a long running JVM process running Jruby and a clojure repl. I could run RSpec tests from the REPL and could run arbitrary Ruby code from there too. But I couldn't figure out how to reload the Ruby classes so that I could edit the code and see it changed in the Ruby runtime. I no longer use this setup, principally because testing it was such a pain.
(I'm using Jruby and can access the Ruby VM programatically from my backend).
Have you tried touching restart.txt? Unfortunately, I have no experience with JRuby, but confirmed it works on my app.
FileUtils.touch('tmp/restart.txt')
You probably want to do something other than a Get request, and secure it behind some authentication.
I threw it in an Admin controller and added the route to config/routes.
# app/controllers/admin.rb
class AdminController < ApplicationController::Base
##time = Time.now # This value gets cached with the model.
def reboot
FileUtils.touch('tmp/restart.txt')
#restarted_time = ##time
end
end
# config/routes.rb
namespace :admin
get 'reboot'
end
# app/views/admin/reboot.html.erb
<%= #restarted_time.to_s %>
I'm beginning with Rails and currently I got stuck on integration testing of controllers.
I'm trying to validate that if certain values are provided by a user in a form, Active Record generates errors and these errors are passed to a view.
My problem is that I have no idea how to access controller assigned variables (for example - #user) inside integration tests. As I read in http://guides.rubyonrails.org/testing.html#what-to-include-in-your-functional-tests I can access variables such as #controller, #request, #response but I was also expecting that I can easily access variables assigned by controller.
Unfortunately things such as:
* #user
* #controller.user
don't really work :-/
Rails Version < 5.0
You can access these variables using assigns method with symbol of variable name as a perameter. In example:
assigns(:user)
Gives you value of #user from your controller.
Rails Version > 5.0
In rails 5.0 assigns and assert_template has been_depracted, so in order to use it as before you need to add:
gem 'rails-controller-testing'
to your Gemfile.
Original discussion why using assigns is a bad idea.
If you want to verify that a certain variable has been assigned in your tests then you can use following statement in your test. This will check whether your #user variable in controller has some value in it or not.If #user has some value in it then the test will pass otherwise it will fail
assert_not_nil assigns(:user)
And then you can run following command to run your test in terminal
ruby -I test test/integration/your_test_file_name.rb -n test_name