1

I I am automating application with selenium Appium Selenium later version Appium 1.9.1

Whenever application launch always permission pop up showing successfully I can click on permission using automation but after giving permission appium is not able to find elements in the application .

If I click manually in the application, for example, I click on the next button and again click on the back button then appium code is working fine.

Can anyone help me how i can run my script without doing manual interaction

Page class 

public class DriverSignUpPages {

    AndroidDriver<MobileElement> driver;
    public static final String packageName = "co.wapanda.android.beta";

    public DriverSignUpPages(WebDriver driver){
        this.driver = (AndroidDriver<MobileElement>)driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    //-Storing element for pop up related access or permission 
    @AndroidFindBy(id = "com.android.packageinstaller:id/permission_allow_button")
    MobileElement permAlert;

    public void allowAccessLocation() {
        if(permAlert.isDisplayed()) {
            permAlert.click();
        } else {

        }
    }

    //-Store signUp locator and performed click action 
    @AndroidFindBy(id = packageName+":id/btn_signup")
    MobileElement signUpButton;

    public void clickOnSignUpButton() {
        signUpButton.click();
    }

    //-Store driversignup locator value and performed click action
    @AndroidFindBy(id = packageName+":id/btn_driver")
    MobileElement driverSignUp;

    public void clickOnDriverSignUp() {
        driverSignUp.click();
    }




------------------------------

Test case class 

public class ValidateDriverProcess {

    AppLaunch applaunch;
    DriverSignUpPages dprocess;
    AppiumDriver<MobileElement> driver;

    @BeforeMethod
    public void launchApplication( ) {
        applaunch = new AppLaunch();
        driver = (AppiumDriver<MobileElement>) applaunch.launchApplication();
    }

     @Test
    public void validateDriverSignupProcess() {
        try {
         DriverSignUpPages dprocess = new DriverSignUpPages(driver);
         Thread.sleep(5000);
         dprocess.allowAccessLocation();
         Thread.sleep(5000);
         dprocess.clickOnSignUpButton();
         Thread.sleep(5000);
         dprocess.clickOnDriverSignUp();
         Thread.sleep(5000);
         dprocess.enterSignUpDetail("test@abc.com", "9898989891", "123456");
         Thread.sleep(5000);
         dprocess.enterFirstAndLastNameInSignUp("Raj", "");
         Thread.sleep(5000);
         dprocess.enterVerificationCode("757575");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @AfterMethod
    public void closeApplication() {
        driver.quit();
    }


} 



------------

In appium log error showing

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result
{"status":7,"value":"No element found"}

not able to find this line 
//-Store signUp locator and performed click action 
 @AndroidFindBy(id = packageName+":id/btn_signup")
 MobileElement signUpButton;
Tayyab Amin
  • 686
  • 10
  • 28
Tarun Dabbs
  • 67
  • 13
  • did u tried autoGrantPermissions=true in capabilities ? – Amit Jain Oct 10 '18 at 10:57
  • @AmitJain No i didn't try with autoGrantPermissions= true because we need to handle positive and negative both condition with permission alert . If i performed action manually then automation code is working fine . – Tarun Dabbs Oct 10 '18 at 11:05
  • @AmitJain For testing purpose I added autoGrandPermissions= true but again the same issue coming. Not able to find the locator – Tarun Dabbs Oct 10 '18 at 11:10
  • See the three offered solutions (one of which @AmitJain suggested here) in response to [this question](https://stackoverflow.com/questions/44412211/after-dismissing-the-alert-appium-is-no-longer-detecting-any-element-on-the-scre) – Bill Hileman Oct 10 '18 at 13:55
  • @BillHileman Thanks for sharing this information. But still i am getting same issue. In my application first pop up open so I can't get coordinates of the pages.First I need to click on allow pop up then i can use application . I have also used . driver.runAppInBackground(Duration.ofSeconds(3)); but still same issue coming . Can you please suggest any other way – Tarun Dabbs Oct 11 '18 at 05:08
  • org.openqa.selenium.NoSuchElementException: Can't locate an element by this strategy: By.chained({By.id: co.wapanda.android.beta:id/btn_signup}) at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:126) – Tarun Dabbs Oct 11 '18 at 05:46
  • @TarunDabbs - Did u check if its a webview or native app ? if webview then setting right context may help in identifying elements – Amit Jain Oct 11 '18 at 06:57
  • @TarunDabbs This issue with Android version 7, your code will work for android 6 device. – shiv Oct 11 '18 at 09:31
  • @AmitJain This is a native application yes I am using 7.1.1 android version. As per requirement, we need to automate application with latest android version . – Tarun Dabbs Oct 11 '18 at 09:37
  • are u using UIAutomator2 ? in this open issue it was written if we use it then issue will be resolved + try to make app in background using code like this `driver.pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH);` instead of using direct method like `runAppInBackground` – Amit Jain Oct 11 '18 at 10:06
  • For Latest version there are workarounds I have given in the answer, these are working for me. Let me know if works.. – shiv Oct 11 '18 at 11:01
  • @AmitJain Thanks for giving a solution. When first time i executed my code after adding this line driver.pressKey(new KeyEvent(AndroidKey.APP_SWITCH)); execution worked fine . But when i start execution second time then again same issue coming . driver.pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH); this is deprecated in new version . – Tarun Dabbs Oct 12 '18 at 05:12
  • @TarunDabbs - I can give you the new version of this command... did u mean app is not switched using this command ? – Amit Jain Oct 12 '18 at 07:00
  • @AmitJain Yes first time this switched and worked fine but after I have executed multiple time an app is not switching. And can you please tell me one more solution i want to capture image from camera .. I am using latest version for java-client 6 , appium 1.9.1 , selenium 3.14 , Java 1.8 – Tarun Dabbs Oct 12 '18 at 07:43

1 Answers1

0

There is an open issue with appium: After dismissing the alert, appium is no longer detecting any of the fields.

Workarounds :

You can put the app in the background and launch again. driver.runAppInBackground(5);

OR

Get coordinates (x, y) of any element on screen before opening dialog interact with dialog (e.g. select elements, close etc.) tap by coordinates (x, y) after closing dialog: new TouchAction(driver).tap(x, y).perform();

shiv
  • 497
  • 3
  • 17
  • I have checked with driver.runAppInBackground(Duration.ofSeconds(3)); and still getting same error. when we launch application then first pop up showing so i can't get coordinates before pop up – Tarun Dabbs Oct 11 '18 at 09:39
  • There is any another element on screen by Id or you can create XPath and get the element coordinates. OR First click on permission popup and then run the app in the background. When it will appear in foreground it should work – shiv Oct 11 '18 at 09:45
  • Thanks shiv for giving reply but this is not working. – Tarun Dabbs Oct 12 '18 at 07:44
  • Yes sure public void allowAccessLocation() { if(permAlert.isDisplayed()) { permAlert.click(); Utils.PauseTestExecution(2); driver.runAppInBackground(Duration.ofSeconds(3)); Utils.getScreenSizeCordinateAndClickOnScreen(driver); } else { System.out.println("Pop up not avaiable"); Utils.getScreenSizeCordinateAndClickOnScreen(driver); } } – Tarun Dabbs Oct 12 '18 at 09:39
  • If i am using driver.runAppInBackground(5) then error message showing "The method runAppInBackground(Duration) in the type InteractsWithApps is not applicable for the arguments (int)." I think this is deprecated now we need write like this driver.runAppInBackground(Duration.ofSeconds(3)); – Tarun Dabbs Oct 12 '18 at 09:50
  • Ok Try this : if (permAlert.isDisplayed()) { permAlert.click(); driver.runAppInBackground(Duration.ofSeconds(5)); } else { System.out.println("Pop up not avaiable"); } – shiv Oct 12 '18 at 10:01
  • I have already used above code driver.runAppInBackground(Duration.ofSeconds(5)); but this is still not working . If you see i shared same code in above comment but i was using 3 second and now i have checked with 5 seconds but same issue is coming – Tarun Dabbs Oct 12 '18 at 10:07
  • https://github.com/appium/appium/issues/7806 you can check this if you find anything but the same code is working for me.. – shiv Oct 12 '18 at 10:11
  • Thanks shiv i have tried if i am using android version 6 then this is working but i need to test with android 7 or above. If i am exeuting with android 7 then execution works fine only first time if run again then same code not working . – Tarun Dabbs Oct 12 '18 at 10:48
  • If you dont want te popup to appear then before you launch/ initialize the appium driver adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.WRITE_EXTERNAL_STORAGE adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.ACCESS_FINE_LOCATION – shiv Oct 12 '18 at 11:37
  • Pop up is mandatory in this application because this application is related OLA / UBER . Without pop up application will not work in many conditions . – Tarun Dabbs Oct 12 '18 at 11:48
  • Take xpath of the clickable element before clicking the element and get X and Y coordinate from that and then click on the popup , now use the x and y coordinates to click on the element (If you have any clickable element on screen) Once you will click it , then appium will be able to find elements. (Use id of any clickable element if you are not able to get xpath by viewer and then use this id to locate element – shiv Oct 12 '18 at 12:05