简体   繁体   English

Xamarin.UITest:如何暂停测试步骤

[英]Xamarin.UITest: How To Pause a Test Step

I have a UITest written in C#/Xamarin and the test is executing too quickly, causing it to fail. 我有一个用C#/ Xamarin编写的UITest,测试执行得太快,导致它失败。 I need the test to wait for an Image to appear on the screen before executing the next step. 在执行下一步之前,我需要测试等待图像出现在屏幕上。

How can I pause the Xamarin UITest and make it wait for an Image to load on the screen before continuing to the next step? 如何暂停Xamarin UITest并使其等待图像加载到屏幕上,然后再继续下一步?

In my experience, there are a couple of ways to pause or slow down Xamarin.UITest. 根据我的经验,有几种方法可以暂停或减慢Xamarin.UITest。

Best Way 最好的办法

Use the WaitForElement API. 使用WaitForElement API。

For example, here I am waiting for a Button called "imageButton". 例如,我在这里等待名为“imageButton”的Button。

app.WaitForElement(x => x.Marked("imageButton"));

The example above waits for the element "imageButton" to appear before executing another step. 上面的示例在执行另一个步骤之前等待元素“imageButton”出现。

Another Way 其他方式

In the case you don't know what element you want to interact with or you simply want to pause Xamarin.UITest, you can use Thread.Sleep : 如果您不知道要与哪个元素进行交互,或者只是想暂停Xamarin.UITest,则可以使用Thread.Sleep

Thread.Sleep(8000);

Here, I'm pausing Xamarin.UITest for 8 seconds. 在这里,我暂停了Xamarin.UITest 8秒。 It is important to note that you will need to add this library to use Thread.Sleep : 请务必注意,您需要添加此库以使用Thread.Sleep

using System.Threading;

Hope this helps! 希望这可以帮助!

The problem with "pausing" UITest “暂停”UITest的问题

You asked: 您询问:

"How can I pause the Xamarin.UITest and make it wait for an Image to load on the screen before continuing to the next step?" “我怎样才能暂停Xamarin.UITest并等待图像在屏幕上加载,然后继续下一步呢?”

It is possible to pause Xamarin.UITest using something like Thread.Sleep() for whatever length of time you require. 可以使用Thread.Sleep()类的东西暂停Xamarin.UITest,无论你需要多长时间。 However, the problem with this is that Thread.Sleep () freezes the test run inflexibly. 但是,问题在于Thread.Sleep ()以不灵活的方式冻结测试运行。 For example, if you set Thread.Sleep (10000) , then the thread would pause for exactly 10 seconds, regardless of how long your app actually takes to load the desired element. 例如,如果设置Thread.Sleep (10000) ,则线程将暂停10秒,无论您的应用程序实际加载所需元素的时间长短。

If your picture only takes 1 second to load on faster devices, then using Thread.Sleep(10000) your test will still always take at least 10 seconds. 如果您的图片只需1秒钟就可以加载更快的设备,那么使用Thread.Sleep(10000)您的测试仍然需要至少 10秒钟。 What's worse is if you use this approach and don't Thread.Sleep long enough, the test will still fail; 更糟糕的是,如果你使用这种方法并且没有Thread.Sleep足够长时间,测试仍然会失败; so you have to force your tests to run as slow as your "worst case scenario" every single time. 因此,您必须强制您的测试每次运行都与“最坏情况”一样慢。 More info: Thread.Sleep documentation 更多信息: Thread.Sleep文档

Solution by waiting for a UI element without "pausing" execution 通过等待UI元素而不“暂停”执行的解决方案

Xamarin.UITest has the IApp.WaitForElement & IApp.WaitForNoElement APIs to handle these scenarios. Xamarin.UITest具有IApp.WaitForElementIApp.WaitForNoElement API来处理这些场景。 These APIs are superior to Thread.Sleep because you can customize the condition at which it resumes beyond just waiting for a set amount of time. 这些API优于Thread.Sleep因为您可以自定义恢复的条件,而不仅仅是等待一段时间。

Example

This snippet example will wait 90 seconds for an element to appear that is either labeled or has an ID as "myButton." 此代码段示例将等待90秒,以便显示标记或ID为“myButton”的元素。

    // Wait up to 90 seconds for myButton to appear             
    app.WaitForElement(c=>c.Marked("myButton"), "Could not find myButton", new TimeSpan(0,0,0,90,0));

If the element takes only 30 seconds to load, than in this example app.WaitForElement will continue after 30 seconds, it will only throw the error "Could not find myButton" if after waiting the full 90 seconds the element is still hasn't appeared. 如果元素加载只需要30秒,那么app.WaitForElement将在30秒后继续运行,只会在等待整整90秒之后仍然没有出现该元素,因此只会抛出错误“找不到myButton” 。

Simple query using default timeouts & error messages 使用默认超时和错误消息的简单查询

You can also use either of these API calls without defining a timeout length, in which case for local UITests they will timeout after 15 seconds, or if run in Xamarin Test Cloud the will timeout after 1 minute. 您也可以使用这些API调用中的任何一个而不定义超时长度,在这种情况下,对于本地UITests,它们将在15秒后超时,或者如果在Xamarin Test Cloud中运行,则将在1分钟后超时。 Source: Xamarin.UITest "Timeouts & Waiting" guide 来源: Xamarin.UITest“超时和等待”指南

This is the simplest possible form of the above query, using the default timeouts & default error message "Timed out waiting for element...": 这是上述查询的最简单形式,使用默认超时和默认错误消息“Timed out waiting for element ...”:

    app.WaitForElement (x => x.Marked ("myButton"));

This solution works if you aren't waiting for a particular UI element and don't want to sleep the current thread: 如果您不等待特定UI元素并且不想睡眠当前线程,则此解决方案有效:

public static void WaitForTimeSpan(IApp app, TimeSpan timeSpan)
{
    try
    {
        app.WaitForElement(c => c.Text("BLAH BLAH BLAH"), "Waiting for something, killing time", timeSpan);
    }
    catch (Exception)
    {
        //do nothing we just wanted to kill some time
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM