Watir Rspec Ruby : Comparing new/edited value with old/previous value - ruby-on-rails

Auto generated name twice, first
$name = generate_name
self.name = $name
produces: $name is "Abc"
And then edited the $name to be "Xyz". Which causes the previous/old name to be no longer available/saved in the app, its overwritten by new/edited name.
I now have to compare the two values of same variable $name to ensure that name is edited.
using expect($name).to_not eq($old_name)
I don't understand how to save the previous/old name into another variable $old_name before overwriting it?

Before you change $name to be "Xyz", or test for equality, you need to set $old_name to be equal to $name.
Something like this:
$name = generate_name
self.name = $name
$old_name = self.name
$name = Xyz
self.name = $name
using expect($name).to_not eq($old_name)

Related

DXL:Cannot assign to an attribute some properties found in history

DXL, ibm DOORS
Looping through module, then through every object history, I am trying to assign to "_Owner" attribute the author from obj history who modified last time "_ReqStatus" attribute, if "_Owner" is empty.
This is what I tried:
Module m = current
History h
HistoryType ht
Object o
string attributName
string authorName
string newOwner
noError()
for o in entire m do {
for h in o do
{
string owner = ""
attributName=""
attributName = h.attrName
authorName=""
owner = o."_Owner"
if isDeleted(o) then continue
if((attributName=="_ReqStatus"))
{
authorName=h.author
//print authorName
//print "\n"
if(null owner)
{
print identifier(o)
print "\n"
newOwner = authorName
print newOwner"\n"
owner = newOwner
print owner
break
}
}
}
}
ErrMess = lastError()
The output for print owner is as expected. My problem is that in-DOORS attribute is not filling at all with any value.
_Owner attribute type is Ennumeration and attribute properties look like this, but I don't know if it matters:
"_Owner" attr properties
When you assign string owner = o."_Owner", the variable owner is not a handle to the object attribute itself, but the content of o's "_Owner" attribute is copied to owner. So, in your later recalculation owner = newOwner, you only change that variable and not the attribute. Try o."_Owner" = newOwner instead.

Groovy: Dynamic nested properties [duplicate]

I am wondering if I can pass variable to be evaluated as String inside gstring evaluation.
simplest example will be some thing like
def var ='person.lName'
def value = "${var}"
println(value)
I am looking to get output the value of lastName in the person instance. As a last resort I can use reflection, but wondering there should be some thing simpler in groovy, that I am not aware of.
Can you try:
def var = Eval.me( 'new Date()' )
In place of the first line in your example.
The Eval class is documented here
edit
I am guessing (from your updated question) that you have a person variable, and then people are passing in a String like person.lName , and you want to return the lName property of that class?
Can you try something like this using GroovyShell?
// Assuming we have a Person class
class Person {
String fName
String lName
}
// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )
// And given a command string to execute
def commandString = 'person.lName'
GroovyShell shell = new GroovyShell( binding )
def result = shell.evaluate( commandString )
Or this, using direct string parsing and property access
// Assuming we have a Person class
class Person {
String fName
String lName
}
// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )
// And given a command string to execute
def commandString = 'person.lName'
// Split the command string into a list based on '.', and inject starting with null
def result = commandString.split( /\./ ).inject( null ) { curr, prop ->
// if curr is null, then return the property from the binding
// Otherwise try to get the given property from the curr object
curr?."$prop" ?: binding[ prop ]
}

Groovy - Append values to variables in a string

Lets assume I have a domain class called Template that looks somewhat like this:
class Template{
String subject
...
}
I save an instance of this class:
Template t=new Template(subject:'Hello ${name}').save()
Now, I fetch this instance inside a method as follows:
def render(Long id){
String name='foo'
Template t= Template.get(id)
println t.subject
}
I want the "println t.subject" to be printed as "Hello foo". What I get is "Hello ${name}".
I want t.subject to dynamically replace the value of variable name - "foo" in place of ${name}.
Can this be achieved in groovy?
I cannot find any documentation of how to do this, or why this cannot be done.
Update:
I tried this on my GroovyConsole.
class Entity{
String name
}
class Template{
String name
String subject
}
String renderTemplate(Template template, Entity entity){
return template.subject
}
Entity e = new Entity(name:'product')
Template template=new Template(name:'emailTemplate',subject:'Product ${e.name}')
renderTemplate(template,e)
The Output I got was:
Result: Product product
class Template {
String subject
// ...
}
Template t = new Template(subject: 'Hello, ${name}').save()
Important: Use single quotes in 'Hello, ${name}' or you will get an error.
def render(Long id) {
String name = "world"
Template t = Template.get(id)
​def engine = new groovy.text.GStringTemplateEngine()
def subject = engine
.createTemplate(t.subject)
.make(name: name)​
println subject
}
There are a couple of things wrong with the code shown. You have this:
class Template{
String subject
...
}
Then you have this:
Template t=new Template(subject:"Hello ${name}").save()
The Groovy String assigned to the subject property will be evaluated as soon as you assign it to subject because subject is a String. The actual value will depend on the value of the name property that is in scope when that code executes.
Later you have this:
def render(Long id){
String name="foo"
Template t= Template.get(id)
println t.subject
}
It looks like you are wanting that local name property to be substituted into a Groovy String that has been assigned to t.subject, but that isn't how Groovy works. t.subject is not a Groovy String. It is a String.
Separate from that you comment that when you println t.subject that the output is "Hello ${name}". I don't think that is possible, but if that is what you are seeing, then I guess it is.
You can use a transient property to emulate the required behavior:
class Template{
String subject
String getSubjectPretty(){ "Hello $subject" }
static transients = ['subjectPretty']
}
Then you can use:
println Template.get(1).subjectPretty

Grails - access data within Config.groovy whilst using variable in path

I know that to access a variable from within the Config.groovy file,
grailsApplication.config.someValue.anotherValue
I want to use a variable within this path because I want to get a URL from the config file. The value foo is passed in as a parameter to the method which will be called a number of times using different variables based on other factors.
def foo = "anothervalue"
grailsApplication.config.someValue.${ foo }.
The actual path to the value I want in the config stays the same as in the first instance.
I have tried:
grailsApplication.config.someValue.${ foo }
grailsApplication.config.someValue${ foo }
grailsApplication.config.someValue + "${ foo }"
grailsApplication.config.someValue + ".${ foo }"
grailsApplication.config.someValue + ${ foo }
grailsApplication.config.someValue."${ foo }" must works.
grailsApplication.config returns a groovy.util.ConfigObject like groovy.util.ConfigSlurper.parse() so you can see how it works in the follow example:
import groovy.util.ConfigSlurper
def configTxt = '''
prop1 {
prop2 {
person.name = 'paco'
}
}
'''
def config = new ConfigSlurper().parse(configTxt)
def foo = "prop2"
println config.prop1."${foo}" // prints [person:[name:paco]]
Hope this helps,
more natural would be grailsApplication.config.someValue[ foo ]

How would I find the text of a node that has a specific value for an attribute in groovy?

I'm using XMLSlurper. My code is below (but does not work). The problem is that it fails when it hits a node that does not have the attribute "id". How do I account for this?
//Parse XML
def page = new XmlSlurper(false,false).parseText(xml)
//Now save the value of the proper node to a property (this fails)
properties[ "finalValue" ] = page.find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
I just need to account for nodes without "id" attribute so it doesn't fail. How do I do that?
You could alternatively use the GPath notation, and check if "#id" is empty first.
The following code snippet finds the last element (since the id attribute is "B" and the value is also "bizz", it prints out "bizz" and "B").
def xml = new XmlSlurper().parseText("<foo><bar>bizz</bar><bar id='A'>bazz</bar><bar id='B'>bizz</bar></foo>")
def x = xml.children().find{!it.#id.isEmpty() && it.text()=="bizz"}
println x
println x.#id
Apprently I can get it to work when I simply use depthFirst. So:
properties[ "finalValue" ] = page.depthFirst().find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};

Resources