我有一个使用Realm db的测验应用程序。每次用户选择答案时,她都会单击一个按钮,并显示问题的新文本。 直到她到达我开始新活动的结尾并根据正确的答案显示分数时才会这样。
我应该如何开始/测试(我猜Espresso)这项活动,而不必每次所有答案时手动输入,并在每次回答后点击按钮,直到我到达最后一个?
我需要的是将一些模拟数据传递给变量并制作一个Intent,但我不知道如何在Espresso中找不到与此相关的任何内容
答案 0 :(得分:50)
答案 1 :(得分:1)
首先,请看这个问题:Android Monkey Runner
然后您可以看到这些指南:Monkey Runner
它使您可以使用Python来测试源外的Android活动。因此,您可以触发事物并进入特定活动,如下所示:
#! /usr/bin/env monkeyrunner
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from random import randint
print "get device"
device = MonkeyRunner.waitForConnection()
package = 'my.packaget'
activity = 'my.package.activity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)
#use commands like device.touch and device.drag to simulate a navigation and open my activity
#with your activity opened start your monkey test
print "start monkey test"
for i in range(1, 1000):
#here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
device.touch(randint(0, 1000), randint(0, 800), 'DOWN_AND_UP')
print "end monkey test"
保存,然后运行:monkeyrunner test.py
答案 2 :(得分:1)
private void launchApp(){
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// Wait for the app to appear
device.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}
答案 3 :(得分:0)
对于使用AndroidX进行测试的开发人员,情况有所变化。
这是一个示例UI测试用例,用于测试单击文本视图后是否打开我的预期活动。
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers.withId
import com.softway.dhananjay.tournamentapp.tournament.TournamentActivity
import org.junit.Test
class MainActivityTest {
@Test
fun tournament_activity_starts_onClick_of_textView() {
Intents.init()
val activityScenario: ActivityScenario<MainActivity> =
ActivityScenario.launch(MainActivity::class.java)
activityScenario.moveToState(Lifecycle.State.RESUMED)
onView(withId(R.id.startTextView)).perform(ViewActions.click())
intended(hasComponent(TournamentActivity::class.java.name))
Intents.release()
activityScenario.moveToState(Lifecycle.State.DESTROYED)
}
}
答案 4 :(得分:-2)
您可以使用意图使用以下代码启动拨号程序活动。
@Rule
public IntentsTestRule<DialerActivity> mActivityRule = new IntentsTestRule<>(
DialerActivity.class);
private static final String PHONE_NUMBER = "1234567890";
private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + PHONE_NUMBER);
private static String PACKAGE_ANDROID_DIALER = "com.android.phone";
static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Starting with Android Lollipop the dialer package has changed.
PACKAGE_ANDROID_DIALER = "com.android.server.telecom";
}
}
@Test public void testDialerIntent()throws Exception
{
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
onView(withId(R.id.edit_text_caller_number)).perform(typeText(PHONE_NUMBER));
onView(withId(R.id.button_call_number)).perform(click());
intended(allOf(
hasAction(Intent.ACTION_CALL),
hasData(INTENT_DATA_PHONE_NUMBER),
toPackage(PACKAGE_ANDROID_DIALER)));
}
}
有关详细说明,请参阅我的博客文章 - http://qaautomated.blogspot.in/2016/02/how-to-test-dialer-activity-with.html