Robotium shell command for cross-process automated testing through broadcast and service +shell commands

Source: Internet
Author: User

This is the weekend when you want to put out the shell, the results, Saturday overtime, plus the class and friends out to play, Sunday and stole a lazy, the results of the weekend did not do anything, with the development of the boss said the interface test data prepared is not ready. Life Ah! By Monday because of the reason of the line, overtime until 2 o'clock in the morning this morning to get home. Life Ah! So hurry up today to write the next article, in fact, things are not many. Hurry to tidy up, if there are mistakes, please correct me.

The last blog post finished the broadcast and service, now we have a service, service inside registered a receiver, to receive our broadcast signal, as long as the receiver matched to the corresponding broadcast signal, we began to onreceive in the code. So today we are mainly about what is in OnReceive.

First imagine a scene, we wrote a test registration of the automated code, the registration process need to add the avatar-for the sake of simplicity, we only choose to take pictures-then we will jump out of the program into the camera, known as the cross-process. At this point the code in Robotium can not be executed, the approximate reason is that the app we are testing is suspended, causing Robotium to be suspended together, so the next thing can not be executed, unless back to the app. So we have a general understanding that what the shell commands to do is complete the entire photo, confirm, clip action, and then go back to the program. A description of the shell command has been posted in the previous post.

So we have two things in this one: 1. What kind of command is sent. 2. How to execute after receiving the command. These two problems are also my actual implementation of the process encountered problems, the actual completion process really embarrassed me, even a little hard to tell.

When you first need to find information on the Internet, you can find a lot of information is the use of Runtime.getruntime (). EXEC () to execute the shell command, but there is no specific example, make me very irritable, should be to use all "adb shell input Tap xxx xxx "or directly with" input tap xxx xxx ", the root of the mobile phone should not add" su ", need to add the words and when, where to add in. The answer on the Internet does not have a clear explanation, may be my limited ability to search, in short I looked for a day and a half time, did not find. As I approached the crash, it occurred to me that Xuxu's own integrated test package was also using shell commands to cross the process, and since he was using the shell as well as Java, there was bound to be the answer I wanted. Hurriedly opened, very smoothly found a piece of code Sushell, here again thanks to Xuxu, poor this code lying in my folder for more than two weeks, I just can't think of it, ashamed to the extreme. The following code is not written by me, I am just a porter.

1 Private Static voidSushell (String cmd) {2         Try {3Thread.Sleep ( -);4}Catch(interruptedexception e) {5 e.printstacktrace ();6         }7Process PS =NULL;8 dataoutputstream os;9         Try {TenPS = Runtime.getruntime (). EXEC ("su"); OneOS =NewDataOutputStream (Ps.getoutputstream ()); AOs.writebytes (cmd +"\ n"); -Os.writebytes ("exit\n"); - Os.flush (); the}Catch(IOException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -         } +}

Well, I admit that the thread pauses that part I added, because different phones start the camera speed is not the same. Well, I struggled for a day and a half, now share, we will not be so troublesome, but also a good thing (in fact, the dark of the mind), how to carry out, then only the implementation of what! Fortunately, a day and a half of the search is not what do not know right, at least I know this command is two possible, or "adb shell input tap xxx xxx" or "input tap xxx xxx", you have to say there are swipe and so on when I did not agree? My heart is already very dark! Try it all. It turns out that the latter. Nonsense not much to say, paste code.

In the test code we first write a method:

1 Private void sendbroad (string[] cmd) {2         New Intent (); 3         Intent.setaction ("Android.qwerty.action.open"); 4         Intent.putextra ("Verify_code", cmd); 5         solo.getcurrentactivity (). Sendbroadcast (intent); 6     }

Then in the test case:

1 string[] Command = {"Input tap 540 1700", "Input KeyEvent 4"}; 2 sendbroad (command);

When we received the broadcast, we left the last one to implement our click in OnReceive:

1 @Override 2          Public void OnReceive (Context context, Intent Intent) {3              for (String Command:intent.getStringArrayExtra (flag)) {4                sushell (command); 5             }6         }

Using the loop of foreach, each value in the array is taken out once. In other words, the actions you want to implement in the camera, in turn, are written into the array.

Well, here, all our code is implemented, and the cross-process can go smoothly. Of course, I wrote a small program when I was implementing cross-process, and there were a lot of problems when I actually used the company's test code. I won't say it here.

Overall, the code is very simple, but because the testing industry generally low level of development, the people will not have a few written a complete tutorial (I can be seen as appreciating it?). ), occasionally a few write also just wrote a thought roughly complete what to do. So making cross-process in the testing industry seems like a good thing. In fact, I only know this thing to their complete realization, but also only for a week, if not the middle of a day and a half of jam, more than three days to understand, if you are a bit of Android technology testing, I estimate a day or two you can finish. So this thing is awesome? Not really. Said so much do not know what he said. Just don't say haha.

Article for my original, there are many said do not understand there is not the right place, but the creation is not easy, reproduced please be sure to declare, thank you very much.

Robotium shell command for cross-process automated testing through broadcast and service +shell commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.