您的位置:首页 > 其它

Robotium_易测云生成的自动化测试脚本(3)

2013-06-02 19:58 465 查看
package com.aaa.bbb_test;

import android.os.SystemClock;

import android.test.ActivityInstrumentationTestCase2;

import com.jayway.android.robotium.solo.*;

import android.app.Activity;

import android.app.Instrumentation;

import android.view.inputmethod.InputMethodManager;

import android.view.KeyEvent;

import android.widget.*;

@SuppressWarnings( "rawtypes" )

public class Test_Case_ccc_Guang extends ActivityInstrumentationTestCase2 {

private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.aaa.bbb.SplashActivity";

private static Class< ? > launchActivityClass;

static {

try {

launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);

} catch (ClassNotFoundException e) {

throw new RuntimeException(e);

}

}

private Solo solo;

protected Integer screenWidth = null;

protected Integer screenHeight = null;

private Instrumentation inst;

public Test_Case_ccc_Guang() {

super("com.aaa.bbb", launchActivityClass);

}

@Override

public void setUp() throws Exception {

solo = new Solo(getInstrumentation(), getActivity());

inst = this.getInstrumentation();

}

@Override

public void tearDown() throws Exception {

solo.finishOpenedActivities();

}

public void testRecorded() {

solo.sleep(2000);

solo.sleep(2000);

//等待 Activity "SplashActivity" 启动

assertTrue("Activity \"SplashActivity\" is not started.", solo.waitForActivity("SplashActivity", 30000));

solo.sleep(2000);

//等待 Activity "MainTabActivity" 启动

assertTrue("Activity \"MainTabActivity\" is not started.", solo.waitForActivity("MainTabActivity", 30000));

solo.sleep(2000);

//生成屏幕截图:"main.jpg"

solo.takeScreenshot("main");

solo.sleep(2000);

//验证文字 "今日精选-全部" 是否存在

assertTrue("Assert failed: 今日精选-全部", solo.searchText("今日精选-全部"));

solo.sleep(2000);

//按下 TextView "今日精选-全部".

solo.clickOnText("值得逛");

solo.sleep(2000);

//验证文字 "值得逛一逛" 是否存在

assertTrue("Assert failed:值得逛一逛", solo.searchText("值得逛"));

solo.sleep(2000);

String category[]={"衣服","周边商场","美妆画眉","美食","鞋子","家居生活","配饰","包包","箱子"};

for(int i=0;i<category.length;i++){

//打开商品分类

solo.clickOnText(category[i]);

solo.sleep(2000);

//验证文字 "值得逛-商品分类" 是否存在

System.out.println(category[i]);

assertTrue("Assert failed: 值得逛-"+category[i], solo.searchText("值得逛-"+category[i]));

solo.sleep(2000);

//按下"返回" 键

solo.sendKey(KeyEvent.KEYCODE_BACK);

solo.sleep(2000);

}

/**

//打开商品分类

solo.clickOnText("数码周边");

solo.sleep(2000);

//按下  "返回" 键

solo.sendKey(KeyEvent.KEYCODE_BACK);

solo.sleep(2000);

*/

//退出程序

for(int j=0;j<2;j++){

//按下  "返回" 键

solo.sendKey(KeyEvent.KEYCODE_BACK);

solo.sleep(2000);

}

}

protected float toX( float x ) {

return x * getScreenWidth();

}

protected float toY( float y ) {

return y * getScreenHeight();

}

protected int getScreenWidth() {

if ( screenWidth == null ) {

screenWidth = getActivity().getWindowManager().getDefaultDisplay()

.getWidth();

}

return screenWidth;

}

protected int getScreenHeight() {

if ( screenHeight == null ) {

screenHeight = getActivity().getWindowManager().getDefaultDisplay()

.getHeight();

}

return screenHeight;

}

public void enterText(int index, String text) {

solo.enterText(index, text);

hideKeyboard(solo.getEditText(index));

}

public void enterText(EditText editText, String text) {

solo.enterText(editText, text);

hideKeyboard(editText);

}

public void hideKeyboard(){

solo.getCurrentActivity().runOnUiThread(new Runnable() {

public void run()

{

solo.getCurrentActivity().getWindow().setSoftInputMode(3);

}

});

}

public void hideKeyboard(final EditText editText) {

solo.getCurrentActivity().runOnUiThread(new Runnable() {

public void run()

{

InputMethodManager imm = (InputMethodManager)inst.getTargetContext().getSystemService("input_method");

imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: