For each @Test method my script runs it individually (restarts my android app)

Hi.

My test uses selenium / junit but when i run it it re- starts my android app for each test method and runs it individually…

Heres my code so far:

import io.appium.java_client.android.AndroidDriver;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class ProductionAppTest {

AndroidDriver driver;
Dimension size;
String destDir;
DateFormat dateFormat;


@Before
public void setUp() throws MalformedURLException {

    // Created object of DesiredCapabilities class.
    DesiredCapabilities capabilities = new DesiredCapabilities();

    // Set android deviceName desired capability. Set your device name.
    capabilities.setCapability("deviceName", "HT35HW914408");

    // Set BROWSER_NAME desired capability. It's Android in our case here.
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

    // Set android VERSION desired capability. Set your mobile device's OS version.
    capabilities.setCapability(CapabilityType.VERSION, "5.0.2");

    // Set android platformName desired capability. It's Android in our case here.
    capabilities.setCapability("platformName", "Android");

    // Dont reset the app for the session
    capabilities.setCapability("fullReset", "false");
    capabilities.setCapability("noReset", "true");

    // Set android appPackage desired capability.
    // Set your application's appPackage
    capabilities.setCapability("appPackage", "com.xxx.xxx");

    // Set android appActivity desired capability.
    // Set your application's appActivity.
    capabilities.setCapability("appActivity", "com.xxx.xxx.xxx");

    // Created object of RemoteWebDriver will all set capabilities.
    // Set appium server address and port number in URL string.
    // It will launch xxxx app in android device.
    driver = new AndroidDriver(new URL("http://xxx:xxx/wd/hub"), capabilities);


    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

}

@Test
public void aLoginscreens() throws InterruptedException {


    WebDriverWait wait = new WebDriverWait(driver, 10);
    //This test will focus on doing a regular login using an email
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    //take screenshot of the login screen
    WebElement loginButton = driver.findElement(By.xpath("//android.widget.Button[@text='LOGIN' and @index='0']"));
    wait.until(ExpectedConditions.visibilityOf(loginButton));
    takeScreenShot();

    //swipe
    //Get the size of screen.
    size = driver.manage().window().getSize();
    System.out.println(size);
    //Find swipe start and end point from screen's with and height.
    //Find startx point which is at right side of screen.
    int startx = (int) (size.width * 0.70);
    //Find endx point which is at left side of screen.
    int endx = (int) (size.width * 0.30);
    //Find vertical point where you want to swipe. It is in middle of screen height.
    int starty = size.height / 2;
    System.out.println("startx = " + startx + " ,endx = " + endx + " , starty = " + starty);

    //Swipe from Left to Right.

    driver.swipe(startx, starty, endx, starty, 1000);
    //Thread.sleep(2000);


}

@Test
public void bLoginWithEmail(){
    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement loginButton = driver.findElement(By.xpath("//android.widget.Button[@text='LOGIN' and @index='0']"));
    wait.until(ExpectedConditions.elementToBeClickable(loginButton));
    loginButton.click();
}

@Test
public void bLoginWithEmail(){

     Click on login  button.
    driver.findElement(By.xpath("//android.widget.Button[@text='LOGIN' and @index='0']")).click();
    login using jaylennes email
    enter email
    driver.findElement(By.xpath("//android.widget.EditText[@text='Email / Username' and @index='1']")).sendKeys("[email protected]");
    enter password
    driver.findElement(By.xpath("//android.widget.EditText[@NAF='true' and @index='1']")).sendKeys("xxx");


}


public void takeScreenShot() {
    // Set folder name to store screenshots.
    destDir = "screenshots";
    // Capture screenshot.
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    // Set date format to set It as screenshot file name.
    dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
    // Create folder under project with name "screenshots" provided to destDir.
    new File(destDir).mkdirs();
    // Set file name using current date time.
    String destFile = dateFormat.format(new Date()) + ".png";

    try {
        // Copy paste file at destination folder location
        FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
    } catch (IOException e) {
        e.printStackTrace();
    }


}

@After
public void End() {

}

}

Not really an Appium problem. The problem is that you don’t understand TestNg annotations. You are looking for @BeforeSuite or something similar. Read up more here:

http://testng.org/doc/documentation-main.html#annotations

Or if you are using jUnit, just add an…

@Before
public void setUp() throws MalformedURLException {

    if (driver == null) {

      <all your driver init code here>

    }

}

That way, it will only do it once.

thanks!! solved my problem! had to edit some annotations.