package lecturer;

import java.lang.*;
import java.nio.Buffer;
import java.util.Random;
import java.io.File;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import android.R.string;
import android.os.RemoteException;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiObjectNotFoundException; public class Lecturer extends UiAutomatorTestCase {
public String Random_text(){ //生成随机手机字符串方法
String a[] = {"a","b","c","d","e","f","g","h","w","j","k","l","n","m","o","p","q","r","s","t","u","v","w","x","Y","z"};
String A[] = {"A","B","C","D","E","F","G","H","W","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String symbol[] = {"!","@","#","$","%","^","&","*","(",")","+","=","-",";","'","?","~","|","[","]","{","}","`"};
String digital[] = {"1","2","3","4","5","6","7","8","9","0"};
String set[][] = {a,A,symbol,digital};
Random length = new Random();
StringBuffer buffer = new StringBuffer();
int length_a = length.nextInt(21)+1;
for(int i=0;i<length_a;i++){
Random random = new Random();
Random random2 = new Random();
int select = random.nextInt(4);
if (set[select].equals(a)){
buffer.append(set[select][random2.nextInt(26)]);
}
else if(set[select].equals(A)){
buffer.append(set[select][random2.nextInt(26)]);
}
else if(set[select].equals(symbol)){
buffer.append(set[select][random2.nextInt(23)]);
}
else if(set[select].equals(digital)){
buffer.append(set[select][random2.nextInt(10)]);
}
}
return buffer.toString();
}
public String Random_Value(){ //生成随机数值的方法
String Value[] = {"1","2","3","4","5","6","7","8","9","0"};
StringBuffer Value_buffe =new StringBuffer();
for(int j=0; j<12;j++){
Random Value_Random = new Random();
Value_buffe.append(Value[Value_Random.nextInt(10)]);
}
return Value_buffe.toString();
}
public void Screenshot(String name) { //截取当前屏幕图片的方法
File file = new File("/mnt/sdcard/log"+name+".png");
UiDevice.getInstance().takeScreenshot(file);
}
public void startapp(String ojb){ //启动apk的方法
StringBuffer app = new StringBuffer();
app.append("am start -n");
app.append(ojb);
try{
Process process = Runtime.getRuntime().exec(app.toString());
process.waitFor();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("NG! Start apk failed....");
}
}
public void test_lecturer()throws UiObjectNotFoundException,RemoteException {
for(int k=0;k<20;k++){
try{
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().sleep();
sleep(2000);
UiDevice.getInstance().wakeUp();
}catch(RemoteException e1) {
e1.printStackTrace();
System.out.println("NO:"+k+"....wakeup Exception!.... ");
sleep(2000);
UiDevice.getInstance().sleep();
sleep(2000);
UiDevice.getInstance().wakeUp();
}
try{
UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
.className("android.widget.FrameLayout"));
assertTrue("Unlock fail!...", Unlock.exists());
Unlock.swipeUp(20);//滑屏解锁
sleep(1000);
}catch(UiObjectNotFoundException e2){
e2.printStackTrace();
System.out.println("NO:"+k+"....Unlock Exception!.... ");
Screenshot("Unlock_Exception"+k);
sleep(2000);
UiDevice.getInstance().sleep();
sleep(2000);
UiDevice.getInstance().wakeUp();
sleep(2000);
UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
.className("android.widget.FrameLayout"));
assertTrue("Unlock fail!...", Unlock.exists());
Unlock.swipeUp(20);//滑屏解锁
}
startapp("com.android.contacts/com.android.contacts.activities.PeopleActivity");//启动联系人
sleep(2000);
try{
UiObject new_contact = new UiObject(new UiSelector().resourceId("com.android.contacts:id/floating_action_button")
.description("添加新联系人"));
assertTrue("new_contact fail!...",new_contact.exists());
new_contact.clickAndWaitForNewWindow();
}catch (UiObjectNotFoundException e3) {
e3.printStackTrace();
System.out.println("NO:"+k+"....new_contact Exception!.... ");
Screenshot("new_contact"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
try{
UiObject Name = new UiObject(new UiSelector().text("姓名").className("android.widget.EditText"));
assertTrue("Name fail!...", Name.exists());
Name.click();
Name.setText(Random_text());
sleep(1000);
}catch (UiObjectNotFoundException e4) {
e4.printStackTrace();
System.out.println("NO:"+k+"....Enter_the_contact_name Exception!.... ");
Screenshot("Enter_the_contact_name"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
try{
UiObject Pinyin_of_name = new UiObject(new UiSelector().text("姓名拼音").className("android.widget.EditText"));
assertTrue("Pinyin_of_name fail!...",Pinyin_of_name.exists());
Pinyin_of_name.click();
Pinyin_of_name.setText(Random_text());
sleep(1000);
}catch (UiObjectNotFoundException e5) {
e5.printStackTrace();
System.out.println("NO:"+k+"....Enter the name Pinyin Exception!.... ");
Screenshot("Pinyin_of_name"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
try{
UiObject nickname = new UiObject(new UiSelector().text("昵称").className("android.widget.EditText"));
assertTrue("nickname fail!...",nickname.exists());
nickname.click();
nickname.setText(Random_text());
sleep(1000);
}catch (UiObjectNotFoundException e6) {
e6.printStackTrace();
System.out.println("NO:"+k+"....Enter the nickname Exception!.... ");
Screenshot("nickname"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
try{
UiScrollable Scroll_contact = new UiScrollable(new UiSelector().resourceId("com.android.contacts:id/contact_editor_fragment")
.className("android.widget.ScrollView")); //定位滚动元素
assertTrue("Scroll_contact fail!...",Scroll_contact.exists());
UiObject number = new UiObject(new UiSelector().text("电话").className("android.widget.EditText"));
Scroll_contact.scrollIntoView(number);
sleep(1000);
number.click();
number.setText(Random_Value());
sleep(1000);
}catch(UiObjectNotFoundException e7){
e7.printStackTrace();
System.out.println("NO:"+k+"....Scroll and enter the number Exception!.... ");
Screenshot("number"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
try{
UiObject determine = new UiObject(new UiSelector().resourceId("com.android.contacts:id/menu_done")
.className("android.widget.TextView"));
assertTrue("determine fail! ...",determine.exists());
determine.clickAndWaitForNewWindow();
}catch (UiObjectNotFoundException e8) {
e8.printStackTrace();
System.out.println("NO:"+k+"....determine Exception!.... ");
Screenshot("determine"+k);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
sleep(1000);
Screenshot("Screenshot_contacts"+k);
sleep(2000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
UiDevice.getInstance().pressBack();
sleep(1000);
}
} }
05-03 22:57