OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/features/step_definitions/web_steps.rb

384 lines
11 KiB

# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'uri'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
module WithinHelpers
def with_scope(locator)
locator ? within(locator) { yield } : yield
end
end
World(WithinHelpers)
Given /^(?:|I )am on (.+)$/ do |page_name|
visit path_to(page_name)
disable_warn_unsaved_popup
end
When /^(?:|I )go to (.+)$/ do |page_name|
step %Q{I am on #{page_name}}
end
When /^I click(?: on)? "(.+?)" within "([^\"]*)"$/ do |target, selector|
element = find_lowest_containing_element(target, selector).first
if element.nil?
with_scope(selector) do
element = find_button(target)
end
end
element.click
end
When /^(?:|I )press "([^\"]*)"(?: within "([^\"]*)")?$/ do |button, selector|
with_scope(selector) do
begin
click_button(button)
rescue Capybara::ElementNotFound
page.find(:css, button).click
end
end
end
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"(?: within "([^\"]*)")?(?: if plugin "(.+)" is loaded)?$/ do |field, value, selector, plugin_name|
if plugin_name.nil? || Redmine::Plugin.installed?(plugin_name)
with_scope(selector) do
fill_in(field, :with => value)
end
end
end
When /^(?:|I )fill in "([^\"]*)" for "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
with_scope(selector) do
fill_in(field, :with => value)
end
end
# Use this to fill in an entire form with data from a table. Example:
#
# When I fill in the following:
# | Account Number | 5002 |
# | Expiry date | 2009-11-01 |
# | Note | Nice guy |
# | Wants Email? | |
#
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#
When /^(?:|I )fill in the following(?: within "([^\"]*)")?:$/ do |selector, fields|
with_scope(selector) do
fields.rows_hash.each do |name, value|
step %Q{I fill in "#{name}" with "#{value}"}
end
end
end
When /^(?:|I )select "([^\"]*)" from "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
with_scope(selector) do
select(value, :from => field)
end
end
When /^(?:|I )unselect "([^\"]*)" from "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
with_scope(selector) do
unselect(value, :from => field)
end
end
When /^(?:|I )check "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
with_scope(selector) do
check(field)
end
end
When /^(?:|I )uncheck "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
with_scope(selector) do
uncheck(field)
end
end
When /^(?:|I )choose "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
with_scope(selector) do
choose(field)
end
end
When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
with_scope(selector) do
attach_file(field, path)
end
end
When /^I wait(?: (\d+) seconds)? for(?: the)? [Aa][Jj][Aa][Xx](?: requests?(?: to finish)?)?$/ do |timeout|
ajax_done = lambda do
page.evaluate_script(%Q{
(function (){
var done = true;
if (window.jQuery) {
if (window.jQuery.active != 0) {
done = false;
}
}
if (window.Prototype && window.Ajax) {
if (window.Ajax.activeRequestCount != 0) {
done = false;
}
}
return done;
}())
}.gsub("\n", ''))
end
timeout = timeout.present? ?
timeout.to_f :
5.0
wait_until(timeout) do
ajax_done.call
end
end
Then /^(?:|I )should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
if defined?(Spec::Rails::Matchers)
begin
wait_until(5) do
elements = find_lowest_containing_element text, selector
elements.length > 0 && elements[-1].visible?
end
rescue Capybara::TimeoutError
fail_msg = "#{text} did not become visible within #{selector} within 5 sec."
unless page.has_content?(text)
fail_msg << " It might not even on the page at all!"
end
fail fail_msg
end
else
with_scope(selector) do
assert page.has_content?(text)
end
end
end
Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
regexp = Regexp.new(regexp)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
page.should have_xpath('//*', :text => regexp)
else
assert page.has_xpath?('//*', :text => regexp)
end
end
end
Then /^(?:|I )should not see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
if defined?(Spec::Rails::Matchers)
begin
wait_until(5) do
elements = find_lowest_containing_element text, selector
elements.length == 0 || !elements[-1].visible?
end
rescue Capybara::TimeoutError
fail "#{text} did not become in-visible within #{selector} within 5 sec"
rescue Selenium::WebDriver::Error::ObsoleteElementError
with_selector = selector.present? ? " within \"#{selector}\"" : ""
step %Q{I should not see "#{text}#{with_selector}"}
end
else
with_scope(selector) do
assert page.has_no_content?(text)
end
end
end
Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
regexp = Regexp.new(regexp)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
page.should have_no_xpath('//*', :text => regexp)
else
assert page.has_no_xpath?('//*', :text => regexp)
end
end
end
Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should contain "([^\"]*)"$/ do |field, selector, value|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
find_field(field).value.should =~ /#{value}/
else
assert_match(/#{value}/, field_labeled(field).value)
end
end
end
Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should not contain "([^\"]*)"$/ do |field, selector, value|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
find_field(field).value.should_not =~ /#{value}/
else
assert_no_match(/#{value}/, find_field(field).value)
end
end
end
Then /^there should be a( disabled)? "(.+)" field( visible| invisible)?(?: within "([^\"]*)")?(?: if plugin "(.+)" is loaded)?$/ do |disabled, fieldname, visible, selector, plugin_name|
if plugin_name.nil? || Redmine::Plugin.installed?(plugin_name)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
find_field(fieldname).should_not be_nil
if visible && visible == " visible"
find_field(fieldname).should be_visible
elsif visible && visible == " invisible"
find_field(fieldname).should_not be_visible
end
if disabled
find_field(fieldname)[:disabled].should == "disabled"
else
find_field(fieldname)[:disabled].should == nil
end
else
assert_not_nil find_field(fieldname)
end
end
end
end
Then /^there should not be a "(.+)" field(?: within "([^\"]*)")?$/ do |fieldname, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
lambda {find_field(fieldname)}.should raise_error(Capybara::ElementNotFound)
else
assert_nil find_field(fieldname)
end
end
end
Then /^there should be a "(.+)" button$/ do |button_label|
if defined?(Spec::Rails::Matchers)
page.should have_xpath("//input[@value='#{button_label}']")
else
raise NotImplementedError, "Only Matcher implemented"
end
end
Then /^the "([^\"]*)" select(?: within "([^\"]*)")? should have the following options:$/ do |field, selector, option_table|
options_expected = option_table.raw.collect(&:to_s)
with_scope(selector) do
field = find_field(field)
options_actual = field.all('option').collect(&:text)
if defined?(Spec::Rails::Matchers)
options_actual.should =~ options_expected
else
raise NotImplementedError, "Only Matcher implemented"
end
end
end
Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should be checked$/ do |label, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
find_field(label)['checked'].should be_true
else
assert_equal 'checked', field_labeled(label)['checked']
end
end
end
Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should not be checked$/ do |label, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
find_field(label)['checked'].should be_false
else
assert_not_equal 'checked', field_labeled(label)['checked']
end
end
end
Then /^(?:|I )should be on (.+)$/ do |page_name|
wait_for_page_load
if defined?(Spec::Rails::Matchers)
URI.parse(current_url).path.should == path_to(page_name)
else
assert_equal path_to(page_name), URI.parse(current_url).path
end
end
Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
actual_params = CGI.parse(URI.parse(current_url).query)
expected_params = Hash[expected_pairs.rows_hash.map{|k,v| [k,[v]]}]
if defined?(Spec::Rails::Matchers)
actual_params.should == expected_params
else
assert_equal expected_params, actual_params
end
end
Then /^show me the page$/ do
save_and_open_page
end
# This needs an active js driver to work properly
Given /^I (accept|dismiss) the alert dialog$/ do |method|
if Capybara.current_driver.to_s.include?("selenium")
page.driver.browser.switch_to.alert.send(method.to_s)
end
end
def find_lowest_containing_element text, selector
elements = []
node_criteria = "[contains(., \"#{text}\") and not(self::script) and not(child::*[contains(., \"#{text}\")])]"
if selector
search_string = Nokogiri::CSS.xpath_for(selector).first + "//*#{node_criteria}"
search_string += " | " + Nokogiri::CSS.xpath_for(selector).first + "#{node_criteria}"
else
search_string = "//*#{node_criteria}"
end
elements = all(:xpath, search_string)
rescue Capybara::TimeoutError, Nokogiri::CSS::SyntaxError
elements
end
def disable_warn_unsaved_popup
# disable WarnLeavingUnsaved function call when testing with selenium as this
# will freeze the server
if defined?(ChiliProject::VERSION::MAJOR) &&
ChiliProject::VERSION::MAJOR > 1 &&
Capybara.current_driver.to_s.include?("selenium")
page.execute_script("window.onbeforeunload = null")
end
end
def wait_for_page_load(seconds = 5)
begin
wait_until(seconds) do
page.has_css?('body')
end
rescue Capybara::TimeoutError
fail "Page did not load within #{seconds} seconds"
end
end