Sajid Zeb
Sajid Zeb

Reputation: 1978

How to write Automated Tests for an Activity other than First Activity in Android Studio for java using Appium

I am going for writing automated tests for my android app using Appium in Ubuntu linux using Android Studio IDE.

Appium is installed and configured with Android Studio. I have a MainActivity, which decides if user is logged in. Take user to Home Screen, Else take user to WelcomeActivity which have login options in it.

I wrote tests for WelcomeActivity for just button clicks and everything works fine and tests are passed after running app on emulator by Appium and Sellinium. Here is the code for ui tests of WelcomeActivity

public class WelcomeActivityUITests {
WebDriver driver;

@Before
public void setUp() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Emulator Nexus_4_API_22 Android 5.1.1, API 22");
    capabilities.setCapability(CapabilityType.PLATFORM, "Android");
    capabilities.setCapability(CapabilityType.VERSION, "5.1.1");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("appPackage", "com.my.app");
    capabilities.setCapability("appActivity", ".activities.WelcomeActivity");

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

@Test
public void testEmailLoginButton() {
    By loginButtonId = By.id(Constant.APP_PACKAGE + "email_signin_button");
    RemoteWebElement loginButton = (RemoteWebElement) driver.findElement(loginButtonId);
    loginButton.click();
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}

@After
public void End() {
    driver.quit();
}
}

This test class works fine when i run from android studio and test passes.


Now i want to write tests for another activity. It name is AnotherActivity and it comes after getting logged in.

The code for this class tests is:

public class AnotherActivityUITests {
WebDriver driver;

@Before
public void setUp() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Emulator Nexus_4_API_22 Android 5.1.1, API 22");
    capabilities.setCapability(CapabilityType.PLATFORM, "Android");
    capabilities.setCapability(CapabilityType.VERSION, "5.1.1");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("appPackage", "com.my.app");
    capabilities.setCapability("appActivity", ".activities.AnotherActivity");

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

@Test
public void test() {
    System.out.println("driver.getPageSource(): " + driver.getPageSource());

    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}

@After
public void End() {
    driver.quit();
}
}

Now when i run this test class. It gives error:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Cannot start the 'com.my.app' application. Original error: Activity name '.activities.AnotherActivity:' used to start the app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity (WARNING: The server did not provide any stacktrace information)

I don't get this. that first activity tests are running fine. Second Activity is giving problem. I also try to give other activity names like LoginActivity to test whether it runs the test or not. Find no success.

Please guide me how to achieve this. Where i am wrong.

Upvotes: 3

Views: 996

Answers (1)

Suban Dhyako
Suban Dhyako

Reputation: 2526

Instead of using appActivity for each activity, you can use MainActivity as appActivity. Then you need to check in which activity the app goes i.e welcomeActivity or AnotherActivity.

@Before
public void setUp() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Emulator Nexus_4_API_22 Android 5.1.1, API 22");
    capabilities.setCapability(CapabilityType.PLATFORM, "Android");
    capabilities.setCapability(CapabilityType.VERSION, "5.1.1");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("appPackage", "com.my.app");

    //put your main activity below-----------------------------------//
    capabilities.setCapability("appActivity", "Your main activity here");
    //---------------------------------------------------------------//

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

then you can check which activity it will goes by checking the element of your welcomeActivity or AnotherActivity is displayed.

By loginButtonId = By.id(Constant.APP_PACKAGE + "email_signin_button");
RemoteWebElement loginButton = (RemoteWebElement) driver.findElement(loginButtonId);
try{
    if(loginButton.isDisplayed()){
      //your test for welcomePage
    }
}catch(Exception e){
    //your test for Another Activity
}

Upvotes: 1

Related Questions