如何使用appium处理已安装的应用程序?

时间:2016-07-18 09:10:25

标签: java android appium

前提条件:已经通过google play在设备中安装了应用程序。

当我尝试在设备中自动安装已安装的应用程序时,每次打开谷歌浏览器而不是打开应用程序时。

任何人都可以让我知道如何打开应用程序,我是一个新的appium。

以下是代码:

public class appium {
private static AndroidDriver driver;
    public static void main(String[] args) throws InterruptedException, IOException {

        File appDir = new File("D:/setup/android-sdk_r24.4.1-windows/android-sdk-windows");
        File app = new File(appDir, "ab.java.programming-1.apk");
        System.out.println("app Dir.--->"+app);     

        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Name of mobile web browser to automate. It should be an empty string, as we are automation an app
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        // Name of the OS: Android, iOS or FirefoxOS
        capabilities.setCapability("platformName", "Android");
        // Device name:  – I am using Galaxy
        capabilities.setCapability("deviceName", "Galaxy");
        // Mobile OS version –  My device is running Android 4.4.4
        capabilities.setCapability("platformVersion", "4.4.4");
        // An absolute local path to the APK file
        //capabilities.setCapability("app", app.getCanonicalPath());

        // Java package of the tested Android app
        capabilities.setCapability("appPackage", "ab.java.programming");

        // An activity name for the Android activity you want to run from your package.
        capabilities.setCapability("appActivity", ".MainActivity");

//       Constructor to initialize driver object with new Url and Capabilities
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

        driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
        Thread.sleep(10000);
        driver.quit();

    }

    }

1 个答案:

答案 0 :(得分:0)

您需要设置指向apk所在位置的功能。

capabilities.setCapability("app", app.getAbsolutePath());

请确保在appium窗口中CHECK NO RESET选项,以便它不会重新安装apk。

编辑:

以下是我在桌面上运行应用的方式。

        File app = new File("app location which is in the desktop"); //install
        DesiredCapabilities capabilities = new DesiredCapabilities(); 

        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName", "LenovoB6000-HV");
        capabilities.setCapability("platformVersion", "5.0.2");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "package name");
        capabilities.setCapability("appActivity", "Activity name");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        Thread.sleep(10000);//wait before closing the app
            driver.quit();