gpt4 book ai didi

c# - Xamarin UI 测试无法点击按钮

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:17 24 4
gpt4 key购买 nike

我在 Xamarin UI 测试中检测到非常奇怪的行为。问题是 - framework 无法点击某些标记的元素。这绝对是随机问题。问题可能出现在物理设备或模拟器、Android 或 iOS 上。旧的或新的 OS 系统。我的测试场景:

app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
try
{
app.WaitForElement(x => x.Marked("Filter"));
app.Tap(x => x.Marked("Filter"));
}
catch (Exception)
{
try
{
app.Tap(x => x.Marked("Button.Home"));
app.Tap(x => x.Marked("IDTransHistory"));
}
catch (Exception)
{
try
{
app.Tap(x => x.Marked("Button.Home"));
app.Tap(x => x.Marked("IDTransHistory"));
}
catch (Exception)
{
app.Tap(x => x.Marked("Button.Home"));
app.Tap(x => x.Marked("IDTransHistory"));
}
}
}

对比日志:

[5/18/2018 12:57:16 PM Informational] ------ Run test started ------
[5/18/2018 12:57:16 PM Informational] NUnit VS Adapter 2.0.0.0 executing tests is started
...
[5/18/2018 12:57:56 PM Informational] Waiting for element matching Marked("IDTransHistory").
[5/18/2018 12:58:03 PM Informational] Using element matching Marked("IDTransHistory").
[5/18/2018 12:58:04 PM Informational] Tapping coordinates [ 532, 1114 ].
[5/18/2018 12:58:05 PM Informational] Waiting for element matching Marked("Filter").
[5/18/2018 12:58:08 PM Informational] Using element matching Marked("Filter").
[5/18/2018 12:58:08 PM Informational] Tapping coordinates [ 684, 96 ].
[5/18/2018 12:58:24 PM Informational] Error while performing Tap(Marked("Date.Start"))
Exception: System.Exception: Unable to find element. Query for Marked("Date.Start") gave no results.
at Xamarin.UITest.SharedApp.FirstWithLog[T](T[] results, ITokenContainer tokenContainer)
at Xamarin.UITest.Android.AndroidApp.<Tap>c__AnonStoreyB.<>m__0()
at Xamarin.UITest.Utils.ErrorReporting.With(Action func, Object[] args, String memberName)
[5/18/2018 12:58:24 PM Informational] Waiting for element matching Marked("Filter").
[5/18/2018 12:58:28 PM Informational] Using element matching Marked("Filter").
[5/18/2018 12:58:28 PM Informational] Tapping coordinates [ 684, 96 ].
[5/18/2018 12:58:32 PM Informational] Using element matching Marked("Date.Start").
[5/18/2018 12:58:32 PM Informational] Tapping coordinates [ 71, 298 ].
[5/18/2018 12:58:35 PM Informational] Using element matching Text("2018").
[5/18/2018 12:58:35 PM Informational] Tapping coordinates [ 139, 228 ].

好的,您可以看到标记为 Filter 的元素正在使用 -> Tapping coordinates [ 684, 96 ]。这意味着该元素存在于监视器上。但第一次这不起作用。我使用 Nunit 3.10 和 Xamarin UI Test 2.24。此问题可能出现在随机设备上 - iOS 或 Android、模拟器或物理设备。当前测试是在三星 sm a310f 上进行的。我不知道这是 Xamarin UI 测试的错误还是 Calabash。

更新:

代码-

app.ScrollDownTo(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Filter"));
app.Tap(x => x.Marked("Filter"));
app.WaitForElement(x=>x.Marked("Date.Start"));
app.Tap(x => x.Marked("Date.Start"));

日志-

[5/21/2018 3:05:00 PM Informational] Skipping installation: Already installed.
----
[5/21/2018 3:05:34 PM Informational] Scrolling down to Marked("IDTransHistory")
[5/21/2018 3:05:37 PM Informational] Using element matching Marked("IDTransHistory").
[5/21/2018 3:05:37 PM Informational] Tapping coordinates [ 533, 1114 ].
[5/21/2018 3:05:38 PM Informational] Waiting for element matching Marked("Filter").
[5/21/2018 3:05:40 PM Informational] Using element matching Marked("Filter").
[5/21/2018 3:05:40 PM Informational] Tapping coordinates [ 684, 96 ].
[5/21/2018 3:05:41 PM Informational] Waiting for element matching Marked("Date.Start").
[5/21/2018 3:05:56 PM Informational] Error while performing WaitForElement(Marked("Date.Start"), "Timed out waiting for element...", null, null, null)
Exception: System.TimeoutException: Timed out waiting for element...
at Xamarin.UITest.Shared.WaitForHelper.WaitForAny[T](Func`1 query, String timeoutMessage, Nullable`1 timeout, Nullable`1 retryFrequency, Nullable`1 postTimeout)
at Xamarin.UITest.Android.AndroidApp.<WaitForElement>c__AnonStorey31.<>m__0()
at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, String memberName)
[5/21/2018 3:05:57 PM Informational] Query for * gave 46 results.
[5/21/2018 3:05:57 PM Informational] [
{

最佳答案

UITest 测试的正常做法是在执行手势后等待某个定义的状态 - 因此我建议在每次点击后添加一个等待命令。

例如,我会将您的示例编码为:

app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Button.Home"));
app.Tap(x => x.Marked("Button.Home"));
app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));

这样,您将等待元素出现在屏幕上,然后再尝试按下它(对于某些设备,这比其他设备更重要,因为屏幕转换动画可能需要一些时间)——也可能是元素标记当它还没有准备好接收手势时作为坐标 - 所以如果上面的方法不起作用你可以尝试在 Tap 之前添加一个 Thread.Sleep 给UI 准备时间 - 这可能有助于稳定您的测试。

如果有必要,您最终会得到类似这样的测试代码:

app.WaitForElement(x => x.Marked("IDTransHistory"));
Thread.Sleep(500);
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Button.Home"));
Thread.Sleep(500);
app.Tap(x => x.Marked("Button.Home"));
app.WaitForElement(x => x.Marked("IDTransHistory"));
Thread.Sleep(500);
app.Tap(x => x.Marked("IDTransHistory"));

我建议从 500 的 sleep 开始,以 500 的增量增加它,直到测试变得稳定。

如果一切都失败了 - 我会使用 Repl 调查问题 - 可能是某些东西阻止了手势。

还值得注意的是(尽管根据您的描述,这不是您的问题),在 iOS 11 中 - Apple 对元素的方式进行了一些更改(或不) Marked 所以一些以前工作测试可能会停止工作,因为 UI 不再以相同的方式将它们识别为 Marked。解决此问题的最佳方法是再次使用 Repl 询问应用程序。

关于c# - Xamarin UI 测试无法点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50410467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com