Android Open Source - robo-remote Test Base






From Project

Back to project page robo-remote.

License

The source code is released under:

Copyright (c) 2012, 2013, 2014, Groupon, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provi...

If you think the Android project robo-remote listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
        Copyright (c) 2012, 2013, 2014, Groupon, Inc.
        All rights reserved./*w ww . j a  v a2 s  .  com*/

        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions
        are met:

        Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.

        Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.

        Neither the name of GROUPON nor the names of its contributors may be
        used to endorse or promote products derived from this software without
        specific prior written permission.

        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
        IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
        TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
        HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
        SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
        TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
        PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
        LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
        NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.groupon.roboremote.roboremoteclient.junit;

import com.groupon.roboremote.Constants;
import com.groupon.roboremote.roboremoteclientcommon.Utils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.rules.TestName;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import static org.junit.Assert.fail;

public class TestBase extends com.groupon.roboremote.roboremoteclient.TestBase {
    private com.groupon.roboremote.uiautomatorclient.TestBase uiAutomatorTestBase = null;

    @Rule
    public TestName name = new TestName();
    protected void onFail(FrameworkMethod m){}
    protected void onPass(FrameworkMethod m){}

    // This rule is evaluated at the end of a test run
    // If there as an AssertionError(test failure) then this takes a screenshot
    // This also calls test teardown
    @Rule
    public MethodRule mr = new MethodRule() {
        public Statement apply(final Statement base, final FrameworkMethod m, Object o) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    AssertionError error = null;

                    boolean failed = false;

                    try {
                        base.evaluate();
                    } catch (AssertionError e) {
                        // call global onFailure
                        try {
                            onFailure();
                        } catch (Exception ee) {
                            // don't want a new failure here to change the the teardown flow
                            // just swallow this error
                        }

                        failed = true;

                        error = e;
                    }

                    logger.info("Calling onFail/onPass functions");
                    if (failed)
                    {
                        // do failure stuff here
                        // for writing own fail function in custom com.groupon.roboremote.uiautomatorclient.TestBase
                        onFail(m);

                    } else {
                        // do passed stuff here
                        // for writing own pass function in custom com.groupon.roboremote.uiautomatorclient.TestBase
                        onPass(m);

                    }

                    // tear down robotium remote
                    logger.info("Calling tearDown");
                    tearDown();

                    // tear down uiautomator remote
                    if (Utils.getEnv("ROBO_UIAUTOMATOR_JAR", null) != null) {
                        logger.info("Killing UiAutomator");
                        uiAutomatorTestBase.killApp();
                        uiAutomatorTestBase.tearDown();
                        uiAutomatorTestBase = null;
                    }

                    // If there is an error to re-throw.. throw it..
                    if (error != null)
                        throw error;
                }
            };
        }
    };

    @BeforeClass
    public static void setUpApp() throws Exception {
        setAppEnvironmentVariables();
    }

    @Before
    public void setUp() throws Exception {
        setUp(false, true);
    }

    /**
     * This is the generic test setup function
     * @param relaunch - true if this is an app relaunch
     * @param clearAppData - true if you want app data cleared, false otherwise
     */
    public void setUp(Boolean relaunch, Boolean clearAppData) {
        try
        {
            // try to setup UIAutomator if it is available
            // base availability on ROBO_UIAUTOMATOR_JAR being set
            if (Utils.getEnv("ROBO_UIAUTOMATOR_JAR", null) != null) {
                logger.info("Starting UI Automator...");
                if (uiAutomatorTestBase == null) {
                    uiAutomatorTestBase = new com.groupon.roboremote.uiautomatorclient.TestBase();
                }
                uiAutomatorTestBase.setUp(getTestName());
            } else {
                logger.info("Not starting UI Automator. Please set the ROBO_UIAUTOMATOR_JAR environment variable.");
            }

            // setup robotium remote
            setUp(getTestName(), relaunch, clearAppData);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Caught exception: " + e + ", " + e.getMessage());
        }
    }

    /**
     * This should be called if the test name is needed somewhere in a junit test
     */
    public String getTestName() {
        String subname = name.getMethodName();

        String test_name = this.getClass() + "_" + subname;
        test_name = test_name.replaceFirst("class ", "");

        return test_name;
    }
}




Java Source Code List

com.groupon.roboremote.Constants.java
com.groupon.roboremote.example.helloworld.HelloWorld.java
com.groupon.roboremote.example.helloworld.TestActivity.java
com.groupon.roboremote.example.helloworld.WebviewActivity.java
com.groupon.roboremote.example.helloworld.support.TestableWebView.java
com.groupon.roboremote.example.helloworldtestrunner.Runner.java
com.groupon.roboremote.roboremoteclient.Client.java
com.groupon.roboremote.roboremoteclient.Constants.java
com.groupon.roboremote.roboremoteclient.PortSingleton.java
com.groupon.roboremote.roboremoteclient.QueryBuilder.java
com.groupon.roboremote.roboremoteclient.Solo.java
com.groupon.roboremote.roboremoteclient.TestBase.java
com.groupon.roboremote.roboremoteclient.components.Button.java
com.groupon.roboremote.roboremoteclient.components.ListView.java
com.groupon.roboremote.roboremoteclient.components.Screen.java
com.groupon.roboremote.roboremoteclient.components.Text.java
com.groupon.roboremote.roboremoteclient.junit.TestBase.java
com.groupon.roboremote.roboremoteclientcommon.Client.java
com.groupon.roboremote.roboremoteclientcommon.Constants.java
com.groupon.roboremote.roboremoteclientcommon.DebugBridge.java
com.groupon.roboremote.roboremoteclientcommon.Device.java
com.groupon.roboremote.roboremoteclientcommon.LogbackAppender.java
com.groupon.roboremote.roboremoteclientcommon.QueryBuilder.java
com.groupon.roboremote.roboremoteclientcommon.Utils.java
com.groupon.roboremote.roboremoteclientcommon.http.Get.java
com.groupon.roboremote.roboremoteclientcommon.http.Post.java
com.groupon.roboremote.roboremoteclientcommon.logging.EmSingleton.java
com.groupon.roboremote.roboremoteclientcommon.logging.EventManager.java
com.groupon.roboremote.roboremoteclientcommon.logging.LogEvent.java
com.groupon.roboremote.roboremoteclientcommon.logging.LogcatLogger.java
com.groupon.roboremote.roboremoteclientcommon.logging.TestLogger.java
com.groupon.roboremote.roboremoteserver.Commands.java
com.groupon.roboremote.roboremoteserver.Constants.java
com.groupon.roboremote.roboremoteserver.RemoteTestRunner.java
com.groupon.roboremote.roboremoteserver.RemoteTest.java
com.groupon.roboremote.roboremoteserver.RoboRemoteServer.java
com.groupon.roboremote.roboremoteserver.robotium.Solo2.java
com.groupon.roboremote.roboremoteserver.robotium.SoloSingleton.java
com.groupon.roboremote.roboremoteservercommon.Constants.java
com.groupon.roboremote.roboremoteservercommon.NanoHTTPD.java
com.groupon.roboremote.roboremoteservercommon.RemoteServer.java
com.groupon.roboremote.uiautomatorclient.Client.java
com.groupon.roboremote.uiautomatorclient.Constants.java
com.groupon.roboremote.uiautomatorclient.PortSingleton.java
com.groupon.roboremote.uiautomatorclient.QueryBuilder.java
com.groupon.roboremote.uiautomatorclient.TestBase.java
com.groupon.roboremote.uiautomatorclient.components.BaseObject.java
com.groupon.roboremote.uiautomatorclient.components.Notification.java
com.groupon.roboremote.uiautomatorclient.components.UiCollection.java
com.groupon.roboremote.uiautomatorclient.components.UiDevice.java
com.groupon.roboremote.uiautomatorclient.components.UiObject.java
com.groupon.roboremote.uiautomatorclient.components.UiScrollable.java
com.groupon.roboremote.uiautomatorclient.components.UiSelector.java
com.groupon.roboremote.uiautomatorserver.Constants.java
com.groupon.roboremote.uiautomatorserver.RemoteTest.java
com.groupon.roboremote.uiautomatorserver.UiAutomatorServer.java