When I use UiAutomator test, I need to use UTF7 input method. I will switch to UTF7 input method before each execution, and then switch to normal input method after each execution. Due to the large number of test machines, I write a method to automatically switch to any other input method. Share code for your reference.

/** * Change the mobile phone input method to UTf7 */ public voidsetMobileInputMethodToUtf() {
		execCmdAdb("adb shell settings put secure default_input_method jp.jun_nama.test.utf7ime/.Utf7ImeService"); } /** * set other input methods */ public voidsetMobileInputMethodToOthers() {
		String name;
		List<String> lists = execCmdAndReturnResult("adb shell ime list -s");
		for (int i = 0; i < lists.size(); i++) {
			if(! lists.get(i).contains("utf7ime")) {
				name = lists.get(i);
				execCmdAdb("adb shell settings put secure default_input_method "+ name); }}}Copy the code

The execCmdAdb and execCmdAndReturnResult methods are as follows:

/** * Run CMD ** @param CMD */ public voidexecCmdAdb(String cmd) {
		output("In execution:" + cmd);
		String OSname = System.getProperty("os.name");
		try {
			Process p = null;
			if (OSname.contains("Mac")) {
				p = Runtime.getRuntime().exec(ADB_PATH + cmd);
			} else {
				p = Runtime.getRuntime().exec("cmd /c "+ cmd); } // InputStream input = p.getinputStream (); BufferedReader = new BufferedReader(new InputStreamReader(input)); // InputStreamReader is used to stream the bufferedReader and receive String line ="";
			while((line = reader.readLine()) ! = null) {// loop read system.out.println (line); / / output saveToFile (line,"runlog.log"); / / save,false} reader.close(); // Reader depends on input. Close input.close(); InputStream errorInput = p.terrorStream (); ErrorReader = new BufferedReader(new InputStreamReader(errorInput)); Inputstreamreader (String eline =)"";
			while((eline = errorReader.readLine()) ! = null) {// loop read system.out.println (eline); / / output saveToFile (eline,"runlog.log"); / / save,false} errorReader.close(); ErrorReader errorInput.close(); } catch (IOException e) { output("Performing" + cmd + "Failure!); e.printStackTrace(); }}Copy the code
@param CMD */ public List<String>execCmdAndReturnResult(String cmd) {
		output("In execution:" + cmd);
		List<String> result = new ArrayList<String>();
		String OSname = System.getProperty("os.name");
		try {
			Process p = null;
			if (OSname.contains("Mac")) {
				p = Runtime.getRuntime().exec(ADB_PATH + cmd);
			} else {
				p = Runtime.getRuntime().exec("cmd /c "+ cmd); } // InputStream input = p.getinputStream (); BufferedReader = new BufferedReader(new InputStreamReader(input)); // InputStreamReader is used to stream the bufferedReader and receive String line ="";
			while((line = reader.readLine()) ! = null) { // output(line); result.add(line); } reader.close(); // Reader depends on input. Close input.close(); } catch (IOException e) { output("Performing" + cmd + "Failure!);
			e.printStackTrace();
		}
		return result;
	}
Copy the code

Selected articles from previous issues

  1. Java one line of code to print a heart
  2. Linux performance monitoring software Netdata Chinese version
  3. Interface Test Code Coverage (JACOCO) solution sharing
  4. Performance testing framework
  5. How to perform performance tests on a Linux command line interface
  6. Graphic HTTP brain map
  7. Programming thinking for everyone
  8. Format the JSON data for output to the console
  9. How do I test a probabilistic business interface
  10. Automatically turn the Swagger document into test code
  11. Mac+ HttpClient high concurrency configuration example
  12. Httpclient handles multiple online users simultaneously

Map of official Account☢ ️~ FunTester together