One method to capture most of the try catch statements - java

In my UI java test framework I have lots of methods that are doing try catch around element actions like click, send keys etc and return true when action is successful and false when any exception occurs. I was wondering is there any smarter way to pass the method as parameter and in that one method surround that code with try catch statement. So that code could be simpler and cleaner to read (may be Java 8 functional interfaces).
public boolean expandPanel(String panelHeading) {
boolean panelFound = false;
try {
getElement(PanelHeadings.fromString(panelHeading)).click();
panelFound = true;
} catch (Exception e) {
panelFound = false;
}
return panelFound;
}
I did try creating an actions class and defining different methods for click, sendKeys etc. But is there a way if i can just have one try catch method and pass code or behaviour to that method.
public boolean expandPanel(String panelHeading) {
return actions.click(getElement(PanelHeadings.fromString(panelHeading)));
}
public class WebElementActions {
public Boolean click(WebElement element) {
try {
element.click();
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
}

You could do something like
public boolean executeSafely(Runnable action) {
try {
action.run();
return true;
} catch (Exception x) {
return false;
}
}
And then call it with return executeSafely(element::click).

Related

How to refactor a return of the same value in both catch and try?

I've been noted by Sonar that this is a smelly code. How could I fix it?
invokeFieldAccessor(property.getField(), this.instance, theValue,
new FieldAccessorHandler() {
#Override
public synchronized Object accessField(
final Field field, final Object objectInstance,
final Object value) {
try {
field.set(objectInstance, value);
} catch (Exception e) {
return null;
}
return null;
}
});
EDIT: The desired output is to return null with or without exception
Just remove the return from the catch:
try {
// ...
} catch (Exception e) {
// Fall through to the return afterwards...
}
return null;
But I suggest that it is a very bad idea to do nothing with the Exception.
It's not advisable to catch Exception, either: you should probably be catching ReflectiveOperationException.
You can use finally to do so.
try {
...
}
catch {
...
}
finally {
return null;
}
You are returning null regardless of the outcome of the try/catch block. What makes this "smelly" is that there's a return null in the catch block, whereas the try block doesn't have any.
It's probable that there's a bug here.
new FieldAccessorHandler() {
#Override
public synchronized Object accessField(
final Field field, final Object objectInstance,
final Object value) {
try {
field.set(objectInstance, value);
//Why is nothing being returned here?
} catch (Exception e) {
return null;
}
return null;
}
});
From what is visible in this code, this method should be a void method. It's setting a value, not reading one, which is why you're returning null by default anyway.
So it's likely that you're implementing the wrong interface, or, if you have to return a value, send null only after the try/catch:
new FieldAccessorHandler() {
#Override
public synchronized Object accessField(
final Field field, final Object objectInstance,
final Object value) {
try {
field.set(objectInstance, value);
} catch (Exception e) {
logger.error(e); //log the exception
}
return null;
}
});
In the catch clause you should log the exception or rethrow it. Your implementation just hides it. And besides that, returning null value is not the best approach.
Just do this:
invokeFieldAccessor(property.getField(), this.instance, theValue,
new FieldAccessorHandler() {
#Override
public synchronized void accessField(
final Field field, final Object objectInstance,
final Object value) {
try {
field.set(objectInstance, value);
} catch (Exception e) {
e.printStackTrace();
}
}
});

Assert Fails Even java.lang.AssertionError: expected [true] but found [false]

I get this error when i run it. I'm trying to run it and I changed return true and return false later. Do you know why it happens?
public static boolean elementIsPresent(MobileElement element) {
try {
element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return true;
}
return false;
}
public void checkbox() {
try {
Assert.assertTrue(elementIsPresent(this.CimonCheckBox));
Log.log(driver).info("Passes matches Cimon Name");
Assert.assertTrue(elementIsPresent(this.KurwaCheckbox));
Log.log(driver).info("Passes matches names");
} catch (Exception e) {
Assert.fail("CheckBox: " + e.getMessage());
}
}
The logic in your if statement is backwards. You're returning true if you get a NoSuchElementException and false otherwise. If you want to consider "is displayed" as "present" then I think your method should be:
public static boolean elementIsPresent(MobileElement element) {
try {
return element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
or if you simply want to return true if it's present (regardless of whether it is displayed or not) then it can be:
public static boolean elementIsPresent(MobileElement element) {
try {
element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
return true;
}

In selenium with java need reusable method to verify isElement present which may have different locators like xpath, id, class name

I want to write a reusable method to identify whether the web Element is Present or not.
This method needs to accept e different locators like xpath, id, class name.
Here is the code snipped I tried, but not worked for the line.
if(Obj.isDisplayed())
public static boolean isElementPresent(WebDriver driver, WebElement Obj)
{
boolean result = false;
try
{
// if(Obj.isDisplayed())
if(driver.findElement(By.id("username")) != null)
{
System.out.println("WEBELEMENT Username FOUND");
result = true;
}
else
{
result = false;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
Below method returns true in case element present.
public boolean isElementPresent(WebDriver driver,By locator){
if(driver.findElements(locator).size()!=0){
//Element present
return true;
}
else{
//Element not present
return false;
}
}
Example:
isElementPresent(By.id("test"));
isElementPresent(By.xpath("//test1"));
For your case solution will be Try/Catch:
public boolean isElementPresent(WebElement element) {
try {
element.getText();
return true;
} catch (NoSuchElementException e) {
return false;
}
If element not exists on the page then element.getText() will throw NosuchElementException, method will catch this exception and returns false.
Try it and let me know if it works for you.

Android / Java: how to define a code block and leave it without custom exceptions?

I seem to be stuck with a very simple task that would require GOTO statements and, in my opinion, would justify a use of those.
I have the very simple task to exit a void on different conditions. Within its code, several dozen operations are being done and most of them can fail. I test them with try {}.
Now, based on the criticality of the operation, I either need to exit immediately and do nothing else, or, I just need to interrupt control flow and jump to a final point to do some cleaning up and then exit the method.
MWE:
public void myMethod () {
try { op1(); } catch (Exception e) { return; } // Fail here: exit immediately
try { op2(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
try { op3(); } catch (Exception e) { return; } // Fail here: exit immediately
try { op4(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
try { op5(); } catch (Exception e) { cleanUpFirst(); return; } // Fail here: do Cleaning up first, then exit
// ....
}
public void cleanUpFirst() { /* do something to clean up */ }
For code readability, I'd like to a) avoid a separate function and b) do not have more than one statement within the catch block; it just blows up the code. So, in my opinion this would perfectly justify the use of a GOTO statement.
However, the only solution I came up with, given that only two outcomes are possible, is this:
public void myMethod () {
do {
try { op1(); } catch (Exception e) { return; }
try { op2(); } catch (Exception e) { break; }
try { op3(); } catch (Exception e) { return; }
try { op4(); } catch (Exception e) { break; }
try { op5(); } catch (Exception e) { break; }
// ....
} while (1==0);
/* do domething to clean up */
}
Yes, I have heard of exceptions and that is is the Java way. Is that not as overkilled as using the separate void? I do not need the specifics, I simply need a yes/no result from each operation. Is there a better way?
why not
boolean cleanupfirst = false;
try {
op1 ();
cleanupfirst = true;
op2 ();
cleanupfirst = false;
op3 ();
} catch (Exception e) {
if (cleanupfirst)
cleanup ();
return;
}
You're over-thinking it.
4 minor adjustments.
Let Opn() return a boolean for success or failure, rather than throwing an Excpetion.
Let CleanupFirst handle program termination (you can rename it to clean exit if you want). The new parameter passed to CleanExit is the System.exit code.
Use System.Exit to return a proper return code to the OS, so you can use it in scripting.
It does not seem like your program has a successful path.
if (!op1())
System.exit(1); // <- send a failed returncode to the OS.
if(!op2())
cleanExit(2);
if (!op3())
System.exit(3); // <- send a failed returncode to the OS.
if (!op4())
cleanExit(4);
if (!op5())
cleanExit(5);
cleanExit(0);
More methods for better readability:
public void myMethod() {
try {
tryOp1();
tryOp2();
...
} catch(Exception ignore) {}
}
public void tryOp1() throws Exception {
op1();
}
public void tryOp2() throws Exception {
try {
op1();
} catch (Exception e) {
cleanUp();
throw e;
}
}

Terminating thread from inside it's run method in Eclipse-RCP

I have done something like this in my code
public void doWork()
{
Job job = new Job("Job")
{
#Override
protected IStatus run(IProgressMonitor monitor) {
while (rsMemName.next()) {
Display.getDefault().syncExec(new Runnable() {
#Override
public void run() {
try {
String memId = rsMemName.getString("id");
if (doMemberTasks(memId)==false)
{
cnn.rollback();
return;
}
} catch (Exception ex) {
ex.printStackTrace();
try {
cnn.rollback();
return;
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
}
job.schedule();
}
What i want to do is exit from the whole method if doMemberTasks(memId) returns false.
But it doesn't return from the method and keep looping on ResultSet. how can i terminate the thread from the run method?
Please give any suggestions how could i achieve that.....
Thanks in advance....
This is because return will return only from the thread run method. What you can do is set a variable(flag) probably static, and check its value after the run code to put another return statement.
Yeah your best bet would be to have a flag,
boolean doWork = true;
...
while( doWork && rsMemName.next(){
...
if (doMemberTasks(memId)==false)
{
cnn.rollback();
doWork = false;
return;
}

Categories

Resources