how to get the data in url using groovy code? - groovy

i want to get defect id from the url using groovy code (To build custom code in tasktop).
for example: I will have an dynamic url generated say www.xyz.com/abc/defect_123/ now I want to retrieve that letter that always starts from 17th position. and return the string
Please help..
Thanks in advance

Here are two possibilities. Please note that the "substring" option is very strict and will always start from the 16th position (what happens if the domain changes from www.xyz.com to www.xyzw.com?)
def str = 'www.xyz.com/abc/defect_123/';
def pieces = str.tokenize('/'); // prints defect_123
def from16 = str.substring(16); // prints defect_123/
println from16;
println pieces.last();

You should define this as dynamic url in UrlMappings.groovy file:
"www.xyz.com/abc/$defect_id" (controller: 'YourController', action: 'method_name')
and you can access the defect_id variable from YourController using params.defect_id

Related

What is the difference between 'page_source' and 'find_element_by_tag_name("body").text'?

Trying to find whether a text is present on UI login page (web-page).
I could verify it by 'driver.page_source()' and driver.find_element_by_tag_name("body").text
driver.page_source()
text = "abcd"
page_source = driver.execute_script("return document.body.innerHTML;")
if text in page_source:
return True
else:
return False
driver.find_element_by_tag_name("body").text
text = "abcd"
value = text in self.browser.find_element_by_tag_name("body").text
if value:
return True
else:
return False
What's the difference between method1 and method2 ?
Which one is preferred to do the required task ?
Which is faster ?
Or anySelenium-UI methods to be used ?
Any help would be appreciated. Looking for valuable inputs.
Any idea on this ? Any help here ?
Page source will give all the text including HTML tags, styles etc. as you have written yourself in execute script to return the innerHTML. So, all HTML code will be returned which obviously will contain the text too. You can also get the whole html with selenium too instead of using JavaScript executor by browser.page_source.
On the other hand, browser.find_element_by_tag_name("body").text will return all the text you see on the page without html tags.
To me, the 2nd method should be preferred and faster because you will have string of smaller length(without un-necessary html tags) and the actual text you are interested in.

How to skip a Parameter with Default Values in Groovy?

My Groovy method has 3 parameters and the last 2 have default values. I want to skip the second parameter, and only provide values for the first and the third like so..
def askForADate(girlsName, msg = 'Will you go out with me?', beg = 'pretty please!!') {
println "$girlsName, $msg $beg!"
}
askForADate('Jennifer',,'Because I love you!')
Right now this prints out...
Jennifer, Because I love you! pretty please!!!
So it looks like it is plugging the value I am passing in for the third parameter into the second.
How to fix that?
As doelleri said, you'll need to write two version of thie method.
Unless you'll use some groovy goodness with named arguments!
def askForADate(Map op, girlsName) {
println "$girlsName, ${op.get('msg', 'Will you go out with me?')} ${op.get('beg', 'pretty please!!')}!"
}
askForADate(beg: 'Because I love you!', 'Jennifer')
Prints out: Jennifer, Will you go out with me? Because I love you!!
See http://mrhaki.blogspot.com/2015/09/groovy-goodness-turn-method-parameters.html for more details
This solution has the clear disadvantage of reordering the arguments as now the girls name is last in line.

Gradle: How to filter and search through text?

I'm fairly new to gradle. How do I filter text in the following manner?
Pretend that the output/result I want to filter will be the two URLs below.
"http://localhost/artifactory/appNameIwant/moreStuffHereThatsDynamic"
> I want this URL
"http://localhost/artifactory/differentAppName"
> I don't want this URL
I want to put up a "match" variable that would be something like
variable = http://localhost/artifactory/appnameIwant
So essentially, the string will not be a perfect match. I want it to filter and provide back any URLs that start with the variable listed above. It cannot be a perfect match as the characters after the /appnameIwant/ will be changing.
I want to use a for loop to cycle through an array, with an if then statement to return any matches. For instance.
for (i=0; i < results.length; i++){
if (results[i] strings matches (http://localhost/artifactory/appnameIwant) {
return results[i] }
I am just filtering the URL strings themselves, not anything complicated inside the webpages.
Let me know if further explanation would be helpful.
Thanks so much for your time and help!
I figured it out - I just used
if (string.startsWith"texthere")) {println string}
A lot easier than I thought!

Start and stop parsing of a string

I grab a url and I want to parse and store two sections of the url
User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
So I want to start at (Confirmation?=) and stop at (&) and store the results
string = QVNERkFTREY
then for the second one I want to start at (&code=) and go to the end of the string and store that result
string = MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
I was have tried a few different things
Uri myUri = new Uri(Request.Url.AbsoluteUri);
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("Confirmation?=");
string param2 = myUri.Query.Split();
Pretty sure I should be going a different route here but any help would be appreciate. I am going to continue to google search for now. I appreciate the help.
EDIT: I feel as though LINQ should be able to help me here..hmm
I would use HttpUtility.ParseQueryString rather than try to parse the URL myself.
String val = "User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==";
System.Collections.Specialized.NameValueCollection parameters = System.Web.HttpUtility.ParseQueryString(val);
Console.Out.WriteLine(parameters[0]); // QVNERkFTREY=
Console.Out.WriteLine(parameters.Get("code"); // MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
You will need to add System.Web.dll, which you can read about here: Cannot add System.Web.dll reference

What do empty square brackets after a variable name mean in Groovy?

I'm fairly new to groovy, looking at some existing code, and I see this:
def timestamp = event.timestamp[]
I don't understand what the empty square brackets are doing on this line. Note that the timestamp being def'd here should receive a long value.
In this code, event is defined somewhere else in our huge code base, so I'm not sure what it is. I thought it was a map, but when I wrote some separate test code using this notation on a map, the square brackets result in an empty value being assigned to timestamp. In the code above, however, the brackets are necessary to get correct (non-null) values.
Some quick Googling didn't help much (hard to search on "[]").
EDIT: Turns out event and event.timestamp are both zero.core.groovysupport.GCAccessor objects, and as the answer below says, the [] must be calling getAt() on these objects and returning a value (in this case, a long).
The square brackets will invoke the underlying getAt(Object) method of that object, so that line is probably invoking that one.
I made a small script:
class A {
def getAt(p) {
println "getAt: $p"
p
}
}
def a = new A()
b = a[]
println b.getClass()
And it returned the value passed as a parameter. In this case, an ArrayList. Maybe that timestamp object has some metaprogramming on it. What does def timestamp contains after running the code?
Also check your groovy version.
Empty list, found this. Somewhat related/possibly helpful question here.
Not at a computer, but that looks like it's calling the method event.timestamp and passing an empty list as a parameter.
The same as:
def timestamp = event.timestamp( [] )

Resources