try..catch..which assert is failed?

106 views
Skip to first unread message

Jürgen Clemens

unread,
Aug 17, 2011, 9:25:30 AM8/17/11
to Selenium Users
Hi,

I have a try block with multiple assertTrue-statements. If a assert
fails I want to know which assert statement has failed. is ist
possible and how can I implement it. In my catch statement, I only get
the information "Failed asserting that <boolean:false> is true
-----------
Here is the code

try {
$this->assertTrue($this->isTextPresent("Mailaddresses"));
$this->assertTrue($this->isTextPresent("activated"));
$this->assertTrue($this->isTextPresent("main mailaddress"));
........
} catch (PHPUnit_Framework_assertionFailedError $e) {

print($e->toString());
}
-----------
Best regards

Jürgen

Krishnan Mahadevan

unread,
Aug 17, 2011, 9:30:39 AM8/17/11
to seleniu...@googlegroups.com
I would use Assert.assertTrue(condition, message) (I use TestNG asserts for my tests)

and let assertTrue method print me the contents of "message" everytime my condition fails and not have to include a try catch block for that.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Paul Hammant

unread,
Aug 17, 2011, 9:50:20 AM8/17/11
to seleniu...@googlegroups.com
If I were you I'd take out the try/catch.  The resulting failure will be explicit about which one failed.  

After all, ALL you're doing in the catch block is printing the failure right??  Therefore its extraneous - PHPUnit is perfectly good at driving developers to root causes of exceptions.

- Paul

Jürgen Clemens

unread,
Aug 17, 2011, 9:57:12 AM8/17/11
to Selenium Users
Thank you for the answer,

but I need the try..catch block to ensure a sane cleanup in error
case. The background is the following:

First I add an new user to the database. After that I test some
functions. After the tests i delete the user again. So If i get an
error during my tests i want to set back my application to the
original state.

If I use the proposed message option, the message will be not
displayed.

Best regards

Jürgen


On 17 Aug., 15:30, Krishnan Mahadevan

Paul Hammant

unread,
Aug 17, 2011, 10:42:25 AM8/17/11
to seleniu...@googlegroups.com
Does PhpUnit not have a tearDown() concept that'll be called regardless ?

- Paul

Krishnan Mahadevan

unread,
Aug 17, 2011, 10:42:37 AM8/17/11
to seleniu...@googlegroups.com
Configuration methods within TestNG (such as @AfterClass, @AfterTest or @AfterSuite) helps you do just that, irrespective of whether a test passes or fails (due to an Assert failing) these configuration methods would do that.
But then again, am not too sure if there are such things with PHPUnit.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Jürgen Clemens

unread,
Aug 17, 2011, 10:45:06 AM8/17/11
to Selenium Users
Here is the compete catch-part:
--------
catch (PHPUnit_Framework_assertionFailedError $e) {
$this->cleanUpOnError();
}
---------

In the function cleanUpOnError() i delete the testuser and set the
application back to the original state.

Best regards

Jürgen


On 17 Aug., 15:57, Jürgen Clemens <juergen.clem...@googlemail.com>
wrote:

Jürgen Clemens

unread,
Aug 17, 2011, 10:50:23 AM8/17/11
to Selenium Users
Thats a good point. Until now I have no teardown-function implemented.
Perhaps thats the way I have to go.

Greetings

Jürgen

Jürgen Clemens

unread,
Aug 18, 2011, 4:04:09 AM8/18/11
to Selenium Users
Hi,

now i added the function teardown() where I want to set back the
application to the original state. Unfortunately it doesn't work.
After my test the browser windows close automatically and I get the
error

"ERROR Server Exception: sessionID should not be null..."

Whats the problem?

Best regards

On 17 Aug., 16:50, Jürgen Clemens <juergen.clem...@googlemail.com>
wrote:

niharika varshney

unread,
Aug 18, 2011, 4:12:21 AM8/18/11
to seleniu...@googlegroups.com
I believe, the teardown would be run each time your case runs, not just when the case fails...but even when a clean execution has occurred..
So I hope you are keeping your selenium session ending code in the teardown and not in your test ..
coz the error suggests that the selenium session is already dead..before teardown executes..whereas it shud be the last thing in ur teardown.

Regards,
Niharika

Jürgen Clemens

unread,
Aug 18, 2011, 4:45:26 AM8/18/11
to Selenium Users
Hi,

I have no session ending code in my tests. The problem is, that after
selenium has reached the end of the testfunction, it finishs the test
automatically. Previously I worked with no teardown-function and after
the test, it finished automatically as well. By the way I use PHPunit
and selenium RC. I start my test from commandline with "phpunit
test1.php"

Best regards

Jürgen

On 18 Aug., 10:12, niharika varshney <niharika.varsh...@gmail.com>
wrote:

niharika varshney

unread,
Aug 18, 2011, 5:07:29 AM8/18/11
to seleniu...@googlegroups.com
Well..hv never worked on phpunit ..so not sure about that..but was curious at how it works..

Seems there is a setautostop function, which needs to be set to false to prevent phpunit from automatically stopping the session..
and you need to explicityly then stop the session in your teardown...

stumbled upon the following..if it helps you

http://stackoverflow.com/questions/2846928/selenium-rc-throws-sessionsid-should-not-be-null-exception-with-asserttextpresent

Regards,
Niharika

Krishnan Mahadevan

unread,
Aug 18, 2011, 5:08:26 AM8/18/11
to seleniu...@googlegroups.com
Jürgen

Irrespective of technologies I am guessing that teardown() is something that gets always called. 
Can you please elaborate on what exactly does your teardown contain ?



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Jürgen Clemens

unread,
Aug 18, 2011, 5:49:57 AM8/18/11
to Selenium Users
Hi,

i have made the following setting in the test class

protected $autoStop = FALSE;

No it works and I can use my own teardown-function without the
autoteardown()-function of phpunit.

On 18 Aug., 11:08, Krishnan Mahadevan
<krishnan.mahadevan1...@gmail.com> wrote:
> Jürgen
>
> Irrespective of technologies I am guessing that teardown() is something that
> gets always called.
> Can you please elaborate on what exactly does your teardown contain ?
>
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
>
> On Thu, Aug 18, 2011 at 2:15 PM, Jürgen Clemens <
>
>
>
>
>
>
>

Jürgen Clemens

unread,
Aug 18, 2011, 8:54:41 AM8/18/11
to Selenium Users
It's a mess,

at first I thought it works, but now I have the problem, that the
session is killed if the tests runs without errors and reaches the
tearDown-function. Here is the short test:

----
<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class test_short extends PHPUnit_Extensions_SeleniumTestCase
{
protected $autoStop = FALSE;

public function setUp()
{
print ("setup\n");
$this->setBrowser("*firefox c:/Program Files/firefox.exe");
$this->setBrowserUrl("http://localhost/");
}
function testshort()
{
$this->open("/phpLogin.php");
$this->waitForPageToLoad("30000");
print ("before test\n");
$this->assertTrue($this->isTextPresent("Bitte"));
print ("after test\n");
}
public function tearDown()
{
print ("before tearDown\n");
$this->open("/ADMINISTRATION/index.php");
$this->waitForPageToLoad("30000");
print ("after tearDown\n");
}

}
?>

If the assertTrue fails, the test goes to the tearDown and calls the
URL, but if the assertTrue is true, the test seems to kill the session
and the firefox and after that tries to call the URL. So I get the
error about SessionID. How can I induce the test to call the
tearDown()-function without killing the test. Perhaps the AutoStop-
Parameter is not set correct?

Regards

Jürgen


On 18 Aug., 11:49, Jürgen Clemens <juergen.clem...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages