您的位置:首页 > 其它

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

2013-06-01 09:14 246 查看
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_01 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_01() {

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() {

//等待 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);

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

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

solo.sleep(2000);

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

solo.takeScreenshot("main");

solo.sleep(2000);

//按下 按钮 (ID: panelHandle)

solo.clickOnView("panelHandle");

solo.sleep(2000);

//按下 TextView "女装".

solo.clickOnText("女装");

solo.sleep(2000);

//验证文字 "今日精选-女装" 是否存在

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

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);

}

});

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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_01 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_01() {

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() {

//等待 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);

String list[]={"女装","男装","居家","母婴","鞋包","配饰","美食","数码家电","化妆品","文体","活动","水果","玩具"};

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

//按下 按钮 (ID: panelHandle)

solo.clickOnView("panelHandle");

solo.sleep(2000);

//按下 TextView "商品分类".

solo.clickOnText(list[i]);

solo.sleep(2000);

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

//验证文字 "今日精选-商品分类" 是否存在

if(list[i]!="活动"){

assertTrue("Assert failed: 今日精选-"+list[i], solo.searchText("今日精选-"+list[i]));

solo.sleep(2000);

}

else{

assertTrue("Assert failed: "+list[i], solo.searchText(list[i]));

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);

}

});

}

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