Try Catch

346 views
Skip to first unread message

jeevan

unread,
Feb 29, 2012, 10:09:54 AM2/29/12
to testng...@googlegroups.com
Hi Cedric,
 
I was having an issue with test case if i use Try catch block.
 
Issue:
Example Test Case:
@Test
public void test(){
     try{
       here i am using the page objects.
  }
catch(Exception e){
  
}
}
 
Console Output:

Verified SearchTerm
Exception in addToSearch org.openqa.selenium.NoSuchElementException: Unable to find element with id == 'mLayout_ctl00_ctl00_ctl00_mQueryEditorLoader_mUserQueryPanel_ctl00_ctl01_mJurisdictionClearAll' (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 344 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.17.0', revision: '15540', time: '2012-01-16 16:52:31'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_30'
Driver info: driver.version: RemoteWebDriver
Exception in addToSearch org.openqa.selenium.NoSuchElementException: Unable to find element with id == 'mLayout_ctl00_ctl00_ctl00_mQueryEditorLoader_mUserQueryPanel_ctl00_ctl01_mJurisdictionSelectAll' (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 374 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.17.0', revision: '15540', time: '2012-01-16 16:52:31'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_30'
Driver info: driver.version: RemoteWebDriver
PASSED: spc_f_test on instance null(test)
===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.EmailableReporter@1a679b7: 16 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1e51060: 47 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@337d0f: 15 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e102dc: 0 ms
[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms
 
If i remove try catch, i am able to get the result as expected. But if i put the test case between try catch even though test case fails, my report shows it passed.
Please help me.
Thanks,
Jeevan.
 

Krishnan Mahadevan

unread,
Feb 29, 2012, 10:21:19 AM2/29/12
to testng...@googlegroups.com
If a @Test annotated method throws exceptions and if
1. It is not being gobbled by try catch block or
2. If it doesnt have expectedExceptions attribute

Then TestNg will mark that method as failed. 
In your case you are catching exceptions which explains why your test is being reported as pass. 

What exactly are you trying to do here?
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

jeevan

unread,
Feb 29, 2012, 11:01:23 AM2/29/12
to testng...@googlegroups.com

Hi Krishnan,
 
Even if i place my code between try catch block, i am getting the report as passed. So i just wanted to know how to make the test case as failed when it's not able to find any element or any other cause. if i remove try catch, then the  report shows test case failed and it is as excpeted.
 
Say for example: In Testcase, try to click on an element SignOn Button. I have created a keyword for the same in my Utility as below:

try{

Wait<WebDriver> wait =

new WebDriverWait(driver, 60);

WebElement object = wait.until(visibilityOfElementLocated(By.id("SignOn")

));

if(object!=null){

if(!object.isSelected()){

object.click();

}

}

}

catch(Exception e){

logger.warning("Error When calling click.");

System.

out.println("Exception" + e.getMessage());

}

If test case failed to locate the element it says timeout after 60 seconds in my eclpise console but report shows test case passed.

If i removed try catch in the keyword and in the test case , then the report shows test case failed.Could you tell me where am i wrong?

 

Thanks,
Jeevan

Cédric Beust ♔

unread,
Feb 29, 2012, 11:43:00 AM2/29/12
to testng...@googlegroups.com
Krishnan explained all there is to explain. Are you sure you understand how try/catch works?

-- 
Cédric

martino turturiello

unread,
Feb 29, 2012, 12:53:11 PM2/29/12
to testng...@googlegroups.com
Hi there,

for testNg a test is pass if no exception are thrown so if you catch the exception testng will never know about it!!


Cheers
-----------------------------------------------------
Chi non ha apprezzato il suo maestro né la sua lezione, un giorno sarà forse colto, ma non sarà mai saggio.


Numero telefonico 09711835500
Skype: turturiellomartino

sabf

unread,
Mar 1, 2012, 2:07:13 AM3/1/12
to testng...@googlegroups.com
if you need to fail your test in the catch block you can use this:

import org.testng.AssertJUnit.*;

fail("Error When calling click.");

That is how i bring a test to fail.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Tomek Kaczanowski

unread,
Mar 1, 2012, 6:40:34 AM3/1/12
to testng...@googlegroups.com
2012/3/1 sabf <s.a...@gmx.de>:

> if you need to fail your test in the catch block you can use this:
> import org.testng.AssertJUnit.*;
> fail("Error When calling click.");
> That is how i bring a test to fail.
Is it required to use AssertJUnit?

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://kaczanowscy.pl/tomek

jeevan

unread,
Mar 1, 2012, 6:43:15 AM3/1/12
to testng...@googlegroups.com
Hi Cedric,
 
I understood what Krishnan was saying but my doubt is how can i make test case fail when i get an exception with the help of try catch block? So i understood if test case throws an exception and the same was caught then TestNG considers that particular test case as passed. As per krishnan need to use either expectedException or need to remove try catch block in the test case level or utility class keywords level. please guide me Cedric if i am wrong. Is there any option to make test case as failed in the catch block as sabf  said, like :
 
catch(Exception e){
fail("Error When calling click.");
}

Thanks,
Jeevan

Krishnan Mahadevan

unread,
Mar 1, 2012, 6:53:07 AM3/1/12
to testng...@googlegroups.com
Yes there is. You would need to catch your exception and then use Assert.fail() which should also fail your test. 

jeevan

unread,
Mar 1, 2012, 10:23:54 AM3/1/12
to testng...@googlegroups.com
Thanks a lot Krishnan. I thought when ever exception occurs TestNG will also considers test case  as failed and update the same in the TestNG report as well. Now i am doing what you suggested and its working fine.

--
Reply all
Reply to author
Forward
0 new messages