I'm looking for a safe way of building a string of a HTML element id from another element id.
I need to go from something like "attr_name_442_a" to "attr-row-422", the text part of the ids are consistent with just the number changing. This number will be in the range of 1 to 4 digits.
I thought of doing this but is there a better Ruby style method?
newID = oldID.gsub("_","-").gsub("name","row").gsub("-a","")
You may be able to pull this off with a single Regex
newID = oldID.gsub /(\w+)_name_(\d+)_a/, '\1-row-\2'
new_id = "#$1-row-#$2" if $old_id =~ /\A([^_]+)_name_(\d{1,4})_a\z/
I would probably add an else condition if the regex didn't match just so I knew I was catching everything.
Related
I need to add a description string to an existing comments variable, which contains either a string or nil. I want to separate the new description from any existing comments with a newline, but only if there are existing comments. I've come up with a couple ways that are kind of concise,
old_comments = comments + "\n" rescue ""
new_comments = old_comments + description
or
new_comments = [comments, description].compact.join("\n")
but I'm surprised there's not a less "tricky" way to squeeze this into a one-liner. Or is there?
[*comments, description].join($/)
I have the following pattern to check:
"MODEL_NAME"-"ID"."FORMAT_TYPE"
where, for example:
MODEL_NAME = [:product, :brand]
FORMAT_TYPE = [:jpg, :png]
First I wanted to check if the regexp is something like:
/^\w+-\d+.\w+$/
and I have also to check if the part of my string is part of my arrays. I want something more flexible than:
/^(product|brand)-\d+.(jpg|png)$/
which I could manage through my arrays. What is a good solution to do it?
/^(#{MODEL_NAME.join '|'})-\d+\.(#{FORMAT_TYPE.join '|'})$/
# => /^(product|brand)-\d+\.(jpg|png)$/
Sorry for this extreme beginner question. I have a string variable originaltext containing some multiline text. I can convert it into an array of lines like so:
lines = originaltext.split("\n");
But I need help sorting this array. This DOES NOT work:
lines.sort;
The array remains unsorted.
And an associated question. Assuming I can sort my array somehow, how do I then convert it back to a single variable with no separators?
Your only issue is a small one - sort is actually a method, so you need to call lines.sort(). In order to join the elements together, you can use the join() method:
var originaltext = "This\n\is\na\nline";
lines = originaltext.split("\n");
lines.sort();
joined = lines.join("");
I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.
I am using selenium and I am using xpath in target value .
I have a table and I need to count the number of rows.
For example I have an X path starting from //tr[2]/td/span/input to //tr[10]/td/span/input. Now I want to count the number of X path count by VerifyXpathCount.
I need to supply a regex in the Target field of Selenium like //tr[(*)]/td/span/input
I am not able to apply such regex so I need to add some regex.
How do I do this?
int cnt = selenium.getXpathCount("//tr[/td/span/input]");
//to deal with all element
for(int i=0;i<cnt;i++){
String loc = "//tr["+i+"]/td/span/input";
//use loc
}
in case if you want to find unique element than you can use other conditions with //tr[/td/span/input] instead of looping
try //tr[./td/span/input] or //tr[//td/span/input] to get/verify xpath count