Selenium PHP "verify" fails do not include line numbers - netbeans

Methods like verifyText do not report the line number of the failure, making it hard to find the point of failure.
The code created by Selenium IDE PHPUnit export looks like this:
try {
$this->assertEquals($text, $this->getTitle());
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
The output for this line looks like line 2 below, which is completely untraceable
Failed asserting that '' matches PCRE pattern "/Harlem/".
Failed asserting that two strings are equal.
Failed asserting that '(Pattern A)' matches PCRE pattern "/\(Pattern B\)/".
I've amended the call to include the referenced text which lets me search for the text failure, but in a large test this is not sufficient. How do I get the line number/stacktrace for each verify failure within my code?
public function verifyTitle($text) {
$title = $this->getTitle();
try {
$this->assertEquals($text, $title);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors,
"Title is '$title' but should be '$text'");
}
}
Note: in order to get stacktraces returning references to my code for asserts, I am using the stacktrace hack

Created this verification Method to include (too much) stacktrace:
public function appendVerification($message,$e) {
array_push($this->verificationErrors,$message."\n".$this->dumpStack($e));
}
I also updated the referenced dumpStack method for PHPUnit tests to dumbly strip out framework calls by class name
protected function dumpStack(Exception $e) {
$stack = '';
foreach ($e->getTrace() as $trace) {
if (isset($trace['file']) &&
isset($trace['line'])) {
if (!isFramework($trace['file']))
$stack .= PHP_EOL .
$trace['file'] . ':' .
$trace['line'] . ' ';
}
}
return $stack;
}
function isFramework($fileName) {
$test = ((preg_match("/PHPUnit/i",$fileName) +
preg_match("/php.phpunit/i",$fileName)) > 0);
return $test;
}
End result, clickable in netbeans, free of any PHPUnit framework line numbers
Failed asserting that 'waitForElementPresent failed for selector: css=input#address.valid' is false.
C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:228
C:\dev\Automation_Dev\phpTests\library\SeleniumUtilsTestCase.php:115
C:\dev\Automation_Dev\phpTests\library\SeleniumTestCase.php:176
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:72
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:39
C:\dev\Automation_Dev\phpTests\usecases\CreateListingTest.php:23
C:\xampp\php\phpunit:46

Related

Why Dancer `halt` command bypass all events?

Example:
hook on_route_exception => sub {
# This code is not executed
}
hook on_handler_exception => sub {
# This code is not executed
}
hook after => sub {
# This code is not executed
}
hook after_error_render => sub {
# This code is not executed
}
hook before => sub {
if ($some_condition) {
halt("Unauthorized");
# This code is not executed :
do_stuff();
}
};
get '/' => sub {
"hello there";
};
I can find this piece of documentation:
Thus, any code after a halt is ignored, until the end of the route.
But hooks are after the end of route, so should not be ignored. Should be?
Why hooks are ignored too?
I would think that the reason is that the processing was halted. The
halt("Unauthorized");
would essentially return that content in the response object and no further events are required. The halt effectively halted all processing for that request/response.
That is a guess based on how it is behaving and the description.
A closer look at :https://metacpan.org/release/BIGPRESH/Dancer-1.3513/source/lib/Dancer.pm#L156
shows that after the Response Content is set to "Unauthorized" it calls:
Dancer::Continuation::Halted->new->throw
which dies:
https://metacpan.org/release/BIGPRESH/Dancer-1.3513/source/lib/Dancer/Continuation.pm#L14
sub throw { die shift }
At least that's how I read that code. Since it dies there is nothing else to do.
Likely a deliberate design decision based on the intention to halt.

Get blob uploaded data with pure Perl

In Javascript, I am sending a blob using XHR by the following code:
var v=new FormData();
v.append("EFD",new Blob([...Uint8Array...]));
var h=new XMLHttpRequest();
h.setRequestHeader("Content-type","multipart/form-data; charset=utf-8");
h.open("POST","...url...");
h.send(v);
In the server, I have created in Perl the following function, that suppose to implement CGI->param and CGI->upload:
# QS (Query String) receive in argument string for single parameter or array of many required parameters.
# If string been supplied: Return the value of the parameter or undef if missing.
# If array been supplied, a hash will be returned with keys for param names and their corresponding values.
# If the first argument is undef, then return hash with ALL available parameters.
sub QS {
my $b=$ENV{'QUERY_STRING'};
if($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$b,$ENV{'CONTENT_LENGTH'}) or die "E100";
}
my $e=$_[0]; my $t=&AT($e); my $r={}; my #q=split(/&/,$b);
my %p=(); if($t eq "A") { %p=map { $_=>1 } #{$e}; }
foreach my $i(#q) {
my ($k,$s)=split(/=/,$i); $s=~tr/+//; $s=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
if($t eq "") { $r->{$k}=$s; }
elsif($t eq "A") { if($p{$k}) { $r->{$k}=$s; } }
elsif($k eq $_[0]) { return $s; }
}
return $r;
}
# AT is a function for determining type of an object, and also a quck way to distinguish between just a string and a number.
sub AT {
if(!defined $_[0]) { return ""; } my $v=ref($_[0]);
if($v eq "") { return ($_[0]*1 eq $_[0])?"N":"S"; }
my $k={"ARRAY"=>"A","HASH"=>"H"};
return $k->{$v}||$_[0]->{_obt}||$v;
}
So in the main program it will be called as:
my $EFD=&FW::QS("EFD"); # FW = The module name where QS and AT are.
When I issuing the POST from the client, the script in the server does not pop-up any errors, and does not terminates - it continues to run and run and run.... Endlessly.... Consuming 100% CPU time and 100% memory - without any explanation.
I have these in the beginning of the script, though:
use strict;
use warnings;
use diagnostics;
but it still behave in such a way that I need to kill the script in order to terminate it...
Anyone know what I did wrong...? No infinite loop here, as far as I know... If I change the Blob to regular classic way of "...url...?EFD=dhglkhserkhgoi" then it works just fine, but I want a Blob....
Thanks a lot
This QS function is only usable for POSTs with an application/x-www-urlencoded body, which yours isn't.

Ignoring GET error of an unexisting webpage

I use WWW::Mechanize to fetch and process web pages. I have a piece of code, which looping through a list of web pages. It looks approximately like this:
while (<$readFileHandle>) {
$mech->get("$url");
}
Now the problem occurs when one of the web pages in the list does not exist for some reason(which is ok). The issue is that in this case - the program returns an error and exits. The error looks like that:
Error GETing <url> Not Found at <PATH/file.pl> line ...
How can I ignore such type of error? I want the program just keep running.
You need to use eval {}; for this:
while ( my $url = readline($readFileHandle) ) {
chomp $url;
eval {
$mech->get($url);
};
if ($#) {
#error processing code
}
}

Getting error messages from Zend_Db_Adapter::exec()

I'm working on a script to make versioning our database easier. In order to do this we have a number of files (each contains at least one statement and a couple have over a hundred) containing the SQL queries that we need to update the schema that are delimited using semicolons. It works nicely 90% of the time except occasionally one of the statements will fail and we're having problems getting these fixed. When this happens we have to delete the database and manually copy and paste each SQL statement in the failing file to test it. Some queries cause an exception but for some queries the function just returns 1.
I'm using the following code but I can't figure out how to find the statement that's having the problem:
$db = Zend_Db_Table::getDefaultAdapter();
try{
// loop through all the files
foreach($files as $file){
echo 'Running ', $file, PHP_EOL;
// use the connection directly to load sql in batches
if($db->exec(file_get_contents($dir . $file)) == 1){
// *how do I get the problem line here?*
return false;
}
$db->exec('INSERT INTO database_history SET file = "' . $file .'";');
}
} catch (Exception $e) {
echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
return false;
}
Initialise a counter var like in the following example. Increment it each iteration by one. Output the counter var, when there's an error. I've called the var $line in this example. The exit terminates the script, after the var has been output. If you don't want that, you can just leave it out.
$line = 0;
foreach($files AS $file){
$line++;
// ...
if($db->exec(file_get_contents($dir . $file)) == 1){
echo '<pre>'; print_r($line); echo '</pre>'; exit;
return false;
}
// ...
}

Sending a SOAP message with PHP

what I'm trying to do is send a load of values captured from a form to a CRM system with SOAP and PHP. I've been reading up on SOAP for a while and I don't understand how to go about doing so, does anybody else know?
In order to do this it might be easiest to download a simple soap toolkit like 'NuSOAP' from sourceforge.
And then you would code something like the following (example submission of ISBN number):
<?php
// include the SOAP classes
require_once('nusoap.php');
// define parameter array (ISBN number)
$param = array('isbn'=>'0385503954');
// define path to server application
$serverpath ='http://services.xmethods.net:80/soap/servlet/rpcrouter';
//define method namespace
$namespace="urn:xmethods-BNPriceCheck";
// create client object
$client = new soapclient($serverpath);
// make the call
$price = $client->call('getPrice',$param,$namespace);
// if a fault occurred, output error info
if (isset($fault)) {
print "Error: ". $fault;
}
else if ($price == -1) {
print "The book is not in the database.";
} else {
// otherwise output the result
print "The price of book number ". $param[isbn] ." is $". $price;
}
// kill object
unset($client);
?>
This code snippet was taken directly from, which is also a good resource to view
http://developer.apple.com/internet/webservices/soapphp.html
Hope this helps.
You probably found a solution since then - but maybe the following helps someone else browsing for this:
soap-server.php:
<?php
class MySoapServer {
public function getMessage ()
{
return "Hello world!";
}
public function add ($n1,$n2)
{
return $n1+n2;
}
}
$option = array ('uri' => 'http://example.org/stacky/soap-server');
$server = new SoapServer(null,$option);
$server->setClass('MySoapServer');
$server->handle();
?>
and soap-client.php
<?php
$options = array ('uri' => 'http://example.org/stacky/soap-server',
'location' => 'http://localhost/soap-server.php');
$client = new SoapClient(null,$options);
echo $client ->getMessage();
echo "<br>";
echo $client ->add(41,1);
?>