在我之前的文章中,我们已经了解了如何使用Espresso Intent测试拨号程序的活动。我们知道有时我们的应用程序会启动应用程序拨号程序,摄像头或浏览器等外部的活动。在本文中,我们将学习如何使用Intent测试测试浏览器的活动。
正在测试的应用-
考虑您有一个带有“单击此处”按钮的应用程序。单击后,将启动Chrome浏览器并打开网址www.google.com。
测试案例方案-
我们的测试用例是单击按钮并检查浏览器活动是否已启动并打开正确的URL。
测试用例-
import android.content.Intent; import android.support.test.espresso.intent.rule.IntentsTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.FixMethodOrder; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import static android.support.test.espresso. 浓咖啡 .onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasData; import static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage; import static android.support.test.espresso.intent.matcher.UriMatchers.hasHost; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; @RunWith(AndroidJUnit4.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @LargeTest public class BrowserIntentTest { private static final String PACKAGE_NAME="com.android.chrome"; @Rule public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>( MainActivity.class); @Test public void test1()throws Exception { Thread.sleep(3000); onView(withId("Click Here")).perform(click()); Thread.sleep(1000); intended(allOf(hasData(hasHost(equalTo("www.google.com"))), hasAction(Intent.ACTION_VIEW), toPackage(PACKAGE_NAME))); } }
我希望这篇文章可以帮助您找到测试服的代码范围:)
请在下面的评论部分中分享您的反馈,并按照质量检查自动化以获取最新的帖子更新。HappyTesting :-)
此评论已被作者删除。
回复 删除这不是一个可靠的解决方案,因为不同的手机可能安装了不同的默认浏览器。例如。如果三星将三星浏览器作为默认设置,"com.sec.android.app.sbrowser"将是一个预期的包裹
回复 删除这是一个很棒的帖子。内容非常实用且富有创意。
回复 删除钦奈WordPress网站开发公司
很棒的文章。感谢你的分享!真是一个很棒的帖子。
回复 删除钦奈的项目中心
钦奈的JavaScript培训
IT的最后一年项目域
钦奈的JavaScript培训