Case insensitive Rspec match
Here's improving on phoet's solution:
page.body.should match(%r{#{string}}i)
Unfortunately the syntax highlighting here isn't doing it much justice (it looks perfectly fine in Sublime Text)
Rails rspec shoulda matcher validating uniqueness of case insensitive item failing?
As nattfodd mentions:
It seems you have some before/after hooks defined. Please show more code of
Tenant
model class.
It was a beforehook that was the issue.
Make Poltergeist case insensitive
As @eugen mentions all searches in capybara are case sensitive by default. The problem you mention of having to rewrite tests usually occurs when moving from a driver that does/does-not support css text-transform to one that does-not/does - so the text being matched is or is not having the css text-transform (uppercase/lowercase/capitalize/...) applied. If you want to be able to swap back and forth between drivers and really need case insensitivity you can pass regexes to the different matchers
expect(page).to have_text(/case insensitive text/i)
expect(page).to have_selector(:css, '#div1', text: /case insensitive text/i)
How to compare strings ignoring the case
You're looking for casecmp
. It returns 0
if two strings are equal, case-insensitively.
str1.casecmp(str2) == 0
"Apple".casecmp("APPLE") == 0
#=> true
Alternatively, you can convert both strings to lower case (str.downcase
) and compare for equality.
case insensitive equals method based on one attribute
How about this,
def ==(another_country)
return false if code.blank? # Remove this line if you want to return true if code and antoher_country.code are nil
code.to_s.downcase == another_country.to_s.code.downcase rescue false
end
Here if any of code
, another_country
or another_country.code
is nil, it will through up an exception and rescue false
statement will return false
value.
If everything goes well, the comparison will happen and true or false
will be returned based on the input.
Target case-insensitive schemes with URI::regexp?
URI::regexp
calls the default parser's make_regexp
which in turn passes the given arguments to Regexp::union
and according to its docs: (emphasis mine)
The patterns can be Regexp objects, in which case their options will be preserved, or Strings.
Applied to your problem:
pattern = URI::regexp([/http/i, /https/i, /ftp/i, /mailto/i])
htmldoc = <<-HTML
<html>
<body>
<a href="https://example.com">here</a>
<a href="HTTPS://example.com">here</a>
</body>
</html>
HTML
puts htmldoc.gsub(pattern, '')
Output:
<html>
<body>
<a href="">here</a>
<a href="">here</a>
</body>
</html>
Is it possible to find elements in SitePrism fields with case insensitive values?
Short answer : No
Longer answer: The Capybara locators (which is what the string associated with the selector type of :field is) are strings and do not accept regexps. This is because they are implemented via XPath and most (if not all) browsers only support XPath 1.0 so there is no regex support there. Most selector types do support the :text
option but that is applied to the result element, not to any other elements that may be used to find the result element (in this case it would apply to the field element not the label). So if you were looking for the actual label elements you could do
element :correct_date_label, :label, nil, text: /correct_date/i
One potential way to get what you want is to just use an xpath selector, either using the current Capybara selectors to help or writing your own custom XPath that would match both case versions, something along the lines of
element :correct_date, :xpath, (Capybara::Selector.all[:field].call('Correct date') + Capybara::Selector.all[:field].call('correct date')).to_s # might need to be | instead of +
or
element :correct_date, :xpath, ".//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][./@id = //label[(contains(normalize-space(string(.)), 'Correct date') or contains(normalize-space(string(.)), 'correct date'))]/@for] | .//label[(contains(normalize-space(string(.)), 'Correct date') or contains(normalize-space(string(.)), 'correct date'))]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]"
but that's starting to get a bit complicated
Related Topics
Re-Opened Nested Module Anomaly in Ruby
How to Find .Index of a Multidimensional Array
Generating Sequential Numbers in Multi-User Saas Application
Emacs Ruby Method Parameter Indentation
Sort a List with Multiple Conditions Ruby on Rails
Deadlock Detected with Capybara-Webkit
Ruby No Implicit Conversion of Fixnum into String (Typeerror)
How Does One Downgrade Jekyll to Work with Github Pages
Gemfile.Lock Always Has Changes Not Staged for Commit
How to Implement Composite Primary Keys in Rails
How to Create Pretty JSON in Chef (Ruby)
Rvm with Jruby 1.7.0 "Unknown Ruby Interpreter"
What's the Easiest Way to Send a Message Through Outlook with Ruby
Can't Install Therubyracer in Jruby
Ruby Attr_Reader Allows One to Modify String Variable If Using <<