多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Robotium的一个测试尝试-自动化测试

更新时间:2022-04-27 09:30:57 作者:多测师 浏览:124

  这几天一直在学习robotium,但是在网上找了很久除了一个测试noteslist的例子外,其他基本上没有多少资料,但是网上关于noteslist的例子在我这不知道为什么老是行不同,于是就觉得自己弄,不按照网上的提供的方法了,经过两天的摸索和研究终于在今天搞定,下面就把我的成果分享给大家:

Robotium的一个测试尝试-自动化测试

  1.启动Eclipse执行 New --> Project --> Android Project --> Create Project from existingsample --> NotePad将自带的例子导入进来.

  2.将robotium导入到刚新建的工程中

  3、在该工程中新建一个类并将从robotium官网下载的例子粘贴到刚建的类中

  4、在AndroidManifest.xml中添加如下内容:

  <uses-library android:name="android.test.runner" />

  </application>

  <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10"/>

  <instrumentation android:targetPackage="com.example.android.notepad" //要测试的包

android:name="android.test.InstrumentationTestRunner" />

  5、现在就可以运行了,下面是我的测试结果:

  6、为了熟悉这些测试代码自己尝试了修改源代码,修改够的内容如下:

package com.jayway.test;

import com.example.android.notepad.NotesList;

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

import android.test.ActivityInstrumentationTestCase2;

import android.test.suitebuilder.annotation.Smoke;

public class NotePadtest extends ActivityInstrumentationTestCase2<NotesList>{

private Solo solo;

public NotePadtest() {

super("com.example.android.notepad", NotesList.class);

}

public void setUp() throws Exception {

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

}

@Smoke

public void testAddNote() throws Exception {

solo.clickOnMenuItem("Add note");

//Assert that NoteEditor activity is opened

solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");

//In text field 0, add Note 1

solo.enterText(0, "第一个测试 ");

solo.goBack();

//Clicks on menu item

solo.clickOnMenuItem("Add note");

//In text field 0, add Note 2

solo.enterText(0, "第二个测试 ");

//Go back to first activity named "NotesList"

solo.goBackToActivity("NotesList");

//solo.goBackToActivity("NotesList");

boolean expected = true;

boolean actual = solo.searchText("第一个测试 ") && solo.searchText("第二个测试 ");

//Assert that Note 1 & Note 2 are found

assertEquals("Note 1 and/or Note 2 are not found", expected, actual);

System.out.println("添加功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);

}

@Smoke

public void testEditNote() throws Exception {

// Click on the second list line

solo.clickInList(2);

// Change orientation of activity

solo.setActivityOrientation(Solo.LANDSCAPE);

// Change title

solo.clickOnMenuItem("Edit title");

//In first text field (0), add test

solo.enterText(0, " test");

solo.goBackToActivity("NotesList");

boolean expected = true;

// (Regexp) case insensitive

boolean actual = solo.searchText("(?i).*? test");

//Assert that Note 1 test is found

assertEquals("Note 1 test is not found", expected, actual);

System.out.println("编辑功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);

}

@Smoke

public void testRemoveNote() throws Exception {

//(Regexp) case insensitive/text that contains "test"

solo.clickOnText("(?i).*?test.*");

//Delete Note 1 test

solo.clickOnMenuItem("Delete");

//Note 1 test & Note 2 should not be found

boolean expected = false;

boolean actual = solo.searchText("(?i).*?test.*");

//Assert that Note 1 test is not found

assertEquals("Note 1 Test is found", expected, actual);

solo.clickLongOnText("第二个测试");

//Clicks on Delete in the context menu

solo.clickOnText("(?i).*?Delete.*");

actual = solo.searchText("第二个测试 ");

//Assert that Note 2 is not found

assertEquals("Note 2 is found", expected, actual);

System.out.println("删除功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);

}

@Override

public void tearDown() throws Exception {

try {

//Robotium will finish all the activities that have been opened

solo.finalize();

} catch (Throwable e) {

e.printStackTrace();

}

getActivity().finish();

super.tearDown();

}

}

  以上内容为大家介绍了自动化测试中的Robotium的一个测试尝试,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部