IOSDrive becomes null and Appium TestNG tests restarts the app in between

125 views Asked by At

I am facing issues with appium test groups. I have written multiple test cases for my app and running different sets at a time.

Basically, my test flow is: Login with Google => Input Password => Logout Then for another set same flow with Login with Facebook.

Following is the code is written in AppiumTest.java file

@Test
public class AppiumTest {

    IOSDriver<MobileElement> driver = null;

    public DesiredCapabilities capabilitiesForDevice(String deviceCode) {
        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("platformName", "iOS");

        capabilities.setCapability("udid", "SOME_VALID_UDID");  // 7+
        capabilities.setCapability("platformVersion", "10.3.3");
        capabilities.setCapability("app", "PACKAGE_PATH");

        capabilities.setCapability("noReset", false);
        capabilities.setCapability("showXcodeLog", true);
        capabilities.setCapability("clearSystemFiles", false);

        ...

        return capabilities;
    }

    @BeforeSuite(groups = {"google"})  // Removing suite also does not make any effect 
    public void setup() throws MalformedURLException {
        System.out.print("Setting up driver.\n");
        DesiredCapabilities capabilities = capabilitiesForDevice("iPhone5s");

        String url = "http://0.0.0.0:4723/wd/hub";
        driver = new IOSDriver<MobileElement>(new URL(url), capabilities);
    }

    @AfterSuite(groups = {"facebook"})  
    // Removing suite also does not make any effect 
    // Between 2 tests execution this method is not being called
    public void tearDown() {
        System.out.println("AfterSuite ... QUITTING DRIVER...");
        driver.quit();
    }

    public MobileElement getElementByName(String name) {
        try {
            MobileElement theElement = (MobileElement) (new WebDriverWait(driver, 30))
                    .until(ExpectedConditions.visibilityOfElementLocated(By.name(name)));

            return theElement;
        } catch (Exception e) {
//          e.printStackTrace();
        }

        return null;
    }

    public MobileElement getElementByXPath(String xpath) {
        try {
            MobileElement theElement = (MobileElement) (new WebDriverWait(driver, 30))
                    .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));

            return theElement;
        } catch (Exception e) {
//          e.printStackTrace();
        }

        return null;
    }

    @Test(groups = {"google"}, priority = 1)
    public void loginWithGoogle() {

        if (driver == null) {
            printLog("Test driver is null.");
            try {
                setup();
            } catch (MalformedURLException e) {
//              e.printStackTrace();
            }
        }

        printLog("driver = " + driver);

        printLog("\nLoading is null and not logged in.." + System.currentTimeMillis());

        String googleIcon = "icon google";
        MobileElement gLoginElement = getElementByName(googleIcon);
        gLoginElement.click();
        printLog("\nGoogle clicked.." + System.currentTimeMillis());

        ...
        // Input Credentials for Google auth or select user from the auth list
        // Assert.assertEquals("Hi test found.", "Test", "Test1");
    }

    public void loginWithIncorrectPassword() {
        String passwordEntry = "//XCUIElementTypeApplication[@name=\"eCare Vault\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSecureTextField";
        MobileElement pwdEntryElement = getElementByXPath(passwordEntry);

        String passwordString = "12345";
        pwdEntryElement.sendKeys(passwordString);
        printLog("\n{Priority=>2} Password entered..." + passwordString + " @ " + System.currentTimeMillis());

        ...

        printLog("\nSend clicked..." + System.currentTimeMillis());

        // Assert for incorrect pwd.
    }

    @Test(groups = {"google"}, priority = 12)
    public void loginWithIncorrectPasswordGoogle() {
        loginWithIncorrectPassword();
    }

    public void loginWithCorrectPassword() {
        String passwordEntry = "//XCUIElementTypeApplication[@name=\"eCare Vault\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSecureTextField";
        MobileElement pwdEntryElement = getElementByXPath(passwordEntry);

        String passwordString = "VALID PWD";
        pwdEntryElement.sendKeys(passwordString);
        printLog("\n{Priority=>6} Password entered..." + passwordString + " @ " + System.currentTimeMillis());

        // XCUIElementTypeButton[@name="Send"]
        String sendKey = "Send";
        MobileElement sendKeyElement = getElementByName(sendKey);
        sendKeyElement.click();

        printLog("\nSend clicked..." + System.currentTimeMillis());
    }

    @Test(groups = {"google"}, priority = 16)
    public void loginWithCorrectPasswordGoogle() {
        loginWithCorrectPassword();
    }

    public void logoutButtonClicked() {
        // This method will logout user from the app and loads the main screen from where user will be able to tap the Google/Facebook icon
        System.out.println("\nLogged out from ECV..." + System.currentTimeMillis());
    }

    @Test(groups = {"google"}, priority = 19)
    public void logoutButtonClickedGoogle() {
        logoutButtonClicked();
    }

    @Test(groups = {"facebook"}, priority = 20)
    public void loginWithFB() {
        System.out.println("\nLogin with Facebook..." + System.currentTimeMillis());

        if (driver == null) {
            printLog("Test driver is null.");
            try {
                setup();
            } catch (MalformedURLException e) {
//              e.printStackTrace();
            }
        }

        printLog("driver = " + driver);

            System.out.println("\nLoading is null and not logged in.." + System.currentTimeMillis());

            String fbIcon = "icon facebook";
            MobileElement fbLoginElement = getElementByName(fbIcon);
            fbLoginElement.click();
            System.out.println("\nFacebook clicked.." + System.currentTimeMillis());

            ...
            // "Log In with the Facebook App" button exists for FB app login
            // "Log In with the Facebook App" -> Tap
            ...

            continueLoginElement.click();
            System.out.println("\nKalis loggedIn.." + System.currentTimeMillis());
        }

        System.out.println(
                "\n{Priority=>1} Password screen found.." + pwdScreen.getText() + " " + System.currentTimeMillis());
        // Assert.assertEquals("Hi test found.", "Test", "Test1");
    }

    @Test(groups = {"facebook"}, priority = 22)
    public void loginWithIncorrectPasswordFB() {
        loginWithIncorrectPassword();
    }

    @Test(groups = {"facebook"}, priority = 26)
    public void loginWithCorrectPasswordFB() {
        loginWithCorrectPassword();
    }

    @Test(groups = {"facebook"}, priority = 28)
    public void homePageLoadedFB() {
        homePageLoaded();
    }

    @Test(groups = {"facebook"}, priority = 29)
    public void logoutButtonClickedFB() {
        logoutButtonClicked();
    }

    private void printLog(String message) {
        System.out.println(message);
    }
} 

Testng.xml example

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="TestGoogle">
        <groups>
            <run>
                <include name="google" />
            </run>
        </groups>

        <classes>
            <class name="login.googleLogin.AppiumTest" />
        </classes>
    </test> <!-- Test -->
    <test name="TestFB">
        <groups>
            <run>
                <include name="facebook" />
            </run>
        </groups>

        <classes>
            <class name="login.googleLogin.AppiumTest" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

When I right click on testng.xml and run the test as TestNG Suite, the app gets restarted when the first test "TestGoogle" is completed and there after a 5-10 seconds restarts, the app continues running and executes pending test cases for "TestFacebook".

When I combine Facebook and Google, test cases for Google/Facebook are not executed.

<run>
                <include name="google" />
                <include name="facebook" />
            </run>

Please give me hint of how to solve the issue.

0

There are 0 answers