在 java 中使用 selenium 运行 adb 命令 [英] Running adb command using selenium in java

查看:22
本文介绍了在 java 中使用 selenium 运行 adb 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 selenium 中使用 Java 执行 ADB(android debug bridge) 命令.后来我需要使用我的 Junit 测试在 AWS(亚马逊网络服务)设备场上执行脚本.怎么做 ?请在下面找到我的 Junit 测试用例,在这段代码中,我模拟了测试用例,我想获取应用程序的内存和 CPU 使用率

I want to execute the ADB(android debug bridge) command in selenium using Java. Later I need to use my Junit test for executing a script on AWS(Amazon web service) device farm. How to do it ? Please find my Junit test case below,In this code i had simulated the test case, I want to get the memory and CPU utilization for the application

import io.appium.java_client.android.AndroidDriver;

public class macdjtest 
 {
    static AndroidDriver driver;
    Dimension size;
    int count=0;

   @Before
    public void setUp() throws Exception {
        System.out.println("capabilities creation start.........");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //capabilities.setCapability("deviceName", "9885f6305856543937");
        capabilities.setCapability(CapabilityType.VERSION,"7.0");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage","com.androidappname.app");
        capabilities.setCapability("appActivity","com.androidappname.app.views.activities.SplashScreenActivity");
        capabilities.setCapability("newCommandTimeout", "450000");
        capabilities.setCapability("gpsEnabled",true);
        System.out.println("android driver loading start.........");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

    @After
    public void tearDown() throws Exception {


        driver.quit();

        //driver.quit();

    }

    @Test
    public void test() throws IOException, Exception {

        System.out.println("Inside the test method");

        size = driver.manage().window().getSize();
        int startx = (int) (size.width * 0.85);
        int endx = (int) (size.width * 0.20);
        int starty = size.height / 2;
        System.out.println(size);
        WebDriverWait wait = new WebDriverWait(driver, 240);


        for(int i=1;i<=10;i=i+1)
        {

        driver.resetApp();  

        System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date())+" Startsession");

        //Selecting 
        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/sit_url")));
        //com.androidappname.app:id/sit_url
        driver.findElementById("com.androidappname.app:id/sit_url").click();

        //System.out.println("Enviorment selected");

        //wait.until(ExpectedConditions.elementToBeClickable(By.id("com.android.packageinstaller:id/permission_allow_button")));
        //com.android.packageinstaller:id/permission_allow_button

        //driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click();

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/txt_title")));
        //clicking on the next 3 times
        System.out.println("Find the text next");
        for(int i1=1;i1<=3;i1++)
        {   
        driver.findElementById("com.androidappname.app:id/txt_next").click();
        }
        System.out.println("Explore as guest");
        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/txt_guest")));

        Thread.sleep(500);

        driver.findElementById("com.androidappname.app:id/txt_guest").click();

        System.out.println("Access Granting for the location");

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.android.packageinstaller:id/permission_allow_button")));

        driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click();


        //com.androidappname.app:id/welcomeText

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/ivYasHeader")));

        System.out.println("Home Page Screen");

        for(int j=1;j<=4;j++)
        {
        System.out.println("Swipe");

        driver.swipe(startx, starty, endx, starty, 3000);
        Thread.sleep(500);
        }

        Thread.sleep(500);

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/ivYasHeader")));

        driver.findElementById("com.androidappname.app:id/footerYasImage").click();

        Thread.sleep(500);

        wait.until(ExpectedConditions.elementToBeClickable(By.id("android:id/button2")));

        driver.findElementById("android:id/button2").click();
        //android:id/button2
        //sign up option declined


        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/whats_on_tv")));

        //com.androidappname.app:id/navigation_whatson


        driver.findElementById("com.androidappname.app:id/navigation_whatson").click();
        Thread.sleep(500);

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/whats_on_tv")));

        //com.androidappname.app:id/navigation_places

        Thread.sleep(500);

        //driver.findElementById("com.androidappname.app:id/navigation_places").click();



        //com.androidappname.app:id/text_places_expanded

        //wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/text_places_expanded")));

        //com.androidappname.app:id/navigation_yas_guide

        driver.findElementById("com.androidappname.app:id/navigation_yas_guide").click();

        Thread.sleep(500);

        //com.androidappname.app:id/image_left_menu_expanded

        wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/icon_menu")));


        driver.findElementById("com.androidappname.app:id/icon_menu").click();

        Thread.sleep(500);

        System.out.println("Clicking on menu Icon");

        System.out.print(count);

    }
    }
 }

推荐答案

以下是我如何使用 adb 获取连接设备列表的示例.未显示的是 adpPath 变量的分配 - 这取决于您.希望这会有所帮助.

Here's an example of how I use adb to get a list of attached devices. Not shown is the assignment of the adpPath variable - that's up to you. Hope this helps.

/**
     * Determine already connected physical devices or already running emulators
     * @author Bill Hileman
     * @return String List of running/connected devices
     */
    public List<String> getRunningDevicesList() {

        List<String> dev = new ArrayList<String>();

        try {

            String[] commandListAVDs = new String[]{adbPath, "devices"};
            System.out.println("Executing command: " + adbPath + " devices");
            Process process = new ProcessBuilder(commandListAVDs).start();

            BufferedReader inputStream = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line = null;

            while ((line = inputStream.readLine()) != null)
                //ignore lines that do not contain device information
                if (!(line.trim().equals("") || line.equals("List of devices attached") || line.startsWith("* daemon ")))
                    dev.add(line.trim());

        } catch (Exception e) {
            System.err.println("Unable to read device list: " + e);
            e.printStackTrace();
        }

        System.out.println("Running Devices: " + dev.toString());

        return dev;

    }

这篇关于在 java 中使用 selenium 运行 adb 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆