How do I avoid duplicate key warnings in Ruby 2.2 with Watir? - watir

Using watir-webdriver with Selenium I have element selectors with duplicate key warnings like
browser.div(class: 'one-possible-class', class: 'another-possible-class')
This works perfectly to resolve to elements that match either one or both of the given classes. The problem is that newer versions of Ruby (2.2 and up) throw warnings like this when different values are passed in using the same key.
warning: duplicated key at line 16 ignored: :class
So I tried using a regex
browser.div(class: /one-possible-class|another-possible-class/)
However, this did not work the same.

Related

Regex deprecation warning confusion [duplicate]

The following fragment of code comes from my github repository found here.
It opens a binary file, and extracts the text within <header> tags. These are the crucial lines:
gbxfile = open(filename,'rb')
gbx_data = gbxfile.read()
gbx_header = b'(<header)((?s).*)(</header>)'
header_intermediate = re.findall(gbx_header, gbx_data)
The script works BUT it receives the following Deprecation Warning:
DeprecationWarning: Flags not at the start of the expression b'(<header)((?s).*)(</' (truncated)
header_intermediate = re.findall(gbx_header, gbx_data)
What is the correct use of the regular expression in gbx_header, so that this warning is not displayed?
You can check the Python bug tacker Issue 39394, the warning was introduced in Python 3.6.
The point is that the Python re now does not allow using inline modifiers not at the start of string. In Python 2.x, you can use your pattern without any problem and warnings as (?s) is silently applied to the whole regular expression under the hood. Since it is not always an expected behavior, the Python developers decided to produce a warning.
Note you can use inline modifier groups in Python re now, see restrict 1 word as case sensitive and other as case insensitive in python regex | (pipe).
So, the solutions are
Putting (?s) (or any other inline modifier) at the start of the pattern: (?s)(<header)(.*)(</header>)
Using the re option, re.S / re.DOTALL instead of (?s), re.I / re.IGNORECASE instead of (?i), etc.
Using workarounds (instead of ., use [\w\W]/[\d\D]/[\s\S] if you do not want to use (?s) or re.S/re.DOTALL).

Template Literals no-useless-concat

I'm curious about the eslint warning "no-useless-concat".
From other stackoverflow questions I saw using template-literals is the preferred method of concatenating strings dynamically, in order to not get such eslint warnings.
I want to define a css-class based upon a condition (liked is a boolean):
const heartClass = `fa fa-heart${liked ? "" : "-o"}`;
While compiling (using React) I still get the following warning:
WARNING in src\components\Like.jsx
Line 6:55: Unexpected string concatenation of literals no-useless-concat
What am I doing wrong - Why do I still get the no-useless-concat warning?
Kind Regards
Andreas

configure sort order in sublimetext3

I know how to sort lines in Sublime (ctrl+p "sort"). The problem is it doesn't sort some characters as I want, namely åäö.
Is there some way to change how sublime orders the text? Perhaps by a specific locale? It would also be interesting to be able to sort V and W as being equal.
Example: I want the following words to be sorted in this order:
bår
bär
bör
but Sublime sorts it like this:
bär
bår
bör
There has been a similar request logged on ST's issue tracker: https://github.com/SublimeTextIssues/Core/issues/1324
One of the ST developers replied:
Sorting in Python 3 uses Unicode code points as the basis for sorting. Sublime Text doesn't know what language your encoding represents, so it doesn't use locale-based sorting rules.
This seems like it is probably best solved by a package dedicated to providing locale-based collation rules.
Using https://packagecontrol.io/packages/PackageResourceViewer, we can see that the case_sensitive_sort method in Packages/Default/sort.py uses Python's built in list.sort method. Typing the following into ST's Python console (View menu -> Show Console), we get the same result as you have shown:
>>> a = ['bår', 'bär', 'bör']
>>> a.sort()
>>> a
['bär', 'bår', 'bör']
So the answer is that there is no setting to configure the sorting behavior, and nor is there likely to be in future. However, according to https://docs.python.org/3/howto/sorting.html#odd-and-ends, it is possible to use a locale-aware sort using locale.strxfrm as a key function.
Let's try. On Windows, I had to use
>>> import locale
>>> locale.setlocale(locale.LC_COLLATE, 'sve')
'Swedish_Sweden.1252'
to get Python to use a Swedish locale - as per https://stackoverflow.com/a/956084/4473405
>>> a.sort(key=locale.strxfrm)
>>> a
['bår', 'bär', 'bör']
Using this knowledge, you might choose to change the case_sensitive_sort method, so that ST's built in sort functionality (Edit menu -> Sort Lines (Case Sensitive)) will use the locale aware sort key. Note that saving the sort.py file opened from PackageResourceViewer will create an override, so that if future builds of ST include changes to sort.py, you won't see them until you delete the override (which you can do by finding the file using the Preferences menu -> Browse Packages -> Default. You can reapply your changes afterwards, if appropriate, using the exact same steps.)
You can also change the case_insensitive_sort method from
txt.sort(key=lambda x: x.lower())
to
txt.sort(key=lambda x: locale.strxfrm(x.lower()))
Note that, if your correct locale isn't picked up automatically (it probably defaults to C), then setting the locale in this (case_sensitive_sort) method isn't recommended, even if, immediately afterwards, you restore it back to what it was beforehand - so use at your own risk.
It is generally a bad idea to call setlocale() in some library routine, since as a side effect it affects the entire program. Saving and restoring it is almost as bad: it is expensive and affects other threads that happen to run before the settings have been restored.
You could instead add the following to the end of the sort.py file:
def plugin_loaded():
import locale
locale.setlocale(locale.LC_COLLATE, '')
which will, when the plugin is loaded, allow Python to infer the locale from your LANG env var, as per https://docs.python.org/3/library/locale.html#locale.setlocale. The advantage being you only set it once, and hopefully won't introduce any problems with other plugin code executing at the same time.
Happy sorting!

IS warning about string ID "IDS_ERROR_27555" not being found in string table

I have an Installshield2010 project that has had this warning since before my employment
ISDEV : warning -3028: The string ID "IDS_ERROR_27555" was not found in the string table. It is referenced in the table:"Error" in column:"Message".
Looking in the svn history, this row was added to Error table a few years ago - alongside a seemingly unrelated change
<row><td>27555</td><td>##IDS_ERROR_27555##</td></row>
However as the error indicates, there is no corresponding entry in the ISString table.
I'm curious how it got added. The IDS_ERROR strings look like canned strings, it seems really unlikely the person who added this to my ism did so manually.
Of course Flexera's forums are a ghost town. I did find an ism that has that string defined as
<row><td>IDS_ERROR_27555</td><td>1033</td><td>Error attempting to apply permissions to object '[2]'. System error: [3] ([4])</td><td>0</td><td/><td>1394414478</td></row>
Do any of your ism's have IDS_ERROR_27555 defined or referenced? Am very sure I can delete the string and be no worse for wear... but it bugs me to not know what happened.
I don't remember in which version we added these strings, but you can get them by importing the strings file for your language (1033.txt for English) under the Languages folder. Upgrading across versions of InstallShield should have added this string. You can add them yourself by importing the strings file in the String Editor view. Of course if you've modified other strings, you'll want to ensure that you keep your preferred version.

How can I find an <acronym> tag with watir-webdriver without taking a huge performance hit?

I am using watir-webdriver (0.5.3) in a Cucumber (1.1.9) test. I am attempting to verify the text value of an <acronym> tag. The code is legacy, and there are plans to change it to a <div> or <span> tag, but in the mean time I have to deal with it. I first attempted:
#browser.acronym(:id => /expense_code(.*)/).text
I received the following error:
NoMethodError: undefined method `acronym' for #<Watir::Browser:0x33e9940>
I poked around in the Watir code to see how tag objects were being created, and found that they seem to be dynamically created based on the HTML5 spec, but then I also found a comment in element.rb stating that they are no longer being created from the spec. At any rate, I couldn't see an easy way to inherit a <span> object and call it an <acronym> object. So, I looked into alternatives, and found the element object.
#browser.element(:id => /expense_code(.*)/).text
This code works, but it takes about a minute to traverse my page. I'm stuck with the regex for now, as the tag id is actually dynamically generated and I don't currently have a way to figure out those values. This is what the tag actually looks like:
<acronym class="editable select fillwith:exp_codes default:E100"
title="Expense Code: Expenses" id="expense_code114_582_10777">
E100 </acronym>
I would appreciate any thoughts on how I can improve the performance of my test.
Is that class name predictable? could you construct that from a set part plus the text you are about to validate (it's the same in your example above) and go that way?
acronym = 'E100'
browser.element(:class, 'editable select fillwith:exp_codes default:#{acronym}'.text.should == acronym
Does using XPath to limit the elements to just acronym tags help performance?
#browser.element(:xpath, "//acronym[contains(#id, 'expense_code')]")
UPDATE: As Chuck mentioned, CSS-Selector is also an option:
#browser.element(:css => "acronym[id^=expense_code]")
I was recently stealing logic from Watir 1.6.5 to make custom locators/collections for my page objects and I noticed in the Watir::TaggedElementLocator, it kind of supports any method that the element supports. Noticing in Watir-Webdriver that elements have a tag_name() method, I thought I would try the same and it looks like it works.
So you can use tag_name as a locator by doing:
#browser.element(:tag_name => 'acronym', :id => /expense_code(.*)/).text
I'm not sure what order the locators get run in, so since the regex is expensive, it might be faster to get all the acronym elements and then find the one with the right ID:
#browser.elements(:tag_name, 'acronym').find{ |acronym|
acronym.id =~ /expense_code(.*)/
}.text
While I think it makes the code look better, unfortunately I'm not sure if its any faster. I am guessing the performance of each will depend on the specific page layout being tested.
I'm not sure what the proper etiquette is here, but this is the answer I came up with using Chuck's reply and feedback from jarib in the #watir IRC chat. With all my examples, expense_code = 'E100'.
#browser.element(:tag_name => "acronym",
:class => "default:#{expense_code}").text
The above code works at a very reasonable speed and doesn't require an xpath. It is a shortening of the following code:
#browser.element(:tag_name => "acronym",
:class => "editable select fillwith:exp_codes default:#{expense_code}").text
I learned that I didn't need to pass the whole string. Anything in a class delimited by a space is dealt with gracefully by watir. I adapted that code from this xpath:
#browser.element(:xpath => "//acronym[contains(#class,
\'editable select fillwith:exp_codes default:#{expense_code}\')]").text
The gotcha in that code above was needing to escape out the ' around the class values so that it would evaluate correctly.
Just searching for the class (code below) did not work. I have no idea why. I did notice that it pounded the database with requests. Whatever it was doing, the page didn't like it. Though the reason it was trying multiple times is I slipped a wait_until_present in there.
#browser.element(:class, "editable select fillwith:exp_codes
default:#{expense_code}").text
Thanks for the help. :)

Resources