Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Road to capture android log using java code

In this post I will show you how to capture android log using java code.
public static void captureAndroidDevicesLog() {
  try {
   Process process = Runtime.getRuntime().exec("adb logcat");

   BufferedReader reader = new BufferedReader(new InputStreamReader(
         process.getInputStream()));
   String s;
   System.out.println("*********************************************");
   System.out.println("Printing android logs");
   System.out.println("*********************************************");
   while ((s = reader.readLine()) != null) {
    System.out.println(s);
   }
   System.out.println("*********************************************");
   System.out.println("End printing android logs");
   System.out.println("*********************************************");
  } catch (IOException e) {
   e.printStackTrace();
  } 
}

Above code capture all log of android device and print in console. Yo can manupulate as per you need.

Road to capture all connected devices using java code at run time

In this post you will learn how to capture connected devices udid at run time using java code.

public static List<String> getAttachedDevicesList(){
 
  List<String> devicesID = new ArrayList<String>();
  try {
         Process process = Runtime.getRuntime().exec("adb devices");       
         BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
         String s;                
         while ((s = reader.readLine()) != null){         
          if(s.contains("device") && ! s.contains("attached")){
           String[] device = s.split("\t");
           devicesID.add(device[0]);
          }
         }  
        
     } catch (IOException e) {
         e.printStackTrace();
     }
     return devicesID;
 }


public static void main(String[] str) {
  List<String> devicesID = getAttachedDevicesList();
  for (String dvc : devicesID) {
   System.out.println(dvc);
  }
 }

When you run this you will get  all connected devices  udid.

How to read XML file data in Java || How to create object repository in selenium

As most of the commercial automation tools has feature of object repository concept, which separate application objects from automation scripts, it help us to modify object when AUT object change without going into code.  Also if we create object repository then it reuse throughout scripts from one place.

In Webdriver we can put locator (object) of web elements in to xml, properties, excel file etc at one place, and can separate from test scripts with logically divided into files.

In this post I am going to show you how to read data and put locators into xml files.

We create xml files for object locator like “Header.xml” where put all locators related to header menus, “Login.xml” for locators of login page, “Dashborad.xml” for locator of dashboard page.

How to read write data in properties file using Java

For reading and writing data into properties file I am using Properties class in this post.
Create a file name with extension .properties like I have created “File.properties” below file:

Reading Data: Below code read data from above file using key and file name as an arguments.
package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class FileReader {
   
    public String readData(String key, String fileName) {
           String value = "";
           try {

                         Properties properties = new Properties();
                         File file  = new File("File.properties");
                         if (file.exists()) {
                                             properties.load(new FileInputStream(file));
                                             value = properties.getProperty(key);
                        }
            } catch (Exception e) {
                        System.out.println(e);
            }
           return value;
    }
   
    public static void main(String []str){
                        
             FileReader fileReader = new FileReader();                           
                        
                         String name = fileReader.readData("Name", "fileName");
                         System.out.println("Name :"+name);
                         String url = fileReader.readData("URL", "fileName");
                         System.out.println("URL :"+url);
        }
}
If you run above file you will get Name and Url values and print on console