现在可以通过Espresso Web使用Web Views测试应用程序。 Espresso允许进行各种Web交互,我们可以使用它们来构建测试用例。定位Web视图的UI元素有所不同,因为UIAutomator无法检测到Web UI元素,因此我们将使用Chrome检查器来定位我已经解释过的UI元素 这里.
在本文中,我将介绍常用的Web迭代,以便您可以立即开始为Web视图编写测试用例.Web视图测试用例只需要花费更多时间,因为我们需要添加显式等待时间,因为网页的加载取决于Internet连接。
影片教学-
正在测试的应用-
假设您有一个带有网页的应用程序,该网页显示一个登录屏幕,该屏幕显示标题为“ WELCOME”的页面。该应用程序还显示两个文本框,第一个输入电子邮件ID,第二个输入密码,并且有两个按钮“取消”和“提交”。单击“提交”后,页面显示“成功登录”。
测试方案
1.首先测试标题“ WELCOME”是否显示。
2.测试是否显示了两个文本框,并且还显示了两个按钮。
3.输入电子邮件地址和密码。
4.点击提交
5.显示测试成功消息。
测试案例示例-
@RunWith(AndroidJUnit4.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @LargeTest public class WebViewSample { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class); @Before public void setUp() { // enable java script for web page to perform web interactions onWebView().forceJavascriptEnabled(); } @Test public void 测试WebPage()throws Exception { // check WELCOME text is present onWebView().withElement(findElement(Locator.ID, "welcome_text"))
.check(webMatches(getText(), containsString("WELCOME")));
//Check UI elements text boxes and buttons are present on the page
onWebView().check(webContent(hasElementWithId("email_edit")));
onWebView().check(webContent(hasElementWithId("password_edit")));
onWebView().check(webContent(hasElementWithId("cancel_button")));
onWebView().check(webContent(hasElementWithId("提交_button")));
//enter email id and password
onWebView().withElement(findElement(Locator.ID,"email_edit")).perform(clearElement());
onWebView().withElement(findElement(Locator.ID,"email_edit")).perform(webKeys("[email protected]"));
onWebView().withElement(findElement(Locator.ID,"password_edit")).perform(webKeys("12345"));
//click on submit button
onWebView().withElement(findElement(Locator.ID,"提交_button")).perform(webClick());
//Explicit wait for submission
Thread.sleep(3000);
//Check for successful submission
onWebView().withElement(findElement(Locator.ID, "display_text"))
.check(webMatches(getText(), containsString("Successfully Logged In")));
}
}
我希望这篇文章可以帮助您找到测试服的代码范围:)
请在下面的评论部分中分享您的反馈,并按照质量检查自动化以获取最新的帖子更新。HappyTesting :-)
@Anuja:这是一个很棒的博客,我认为它真的很有帮助。我绝对会鼓励有兴趣学习自动化的朋友们来学习它。
回复删除我有一个与Espresso-Web库或您上面的代码示例有关的问题。
I just started using espresso-web for my mobile application(hybrid app) and when i follow the above instructions, it does not find the elements(username and password) in the first page of my app which i thought was using webview. But after further digging in to code, i found that its not a webview and its using chrome custom tab (//developer.chrome.com/multidevice/android/customtabs).
您是否曾经使用过Espresso-web来查找/验证使用chrome自定义标签的UI元素无效(移动页)?如果是的话,那么如果您可以帮助我提供任何示例代码或url /链接,那将是非常好的。
非常感谢您阅读和分享我的博客。我还没有遇到过自定义标签测试。但是,我将对此进行更多的探索,一旦获得答案,我就会将其添加到我的博客中。请点赞我的FB页面或加入google +以获取我的最新帖子更新
删除谢谢,
阿努加
嘿。我有一个混合应用程序,当我单击本机组件中的按钮时,我将重定向到登录页面(Web视图组件)。在单击本机组件中的按钮以自动执行登录步骤后,我添加了以下代码行(输入用户名,密码并单击登录按钮)。运行测试时,我看到正在加载Webview页面,但未输入用户名,密码,并失败,并出现以下错误。您能告诉我我在做什么错吗?
回复删除代码行:
=============
.......
//输入用户名,密码,然后单击登录按钮
onWebView()。forceJavascriptEnabled();
onWebView()。withElement(findElement(Locator.ID,"user")).perform(webKeys("test"));
onWebView()。withElement(findElement(Locator.ID,"passwd")).perform(webKeys("test1234"));
onWebView()。withElement(findElement(Locator.ID,"submit"))。perform(webClick());
.......
错误:
======
android.support.test.espresso.NoActivityResumedException:阶段RESUMED中没有活动。您是否忘了发起活动。 (test.getActivity()或类似名称)?
嗨,即使我在尝试访问Hybrid应用中的Webview时也遇到了同样的问题。你有什么解决办法吗?
删除嗨,阿伦..你能解决这个问题吗?
删除@Anuja,我'我试图与网络元素互动,但我没有。我需要导入一些库吗?
回复删除你能帮助我吗?
回复删除您是否有示例如何处理“模态”窗口(如弹出窗口)
回复删除