0

Hello I am trying to learn Appium to automate a test case on my app.

I managed to run a simple script, but I do NOT understand the logic of of the running process like android life-cycle.

What is the cycle for a testcase.

Because When I run the code below it does not run in order as of, test1, test2, test3...

How do we tell the testCase what to run first and in what order ? thanks

package appium.learning;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class LearningTest {

    WebDriver driver;

    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("device", "Android");
        capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
        capabilities.setCapability(CapabilityType.VERSION, "4.4");
        capabilities.setCapability(CapabilityType.PLATFORM, "Android");
        capabilities.setCapability("app-package", "com.myapp"); //Replace with your app's package
        capabilities.setCapability("app-activity", ".myapp"); //Replace with app's Activity
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }




    @AfterClass
    public void tearDown(){
        driver.quit();
    }


    @Test
    public void Cal1(){
        driver.findElement(By.name("1")).click();
    }
    @Test
    public void Cal2(){
        driver.findElement(By.name("2")).click();
    }
    @Test
    public void Cal3(){
        driver.findElement(By.name("3")).click();
    }
    @Test
    public void Cal4(){
        driver.findElement(By.name("4")).click();
    }

}
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • How are you running the code? Are you not using run as junit option from eclipse. In junit runner you can see all your test cases and run them all or individually. – vvvvaib May 27 '15 at 13:23
  • yes, using eclipse, right click and run as junit, – Thiago May 28 '15 at 02:07
  • Another question, why every time, it run a test, it was to install the apk file agian ? – Thiago May 28 '15 at 02:08
  • @Mr T Use can use the command: desired_caps['noReset'] = True in order to keep the app in same state instead of reinstalling – krishna chetan May 28 '15 at 06:17
  • @ krishna chetan, hiya. Can you expand you anwser, how can run multiple test case, without re install the apk everytime. thank u – Thiago May 28 '15 at 06:36
  • Check this link about the ordering of the test cases during execution. Very nicely explained. http://stackoverflow.com/questions/9528581/specifying-order-of-execution-in-junit-test-case – vvvvaib May 28 '15 at 07:15

2 Answers2

4

In your tests, it is essential to not have test dependencies, meaning that one test should not depend on the output of another test. Each test should be unique and independent on its own. Thus, the need to order your tests is eliminated.

The JVM decides at runtime at random which test will be run first. The order is random. This is because you shouldn't need to run the tests in a specific order anyways.

However, there are annotations with which you can force the JVM to run the tests in a certain order but this is highly discouraged.

As regards to your question about not uninstalling the app between each test, let me clarify this for you. So basically, each test that is annotated with @Test will close out at the end before the next test (in random order) runs.

However, just because the app is always closed and re-opened between tests doesn't mean that it is always reinstalled. There are actually two main capabilities which allow you to control this:

fullReset - (iOS) Delete the entire simulator folder. (Android) Reset app state by uninstalling app instead of clearing app data. On Android, this will also remove the app after the session is complete. ex. capabilities.setCapability("fullReset", true/false);

noReset - Don’t reset app state between sessions. (IOS: don’t delete app plist files; Android: don’t uninstall app before new session) ex. capabilities.setCapability("noReset", true/false);

Also, the @BeforeClass annotation means that that method will run once and only once before all the tests in the class and the @AfterClass is its opposite. I, however, recommend annotating the setup and tearDown methods with @Before and @After which makes the driver be setup and tear down before and after each test is run in which ever order. This will reduce your chance of error.

I hope this gives you a good idea of some basic practices and also the last paragraph should give you an idea about the life cycle as well.

Comment if you still have any questions.

Happy testing :)

qazimusab
  • 1,031
  • 1
  • 7
  • 14
  • Hello @qazimusab, thank you for your reply. very detailed, really appreciate your time. The reason why I need my test case to be in order is because, 1st i need to do login. then if successful. follow other test. when I used appium for the 1st case, it took at-lest 1 minute. So in test 2 I dont want to do the login all over again. – Thiago Jun 20 '15 at 14:22
  • So you can either login every time or just not do a reset so that the user stays logged in if you have that feature in your app. – qazimusab Jun 22 '15 at 15:50
0

Another option for you is to log in before you test 2 start: meaning merge both test cases. I had the same issue and just changing the logic of test step resolves it.

Salman Arshad
  • 59
  • 1
  • 14