How to access a stored value in PHPUnit_Extensions_SeleniumTestCase - selenium

How can I store a value within Selenium-RC (through PHPUnit) and then retrieve/access it later using PHPUnit?
Suppose I run a command like the following in a test:
$this->storeExpression( "foo", "bar" );
If I understand the Selenium API documentation correctly, I could access this data using javascript{storedVars['foo']} using good 'ol fashioned Selenese. It should contain the value "bar".
My question is this: how can I access this javascript{storedVars['test']} expression (or, more generally, javascript{storedVars} in PHPUnit?
For example, here's a simple test I've run:
public function testStorage()
{
$this->open('http://www.google.com/'); // for example
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression('foo');
echo $foo;
}
The output of which is "foo" (among the other standard PHPUnit output), while I expect it should be "bar". It's just giving me back the name of the expression, not its value.
Can anyone with experience in this give me some guidance?

Good posts in this thread, but looks like no 100% working answer so far.
Based on the Selenium reference here
http://release.seleniumhq.org/selenium-core/1.0/reference.html#storedVars
It would seem the correct code syntax would be:
$this->storeExpression( 'bar', 'foo' );
$foo = $this->getExpression("\${foo}");
I haven't tested that exactly, but doing something similar with
$this->storeHtmlSource('srcTxt');
$val = $this->getExpression('\${srcTxt}');
print $val;
did the trick for me.

The PHPUnit Selenium Testcase driver actually understands storeExpression and getExpression; have a look at its source code. You can do
$this->storeExpression('foo', 'bar');
and
$this->getExpression('foo');

As Selenium Stores the expression result in second argument it stores value in "bar" and when u need to call it you should call the stored name to get the expression.
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression("bar");
May this helps you it worked for me.
EDIT :
$evaluated = $this->getEval("regex:3+3");
$expressed = $this->getExpression("regex:3+3");
The First Evaluated will give the evaluated output for expression
and the second will show the expressed output.
The secound is used to verify that the specified expression is genrated or not by the alert.

Related

How to call traverseLinkedWorkItems method using Velocity script block?

In the Polarion documentation:
https://almdemo.polarion.com/polarion/sdk/doc/javadoc/com/polarion/alm/tracker/model/IWorkItem.html#traverseLinkedWorkitems(java.util.Set,java.util.Set,java.util.Set,com.polarion.alm.tracker.model.IWorkItem.ITerminalCondition)
I have created empty sets using $objectFactory.newSet() to account for the first 3 parameters, and I have tried "null" for the conditional parameter, but nothing works.
This is an example of what I have tried:
#set($project = "Project X"
#set($workItem1 = 'ABC-123')
#set($emptySet = $objectFactory.newSet())
#set($ts1 = $trackerService.getWorkItem($project,$workItem1))
$ts1 ##output: PObject(WorkItem; subterra:data-service:objects:/default/Project X${WorkItem}ABC-123)
$ts1.traverseLinkedWorkItems($emptySet,$emptySet,$emptySet,'null')
The output is always $ts1.traverseLinkedWorkItems($emptySet,$emptySet,$emptySet,'null')
Is there no way to do this in Velocity? I have seen only one other post regarding this question:
https://community.sw.siemens.com/s/question/0D54O000075P0SCSA0/any-way-to-call-traverselinkedworkitems-from-a-velocity-script-block-widget
Have you tried $null as the last argument? As an undefined reference, it will translate to null.
But this solution will only work if Velocity is not running in strict mode.

How to pass variable to groovy code in Intellij IDEA live templates groovy script?

I have a groovyScript in my Intellij IDEA live template, like this :
groovyScript("D:/test.groovy","", v1)
on my D:/test.groovy i have a code like this:
if ( v1 == 'abc') {
'abc'
}
Now I want to pass v1 variable into test.groovy ,can any one help me how can I do this?
For exemplification purposes I made a live template which is printing a comment with the current class and current method.
This is how my live template is defined:
And here is how I edited the variableResolvedWithGroovyScript variable:
The Expression for the given variable has the follwing value:
groovyScript("return \"// Current Class:\" + _1 + \". Current Method:\"+ _2 ", className(),methodName())
As you can see, in this case the _1(which acts like a variable in the groovy script) takes the value of the first parameter which is the class name, and the _2 takes the value of the
second parameter which is the method name. If another parameter is needed the _3 will be used in the groovy script to reference the given parameter.
The arguments to groovyScript macro are bound to script variables named _1, _2 etc. This is also described at groovyScript help at Edit Template Variables Dialog / Live Template Variables.
I found a solution.
I was needing to calculate a CRC32 of the qualified class name using live models
I used it like this:
groovyScript("
def crc = new java.util.zip.CRC32().with { update _1.bytes; value };
return Long.toHexString(crc);
", qualifiedClassName())
then the result is
Based on the documentation, your variables are available as _1, _2, etc. Please note that variables are passed without dollar signs (so only v1 instead of $v1$)
So your test script should look like
if ( _2 == 'abc') {
'abc'
}

Mule ESB: How to do Condition checking in Datamapper using Xpath

i'm facing issue in xpath-I need do a check two attribute values, if the condition satisfies need to do hard code my own value. Below is my xml.
I need to check the condition like inside subroot- if ItemType=Table1 and ItemCondition=Chair1 then i have to give a hard coded value 'Proceed'( this hard coded value i will map to target side of datamapper).
<Root>
<SubRoot>
<ItemType>Table1</ItemType>
<ItemCondition>Chair1</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
<SubRoot>
<ItemType>Table2</ItemType>
<ItemCondition>chair2</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
....Will have multiple subroot
</Root>
I have tried to define rules as below, but it is throwing error
Type: String
Context:/Root
Xpath: substring("Proceed", 1 div boolean(/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
But it is throwing error like
net.sf.saxon.trans.XPathException: Arithmetic operator is not defined for arguments of types (xs:integer, xs:boolean)
Is there any other shortcut way to perform this.Could you please help me, i have given lot more effort. Not able to resolve it. Thanks in advance.
I am not sure where you are applying this but the XPath expression you are looking for is:
fn:contains(/Root/SubRoot[2]/ItemCondition, "chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")
So here is an example returning "Proceed" or "Stop" as appropriate:
if (fn:contains(/Root/SubRoot[1]/ItemCondition, "Chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")) then 'Proceed' else 'Stop'
To implement the above condition , i was initially tired to do in xpath, gave me lot of error. I have implemented by simple if else condition in script part of data mapper
if ( (input.ItemType == 'Table') and (input.ItemCondition == 'chair')) {
output.Item = 'Proceed'}
else {
output.Item = 'Stop '};
Make sure about your precedence. Example, Here in the xml structure( or converted POJO) ItemType has to be checked first then followed with ItemCondition.
&& not seems to be working for me, change to 'and' operator
If you were first time trying to implement the logic. It may help you.

How do I assert to accept one of multiple possible values in JUnit

I have a test that returns some value after completion. The returned value is a String and can have one of several possible values based on some if condition in the test.
But in the assert statement I can check only one of the Expected values not both.
How can I do this? Thank you.
I will use AssertJ for this
assertThat(value).isIn(expected1, expected2, expected3);
The code is much simpler than with Hamcrest.
My solution was to save the result and then run a Jtest that the result was in an array of accepted solutions.
E.g:
`it("Test Message", () => {
const result = testFunction(inputValue);
assert.equal((result in [possibeResult1, possibleResult2, ...]), true);
});`

Can I pretty-print the DBIC_TRACE output in DBIx::Class?

Setting the DBIC_TRACE environment variable to true:
BEGIN { $ENV{DBIC_TRACE} = 1 }
generates very helpful output, especially showing the SQL query that is being executed, but the SQL query is all on one line.
Is there a way to push it through some kinda "sql tidy" routine to format it better, perhaps breaking it up over multiple lines? Failing that, could anyone give me a nudge into where in the code I'd need to hack to add such a hook? And what the best tool is to accept a badly formatted SQL query and push out a nicely formatted one?
"nice formatting" in this context simply means better than "all on one line". I'm not particularly fussed about specific styles of formatting queries
Thanks!
As of DBIx::Class 0.08124 it's built in.
Just set $ENV{DBIC_TRACE_PROFILE} to console or console_monochrome.
From the documentation of DBIx::Class::Storage
If DBIC_TRACE is set then trace information is produced (as when the
debug method is set). ...
debug Causes trace information to be emitted on the debugobj
object. (or STDERR if debugobj has not specifically been set).
debugobj Sets or retrieves the object used for metric collection.
Defaults to an instance of DBIx::Class::Storage::Statistics that is
compatible with the original method of using a coderef as a callback.
See the aforementioned Statistics class for more information.
In other words, you should set debugobj in that class to an object that subclasses DBIx::Class::Storage::Statistics. In your subclass, you can reformat the query the way you want it to be.
First, thanks for the pointers! Partial answer follows ....
What I've got so far ... first some scaffolding:
# Connect to our db through DBIx::Class
my $schema = My::Schema->connect('dbi:SQLite:/home/me/accounts.db');
# See also BEGIN { $ENV{DBIC_TRACE} = 1 }
$schema->storage->debug(1);
# Create an instance of our subclassed (see below)
# DBIx::Class::Storage::Statistics class
my $stats = My::DBIx::Class::Storage::Statistics->new();
# Set the debugobj object on our schema's storage
$schema->storage->debugobj($stats);
And the definition of My::DBIx::Class::Storage::Statistics being:
package My::DBIx::Class::Storage::Statistics;
use base qw<DBIx::Class::Storage::Statistics>;
use Data::Dumper qw<Dumper>;
use SQL::Statement;
use SQL::Parser;
sub query_start {
my ($self, $sql_query, #params) = #_;
print "The original sql query is\n$sql_query\n\n";
my $parser = SQL::Parser->new();
my $stmt = SQL::Statement->new($sql_query, $parser);
#printf "%s\n", $stmt->command;
print "The parameters for this query are:";
print Dumper \#params;
}
Which solves the problem about how to hook in to get the SQL query for me to "pretty-ify".
Then I run a query:
my $rs = $schema->resultset('SomeTable')->search(
{
'email' => $email,
'others.some_col' => 1,
},
{ join => 'others' }
);
$rs->count;
However SQL::Parser barfs on the SQL generated by DBIx::Class:
The original sql query is
SELECT COUNT( * ) FROM some_table me LEFT JOIN others other_table ON ( others.some_col_id = me.id ) WHERE ( others.some_col_id = ? AND email = ? )
SQL ERROR: Bad table or column name '(others' has chars not alphanumeric or underscore!
SQL ERROR: No equijoin condition in WHERE or ON clause
So ... is there a better parser than SQL::Parser for the job?