Please what exactly am i doing wrong.
I have checked and checked but all to no avail.
I have also checked previous code but I am not getting an error so my code works fine but just slight error somewhere.
The code is running fine and assertTrue is behaving as expected but when I put it in the try/catch, I only get the log in the catch block, even when text was found.
I believe that if the assertTrue found the text, it should go to the next line of code in the try block and pass the test rather than the catch block. Don't get me wrong, I am not getting any error just that it's printing out the wrong message.
Code below including print out message in console.
public boolean verifyTextPresent(String value) throws Exception {
Thread.sleep(5000);
try{
boolean txtFound = driver.getPageSource().contains(value);
log.log(value + " : text Found, .......continue");
return txtFound;
}catch(Exception e)
{
log.log(value + " :NOT Found, check element again ot Contact developer.");
return false;
}
}
public static void verifySignOutBtn() throws Exception
{
log.header("VERIFY IF SIGN_OUT EXIST AND CLICKABLE.........");
callMethod.myAccountPageNative(CONSTANTElements.SIGN_IN_LINK);
Thread.sleep(2000);
log.header("LOCATE SIGN_OUT BTN, AND CLICK ......");
callMethod.elementPresent_Click(By.cssSelector(CONSTANTElements.SIGN_OUT_BTN));
Thread.sleep(4000);
log.header("VERIFY SIGN_OUT NAVIGATES TO HOME PAGE WHEN CLICKED......");
try{
Assert.assertTrue(callMethod.verifyTextPresent("SIGN IN"), "SIGN IN");
log.log("User Successfully Signed Out.......");
log.log("Test Passed!...");
//callMethod.close();
}
catch(Throwable e)
{
log.log("User NOT Successfully Signed Out.... Contact developer.");
log.log("Test Failed!...");
//callMethod.close();
}
callMethod.close();
}
}
Msg in console:
SIGN IN : text Found, .......continue
User NOT Successfully Signed Out.... Contact developer.
Test Failed!...
The confusing part is that why is it printing out the catch block instead of the next line in the try block?
Shouldn't it be the other way around?
Assert.assertTrue("Message if it is false", callMethod.verifyTextPresent("SIGN IN"));
The only possible explanation is that verifyTextPresent(String value) returns false (you never actually check the value of boolean txtFound) and assertTrue fails (throwing an AssertionError which is not handled well in your catch block). To find out, replace this
log.log(value + " : text Found, .......continue");
for example with this line
log.log(value + " : text Found, ......." + txtFound);
or just print the stacktrace in catch block.
Related
I want to test my simple application with AssertJ.
I can check that text is showing like that:
frame.label(JLabelMatcher.withText(text).andShowing());
But is there any way to check that specific text not showing?
As a result, I solved this issue in such a not pretty way:
public void iDontSeeText(String text) {
try {
frame.label(JLabelMatcher.withText(Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE)).andShowing());
} catch (Exception e) {
// Control isn't found. Test complete successful
return;
}
// Control is found. Execute exception
throw new RuntimeException("Text \"" + text + "\" is found in frame.");
}
I am getting Unchecked Input for Loop Condition checkmarx issue.
I tried recommended code handling but its not working for me.
Checkmarx report's description :
Method transformPojoCommon at line 334 of
to_web/src/com/toweb/bd/TrainCategoriesBD.java gets user input from
element TC_TRAIN_CAT_NAME . This element’s value flows through the
code without being validated, and is eventually used in a loop
condition in getParentTrainTypes at line 162 of
to_web/src/com/toweb/dao/TrainCategoriesDAO.java. This constitutes an
Unchecked Input for Loop Condition.
I tried below code:
valdiateRequestInput(ESAPI.encoder().
canonicalize(request.getParameter(TOWebRequestConstants.TC_OBJID).trim()));
private String valdiateRequestInput(String currentPage) {
try {
currentPage = ESAPI.validator().getValidInput("HTTP parameter value: ", currentPage, "HTTPParameterValue", 2000, true);
} catch (Exception e1) {
log.error("failed to validate HTTP parameter value ", e1);
throw new IllegalArgumentException("failed to validate HTTP parameter value "+currentPage, e1);
}
return currentPage;
}
I have fixed these issues by using ESAPI.validator().getValidInteger() or ESAPI.validator().getValidDouble() based on the return type.
Hope this solution will help you too.
Hi all I know its a stupid question but I'm trying everything.
My game server show me a very long error which is too long for console and can see the top of it, but it is missing from my log and I can't see the details of this error.
Can somebody help me on how can I capture that error:
private void fix(L2PcInstance pl, int playerPoints)
{
try
{
IAchievement arc = Achievements.getInstance().GetAchivment(_id, pl.getAchivmentLevelbyId(_id) + 1);
if ((arc != null) && (playerPoints > arc.getNeedPoints()))
{
Achievements.getInstance().reward(pl, arc);
fix(pl, playerPoints);
}
}
catch (Exception e)
{
_log.error(getClass().getSimpleName() + ": Error in fix: " + e);
}
}
the error
the line 96 is
fix(pl, playerPoints);
but the error is missing from my log im try to capture the console to txt file from command
java myserver.jar > capturemyconsole.txt but again it's showing error in console that the txt is missing. How is this possible ? Thanks for your time and help :)
and sorry for my bad english :(
You method fix calls itself, with the same arguments, causing stack overflow...
Should add some modification to the arguments, or some more condition to the recursion...
You can catch "Error" instead of "Exception" and then log
[...] catch (Error e)
{
_log.error(getClass().getSimpleName() + ": Error in fix: " + e, e);
}
In Your current code StackOverflowException (which is not a child of Exception) is propagated without going into catch block. Remember to put additional "e" parameter to log full stacktrace.
It's not a good practice to catch Error, but it'll answer Your question.
More info: try/catch on stack overflows in java?
I am using Selenium with Java an facing the following problem.
When I use the following code:
driver.findElement(By.xpath(firstNameXPath)).sendKeys(firstName);
I am able to successfully select the appropriate locator on the page and get no errors.
However, when I pass the locator to a method to check if the locator exists first and only then send keys I always get an error (locator not being present).
This is the method I am using:
public boolean IsXPathPresent(String XPath)
{
try
{
driver.findElement(By.xpath(XPath));
System.out.println("Selector: " + XPath + " found");
return true;
} catch (Exception e)
{
System.out.println(XPath + " Selector Not Present");
return false;
}
}
I am passing firstNameXPath to the method IsXPathPresent.
For some reason my program always outputs "Selector Not Present" which means it is always executing the catch part. But why ? The selector is present on page..
Is there something wrong with my try/catch block?
Thanks
i want to put if else or switch statement which is more suitable for checking employee count before commit.where i put my if else or switch code . i want restriction on employee if count is 5 then its show message "reached maximum employee limites" otherwise allow commit.
i am new in java plz someone help me to solve this
public String cmdSave_action()
{
// my code before
{
DeptSet result;
try {
dbo.connect();
result =
dbo.execSQL("select count(*) from empmasterinfo where mainid='ORGElement' and designationid='?') "
(inputText_ORGElement.getValue() != null ?
""));
result = dbo.execSQL(sSQL);
catch (Exception e) {
System.out.println(e.getMessage());
finally
{
dbo.close();
}
return null;
}}}
// my code above
{
Global.PerformIteratorAction(this.bindings, "Commit");
AdfFacesContext afContext = AdfFacesContext.getCurrentInstance();
afContext.getProcessScope().put("EmployeeID",
Global.getCurrRowFieldValue("EmpmasterinfoViewIterator",
"Employeeid"));
if (afContext.getProcessScope().get("AddEdit").toString().equals("0"))
{
Global.PerformIteratorAction(this.bindings,
"EPR_TRANSFER_APPLICANT_INFO");
Global.PerformIteratorAction(this.bindings, "eprGenerateApPlan");
}
return null;
}}
My Error Log
Error(149,12): 'try' without 'catch' or 'finally'
Error(154,36): , expected
Error(157,34): field SQL not found in class hcm.view.backing.empprofile.EmployeeMasterInfo_Add
Error(159,11): illegal start of expression
Error(159,11): ; expected
E:\HCM\ViewController\src\hcm\view\backing\empprofile\dbo.java
Error(13,16): method does not return a value
Please close Your try catch block properly
try{
}catch(Exception e){
}finally{
}
And Read this
catch and finally are within try block
try {
//code
}
catch(Exception e) {
System.out.println(e.getMessage());
}
finally {
dbo.close();
}
Using an IDE will help you with indentation and proper formatting while writing code. e.g Eclipse.
For the first error close the try-catch blocks properly
And for the second error: Since your method is declared as public String cmdSave_action(), you should return a String value at the end of the method. The return statement is missing in your code.