Back function to go back on an Android device from Perfecto Lab

Asked

Viewed 477 times

0

Good afternoon Commonwealth,

I am using Cucumber in Eclipse to elaborate automated tests on an application installed on an Android that is on a VM, namely hosted on the site Perfecto Lab. Perfecto Lab has several functions that we can call in the eclipse, for example, return to the homescreen, make Swipe, among others. One function that I consider important that does not exist, is the function to click the Back button of Android. The tests are performed in Cucumber, but the functions are created in java. I am also working with appium if it is relevant to solving my problem.

This is the code corresponding to the test to be performed. Open the application, click a button and at the end, I would click the back button of android to go back.

@MyAxa_Spain_Login
Feature: Customer without car contracts tries to consult his car contracts

@Start_app_and_Login
Scenario: launch APP
    Given I start application by name "My AXA España" 
    Then I click on "button.aceder"
    And wait until "button.username" to be visible
    And I wait for "5" seconds
    Then I Click Back

This is my function I’m trying to implement to click the back button of android.

@QAFTestStepProvider
public class BackKey {

    @Then("^I Click Back")  
    public void clickBack() {

        //AppiumDriver<WebElement> driver;
        AndroidDriver<WebElement> driver;

        PerfectoDeviceSteps getSteps = new PerfectoDeviceSteps();

        String host = "https://atc.perfectomobile.com/nexperience/perfectomobile/wd/hub";
        //PerfectoDeviceSteps getSteps = new PerfectoDeviceSteps();

        //getSteps.goToHomeScreen();

        try {
            //DesiredCapabilities capabilities = new DesiredCapabilities("", "", Platform.ANY);
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("user", "[email protected]");
            capabilities.setCapability("password", "passfake");
            //capabilities.setCapability("deviceName", "CE12160C43D92A0704");
            driver=new AndroidDriver<WebElement>(new URL(host), capabilities);
            //((AndroidDeviceActionShortcuts) driver).pressKeyCode(AndroidKeyCode.BACK);
            ((AndroidDriver<WebElement>) driver).pressKeyCode(4);
            System.out.println("chega aqui");
            getSteps.takeScreenshot();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

What’s happening when I try to run the test, it takes all the steps without mistakes, the problem is it’s not going back. The code has commented lines, so you can see what I’ve already tested. I have a feeling I’m on the right track, I don’t know if I’m initializing the variable wrong.

The goal was to get the back button function to work.

1 answer

0

Fala Ivo,

It’s not much of a secret, but I believe you need to separate the responsibilities.

In your code you are using capabilities as the orchestrator of your tests, it should only set the capabilities and nothing else, you could have a class where you would create the methods using JUNIT.

But to answer your question the right way to tap the back button of android is :

driver.pressKeyCode(AndroidKeyCode.BACK);

Where Driver is an instance of Appium and pressKeyCode is the method available in that instance.

Correcting what you put should be like this your step_definiton:

@QAFTestStepProvider
public class BackKey {

@Then("^I Click Back")
public void clickBack() {

AndroidDriver<WebElement> driver;

try {

  driver.pressKeyCode(AndroidKeyCode.BACK);
  getSteps.takeScreenshot();
} catch (MalformedURLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.