Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reopening the application freshly through appium java script for Next Testcase

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    
    }
}
like image 207
senthilM Avatar asked Jun 08 '15 08:06

senthilM


People also ask

How do you write a test script in Appium?

To do this we need to have two parameters, the first is the Appium server address with the port number which it is running and the Capabilities. Hover mouse on URL and import URL from Java.net. You will get an error for complete new URL section, hover mouse on the error and select Add throws declaration.

How can I make Appium run faster?

Remove unnecessary steps/checks - every command sent to appium costs valuable time as it's a server. Cache WebElement (well, more specifically MobileElement) results if you're checking multiple things on a single screen. This would allow you to cut out redundant element search queries to Appium.


1 Answers

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)

like image 72
kiedysktos Avatar answered Nov 10 '22 18:11

kiedysktos