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;
}
Related
In my little e-mail client I try to implement a feature to move an ImapFolder to another ImapFolder so that the first one is the secound one's subfolder.
Because I was not able to find any method which realizes that, I tried to implement the feature on my own like this:
public static boolean moveFolder(Folder move, Folder parent) {
Folder newFolder = FolderUtils.createFolder(parent, move.getName());
if(move == null || newFolder == null)
return false;
if(!(move instanceof IMAPFolder))
return false;
if(FolderUtils.openFolder(move))
try {
((IMAPFolder) move).moveMessages(move.getMessages(), newFolder);
FolderUtils.deleteFolder(move);
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
public static boolean openFolder(Folder folder) {
if(folder == null)
return true;
if(!folder.getStore().isConnected())
return false;
if(!folder.isOpen()) {
try {
if(folder.getType() == Folder.HOLDS_FOLDERS)
return false;
} catch (MessagingException e1) {
}
try {
folder.open(Folder.READ_WRITE);
return true;
} catch (MessagingException e) {
return false;
}
}else
return true;
}
public static boolean deleteFolder(Folder folder) {
try {
if(folder != null){
if(folder.isOpen())
folder.close();
folder.delete(true);
return true;
}
} catch (Exception e) {
}
return false;
}
public static Folder createFolder(Folder parent, String name) {
try {
if(parent != null) {
Folder nF = parent.getFolder(name);
if(!nF.exists()) {
boolean result = nF.create(Folder.HOLDS_FOLDERS + Folder.HOLDS_MESSAGES);
if(result) {
nF.setSubscribed(true);
return nF;
}
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
My problem is that I allways get the following exception when I try to create a new Folder in a subfolder of my inbox:
javax.mail.MessagingException: Unsupported type;
nested exception is:
com.sun.mail.iap.ProtocolException: Unsupported type
at com.sun.mail.imap.IMAPFolder.doCommandIgnoreFailure(IMAPFolder.java:3898)
at com.sun.mail.imap.IMAPFolder.create(IMAPFolder.java:810)
at de.cpu.outlook.utils.FolderUtils.createFolder(FolderUtils.java:33)
at de.cpu.outlook.utils.FolderUtils.moveFolder(FolderUtils.java:63)
at de.cpu.outlook.gui.toolbars.utils.MoveFolderScreen$1$1.run(MoveFolderScreen.java:90)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: com.sun.mail.iap.ProtocolException: Unsupported type
at com.sun.mail.imap.IMAPFolder$6.doCommand(IMAPFolder.java:831)
at com.sun.mail.imap.IMAPFolder.doProtocolCommand(IMAPFolder.java:3921)
at com.sun.mail.imap.IMAPFolder.doCommandIgnoreFailure(IMAPFolder.java:3891)
... 5 more
Is there any way to fix this problem?
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).
How do you make an if statement check if something is null, and will return null if the if statement is true?
public String getMiddle(String word)
{
// I don't know if 'is null' or 'return null' are actually things
if (is null){
return null;
}
}
Also, what would be an example of an input that would make it null?
As Aominè and litelite both said(in the same way at the same time), you can just do this:
if(word == null) return null;
And your second question:
Also, what would be an example of an input that would make it null?
If you call the method like getMiddle(null), then that would be possible.
Here are a bunch of ways to check if a word is null:
1:
private static void getWord(String str){
try {
if(!str.equals("")) {
System.out.println(str);
}
} catch (NullPointerException e) {
System.out.println("Your word was null.");
}
}
2:
private static void getWord(String str){
try {
if(!str.equals(null)) {
System.out.println(str);
}
} catch (NullPointerException e) {
System.out.println("Your word was null.");
}
}
3:
private static void getWord(String str){
try {
if(str != null) {
System.out.println(str);
}
} catch (NullPointerException e) {
System.out.println("Your word was null.");
}
}
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.
I have this code:
private String Style(String Arg, Vector VctrClass) throws Exception {
if (Verify that Arg is contained into VctrClass)) {
return "Something";
} else {
throw new Exception("Error The argument required \""+Arg+"\" doesn't exist<br>");
}
}
Here my problem, I had this method:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
finally {
return "";
}
}
But' I have this message:
Void methods cannot return a value
Then I changed my method to:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
I have this message:
This method must return a result of type String
Add the return after the println, not in the finally:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
return "";
}
}
Add the return after the catch instead of in the finally:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
return "";
}