1

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);

    }
    }
 }
2
  • I believe you should be able to use Runtime environment in java to execute adb commands Commented Jan 19, 2018 at 12:04
  • what have you done by now? Paste your code Commented Jan 19, 2018 at 12:08

1 Answer 1

1

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;

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.