Reopening the application freshly through appium java script for Next Testcase

1.5k views Asked by At

I run appium java script successfully.But problem is ,My Application starts with sign in page.after signing in, i Could automate some test cases.But I want to come again from sign in page through script to run next test case. .How can I close the app and also from background mode and reopen applciation without re-installing app again?because testcases should not depend each other

public class AppTest {
    
    private static RemoteWebDriver driver;
      
    @BeforeClass
    public static void initSimulator() throws MalformedURLException
    {
            DesiredCapabilities capabilities = new DesiredCapabilities();

            capabilities.setCapability("platformName", "Android");            
            capabilities.setCapability("platformVersion", "5.0.1");           
            capabilities.setCapability("deviceName", "emulator-5554");                
            capabilities.setCapability("app", "D:\\adt-bundle-windows-x86_64-20140702\\sdk\\platform-tools\\Yr.apk");    
            capabilities.setCapability("app-package", "com.you.android");                   
            capabilities.setCapability("app-activity", "com.yr.sts.SplashActivity");                  
            capabilities.setCapability("app_wait_activity",".MainActivity");
            
            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
          
            System.out.println("App Launched");
    }
    @AfterClass
    public static void quitDriver() 
    {
       driver.quit();
       System.out.println("Driver has been Quit");
    }

    @Test
    public void sign_in_Click() throws InterruptedException 
    {
        WebElement sign_button = driver.findElement(By.name("Sign-in"));
        sign_button.click();
        WebElement usr = driver.findElement(By.id("com.you.android:id/et_login_email_or_mobile"));
        usr.sendKeys("[email protected]");
        Thread.sleep(2000);
        WebElement passwrd = driver.findElement(By.id("com.you.android:id/et_login_pwd"));
        passwrd.sendKeys("123456789");
        Thread.sleep(2000);
        driver.findElementByName("Sign in").click();
        Thread.sleep(5000);
        assertTrue( true );
    }
    @Test
    public void second_sign_in_Click() throws InterruptedException 
    {    
        //HERE I WANT TO REOPEN THE APPLICATION WITH SIGN IN  AFTER CLOSING APP.BECAUSE IN 1st TESTCASE , I HAVE SIGNED IN.this is a 2nd test case    
    }
}
1

There are 1 answers

3
kiedysktos On

change @Beforeclass and @AfterClass tags to @Before and @After respectively.

Right now your initSimulator() method will be called before every @Test, and quitDriver() method will be called after it. This way you will have new driver (with clear app) restarted every case.

So - as you see - in JUnit every test case is independent by default.

Why your problem occurred? From the @BeforeClass docs:

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class.

BTW, my initDriver() method looks like this, it is better way to handle quitting driver:

   public void quitDriver() {
        driver.closeApp();
        driver.quit();
        driver = null;
    }

(it is static in your case)