简体   繁体   English

有AssertionException时如何截屏? (Xamarin.UITest,C#)

[英]How do I take a screenshot when there's an AssertionException? (Xamarin.UITest, C#)

I have built a Xamarin.UITest suite and I'm testing locally on a real Android device. 我已经构建了Xamarin.UITest套件,并且正在真实的Android设备上进行本地测试。 I notice that one or two tests fail, but not necessarily the same tests. 我注意到一个或两个测试失败,但不一定是相同的测试。 Whenever I try to re-run such tests to see why they're failing, they pass! 每当我尝试重新运行此类测试以查看其失败原因时,它们就会通过! Therefore I need to add functionality to see what's on the screen when a test fails. 因此,我需要添加功能以查看测试失败时屏幕上的内容。 I don't seem to be able to find a guide specifically for this - just bits and pieces... 我似乎无法找到专门针对此的指南-点点滴滴...

I've added EnableLocalScreenshots() to my StartApp() call, but I'm not sure of the next steps. 我已经将EnableLocalScreenshots()添加到了StartApp()调用中,但是我不确定接下来的步骤。 So I have some questions: 所以我有一些问题:

  1. Do I need to specify the location of where the screenshots are saved, or is this done automatically? 我需要指定屏幕快照的保存位置,还是自动完成?

  2. Do I need to explicitly write code to take a screenshot when there's an AssertionException, or is this what the screenshot functionality does by default? 在出现AssertionException时,我是否需要显式编写代码以截取屏幕快照,或者这是屏幕快照功能默认情况下的功能吗?

  3. If I need to write code to take a screenshot when there's an AssertionException, please can you point me to an example of this code so I can add it to my suite? 如果在发生AssertionException时需要编写代码以截取屏幕截图,请问能否为我提供此代码的示例,以便将其添加到我的套件中?

Thank you 谢谢

EDIT: Re: Take screenshot on test failure + exceptions 编辑:回复: 在测试失败和异常上截图

I have tried the following in my Hooks.cs file: 我在Hooks.cs文件中尝试了以下操作:

[OneTimeTearDown]
public void OneTimeTearDown()
{
    if (TestContext.CurrentContext.Result.Outcome == ResultState.Error || TestContext.CurrentContext.Result.Outcome == ResultState.Failure)
    {
        App.Screenshot(TestContext.CurrentContext.Test.Name);
    }
}

Upon debugging this method, I find that it is never called. 调试此方法后,我发现它从未被调用过。 Not even if I use TearDown and AfterScenario attributes. 即使我使用TearDown和AfterScenario属性也不行。 So great - that Intellisense likes my code, bad - because it never gets called. 非常好-Intellisense很喜欢我的代码-很糟糕-因为它从未被调用。 It shouldn't be this hard! 不应该那么难!

I am using Specflow in this suite, could that be anything to do with why I'm getting this issue? 我在此套件中使用Specflow,这与为什么我遇到此问题有关吗? This is why I can't implement the solution on the above thread as follows because Specflow manages the NUnit tests... 这就是为什么我无法在上述线程上实现解决方案的原因,因为Specflow管理着NUnit测试...

UITest(() =>
{
    // Do your test steps here, including asserts etc.
    // Any exceptions will be caught by the base class
    // and screenshots will be taken
});

Ok so for me this was the answer. 好的,对我来说,这就是答案。

Added [Binding] attribute above class (dumb error on my part)... 在类上方添加了[Binding]属性(我这部分是哑巴错误)...

[Binding]
    class TestInitialise : BasePage

Added [AfterScenario()] above my screenshot method, and made it a static method... 在我的屏幕截图方法上方添加了[AfterScenario()],并使其成为静态方法...

[AfterScenario()]
public static void TakeScreenshot()

Specified where to save the screenshot... 指定保存屏幕截图的位置...

App.Screenshot(TestContext.CurrentContext.Test.Name).CopyTo(@"C:\Users\me\Desktop\" + TestContext.CurrentContext.Test.Name + ".png");

So I'm guessing App.Screenshot(string title) simply takes a screenshot and holds it in memory, but you actually need to save it somewhere explicitly to get it, rather than assuming it just saves to a default location. 因此,我猜测App.Screenshot(string title)只是获取一个屏幕截图并将其保存在内存中,但是实际上您需要将其明确地保存在某个地方以获取它,而不是假设它只是保存到默认位置。

That's that then! 就是这样!

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

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