Appium checking if element is displayed - appium

I'm using Appium for Android
the following works for clicking on the element
driver.findElement(By.xpath("//*[#resource-id='com.app.android:id/prelogin_signup']")).click();
But I am trying to check if an element is on the screen and I tried the following
if (driver.findElement(By.xpath("//*[#resource-id='com.app.android:id/prelogin_signup']")).isDisplayed()) {
System.out.println("FOUND");
} else {
System.out.println("NOT FOUND!");
}
but it returns an Exception saying
INFO: HTTP Status: '405' -> incorrect JSON status mapping for 'unknown error' (500 expected)
org.openqa.selenium.WebDriverException: Method is not implemented
How can I check to see if an element is on the screen?

You can try this, hope it helps
//If the element found, do as you want
if (driver.findElements(By.xpath("//*[#resource-id='com.app.android:id/prelogin_signup']")).size() > 0) {
System.out.println("FOUND");
} else {
System.out.println("NOT FOUND!");
}

you can surround your code with try catch block.
public boolean isElementDisplayed(){
try{
return driver.findElement(By.xpath("//*[#resource-id='com.app.android:id/prelogin_signup']")).isDisplayed();
}catch(Exception e){
//System.out.println(e);
return false;
}
}
you can also make the generic function to check if element is displayed.
public boolean isElementDisplayed(MobileElement element){
try{
return element.isDisplayed();
}catch(Exception e){
//System.out.println(e);
return false;
}
}

You may also try isDisplayed().
if (demoPage.proceedButton().isDisplayed() == true) {
System.out.println("BUTTON FOUND*****");
TestUtil.click(cartPage.proceedButton(), 10);
} else {
System.out.println("PROCEED BUTTON NOT FOUND!*********");
}
Note - I am using Page Factory model. Hence, demoPage.proceedButton() is used, you may use your locator in place of this declaration.

Related

Custom Exception in Dart programming language

I created a custom exception called DepositeException and trying to access its custom exception message (errorMessage) in main method but it's throwing error. What might be the problem in following code.
void main() {
try {
depositAmount(-100);
} catch (e) {
print(e.errorMessage());
}
}
class DepositException implements Exception {
String errorMessage() {
return "you cannot enter amount less then 0";
}
}
void depositAmount(int amount) {
if (amount < 0) {
throw new DepositException();
}
}
This happen because ~~exception catching in Dart is unchecked~~ the exception type is unspecified so it will return Object (thanks to #jamesdlin for the correction).
To catch the custom exception, you need specify the type like this:
try{
depositAmount(-100);
} on DepositException catch (e){
print(e.errorMessage());
}
Reference:
How to create a custom exception and handle it in dart

Dart The function 'errorMessage' isn't defined

I am new to dart and I am learning dart from youtube. And courses that I am following are of 2018. The programs that they created in their videos are not working. I am facing the below issue in all my programs. Anyone, please guide me that why the programs show errors while the programs are running properly in their videos. Is it happening due to an update in dart? or any other reason? Please help to fix this issue. Thanks!
The function 'errorMessage' isn't defined.
Try importing the library that defines 'errorMessage', correcting the name to the name of an existing function, or defining a function named 'errorMessage'.
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} catch (e) {
print(errorMessage());
}
}
You are not calling the errorMessage() message on the exception. Another problem is that your catch is set to handle all types of exceptions. Since Exception does not have the errorMessage() method, you cannot call it.
You should therefore specify the type of exception you want to catch which will allow you to call the errorMessage() method on the catched exception:
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} on CustomException catch (e) {
print(e.errorMessage());
}
}

is this possible to print the web page in android without popup?

I am using default android printer option (android version 4.4)
I want to byPass printManager adapter popup. how to hide the popup and give a direct print to the printer in android
you cannot extend PrintManager class. It is a final class .Please check below link
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/print/PrintManager.java
These guys have designed their own printing framework.Where they are designing their own dialog.Have a look
http://apf.isb-vietnam.com/index.php/programming-with-apf.html
It seems to me like the answer is yes.
Here is the print() method from Android's PrintManager class:
public PrintJob print(String printJobName, PrintDocumentAdapter documentAdapter, PrintAttributes attributes)
{
if (mService == null)
{
Log.w(LOG_TAG, "Feature android.software.print not available");
return null;
}
if (!(mContext instanceof Activity))
{
throw new IllegalStateException("Can print only from an activity");
}
if (TextUtils.isEmpty(printJobName))
{
throw new IllegalArgumentException("printJobName cannot be empty");
}
if (documentAdapter == null)
{
throw new IllegalArgumentException("documentAdapter cannot be null");
}
PrintDocumentAdapterDelegate delegate = new PrintDocumentAdapterDelegate((Activity) mContext, documentAdapter);
try
{
Bundle result = mService.print(printJobName, delegate, attributes, mContext.getPackageName(), mAppId, mUserId);
if (result != null)
{
PrintJobInfo printJob = result.getParcelable(EXTRA_PRINT_JOB);
IntentSender intent = result.getParcelable(EXTRA_PRINT_DIALOG_INTENT);
if (printJob == null || intent == null)
{
return null;
}
try
{
mContext.startIntentSender(intent, null, 0, 0, 0);
return new PrintJob(printJob, this);
}
catch (SendIntentException sie)
{
Log.e(LOG_TAG, "Couldn't start print job config activity.", sie);
}
}
}
catch (RemoteException re)
{
Log.e(LOG_TAG, "Error creating a print job", re);
}
return null;
}
Simply create your own class (I will pretend it is named MyPrintManager) which extends PrintManager and #Override the print() method in it. Then, use the code above, but remove this line:
mContext.startIntentSender(intent, null, 0, 0, 0);
Then, get an instance of the MyPrintManager class using the code below:
MyPrintManager printManager = (MyPrintManager) appContext.getSystemService(Context.PRINT_SERVICE);
You could try this, though I am not sure if it works, because I haven't tested it. Please reply with the result, and I will try to help you further if it didn't work.

Getting NullPointerException For Showing the status (or) alert if edifield is empty

I want to perform validations for my editfields.so I am writing the validations on ButtonField.setChangeListener method. If editField is empty and when clicking on the button i have to show that the field is empty. To show the message i tried by using both status.show() and dialog.alert() methods. But both are generating a NullPointerException. What is the problem? Can anyone help to solve this problem or are there any other solutions to this problem?
I have written my code like this:
btnencrypt = new ButtonField("Encrypt");
btnencrypt.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
//getphonenos();
System.out.println("savedPhone no are in compose encrypt:"+savedphoneno);
encryptClicked= true;
if (savedphoneno.equals("")) { **Getting the exception here....**
Dialog.alert("Please select valid contact");
} else {
if (!(savedphoneno.equals(""))) {
if (edmsg.getText().toString().trim().equals("")) {
Dialog.alert("Please enter message");
}else {
int index = savedphoneno.indexOf(",");
if (index < 0) {
encryptBTNClicked = true;
try {
base64msgString = encrypt(savedphoneno);
} catch (CryptoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
edencryptmsg.setText(base64msgString);
} else {
//encryptTV.setText("");
edencryptmsg
.setText("Sending data to multiple receipients,"
+ "can't show the encrypted msg,as it varies");
//edencryptmsg.setTextColor(Color.MAGENTA);
}
btnencrypt.setEnabled(false);
btnclear.setEnabled(false);
}
}
}
}
});
I have a useful method that I put into my StringUtils class that checks if a string is valid.
/**
* Tests if a string is a non-null, non-empty string. This can be called to
* determine if the string should be displayed, or not.
*
* #param text
* String to test.
* #return
* If <code>text</code> is <code>null</code>, returns
* <code>false</code>. <br>
* If <code>text</code> is an empty string (""), returns
* <code>false</code>. <br>
* Else returns <code>true</code>.
*/
public static boolean isNonBlankString(String text)
{
// null text -> false
if (text == null)
return false;
// empty text -> false
if ("".equals(text))
return false;
return true;
}
This will help you with Rupak's answer.

Blackberry check internet connection on device

How do I check if the Internet connection is ON or OFF on a device?
You better check using
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT);
The CoverageInfo class provides some more types of coverage to check for. See http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/CoverageInfo.html
I think there is no direct way.
You just request for a server,if there is there is no internet not avilable at that time an exception is thrown, you catch it display an alert to the user.
Some thing like below:
try {
// request http
}
catch(IOException e) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("No Internet Connectivity");
//System.exit(0);
}
});
System.out.println(e);
}
Call this method, if it returns true then you've got connection. It checks to ensure you have enough battery for internet connection, your 3G or wireless is turned on and then you have enough signal.
public synchronized static boolean checkConnection() {
boolean returnVal = true;
if (DeviceInfo.getBatteryLevel() < 6) {
returnVal = false;
}
else if (RadioInfo.getState() == RadioInfo.STATE_OFF) {
returnVal = false;
}
else if (RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE) {
returnVal = false;
}
return returnVal;
}
Here's what I use in my application, and it works just fine:
protected static boolean isOutOfServiceRange() {
return !RadioInfo.isDataServiceOperational();
}

Resources