Micro Focus Expert
Micro Focus Expert
395 views

Protecting your MC while working with Appium

Working wiht Appium can be challening: Mobile Center invoke web driver instance on the connector machine.

But what will happend if the test is not terminated as expected or aborted? MC have a capability to automatically terminate not used instances of web driver, however it is better to eliminate such situaiton from the beginnig (relevant not only for MC, but for any Appium driven framework). The solution: addShutdownHook.

Please see the code below: the run method will be executed anytime that JVM is going down - perfect place to have your driver.quit() method to keep it clean.

package com.mf.cfrd;

public class Test {
    public static void main(String[] args) {

        Test test = new Test();
        test.attachShutDownHook();
        try {
            while (true) {
                Thread.sleep(1000);
                System.out.println("Inside Main");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Finally");
        }
    }

    public void attachShutDownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("Inside Add Shutdown Hook");
            }
        });
        System.out.println("Shut Down Hook Attached.");
    }
}

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.