2

I would like to wrap a class into my Test so i don't need to write the class every time.

Story: I have a base class that takes a screenshot on a test fail. The function works well, but I have to wrap all the test with the class and that's what I want to avoid.

A test looks like this now.

[Fact]
public void TextEditor()
{
    UITest(() =>
    {
        // Run Test
    });
}

But my expected result is

[Fact]
public void TextEditor()
{
    // Run Test
}

Is it possible to merge the class with the FACT attribute? or do you have an idea on how it could be done?

Base Class:

protected void UITest(Action action)
{
    try
    {
        action();
    }
    catch (Exception ex)
    {
        var screenshotHandler = CurrentBrowser.Driver as ITakesScreenshot;
        if (screenshotHandler != null)
        {
            var screenshot = screenshotHandler.GetScreenshot();
            var path = CurrentBrowser.GetScreenshotLocation(
                new StackFrame(1).GetMethod().Name);
            var file = new FileInfo(path);
            if (file.Directory != null && !file.Directory.Exists)
            {
                file.Directory.Create();
            }
            screenshot.SaveAsFile(path, ScreenshotImageFormat.Jpeg);
        }

        throw;
    }
}
budi
  • 6,351
  • 10
  • 55
  • 80
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 5
    This might help you: https://stackoverflow.com/questions/2206554/how-to-wrap-a-method-via-attributes – Mischa Jul 07 '17 at 11:42
  • 2
    Yeah, Aspect-Oriented Programming would be one way. Alternatively, write your own test-runner and implement this behaviour in that...([XUnit](https://github.com/xunit/xunit) is open-source, so you could customize that to your needs - it looks like you are using it anyway). – RB. Jul 07 '17 at 12:38
  • @MischaBehrend @.RB. Thank you! Had missed AOP. Will look into that now. – Dymond Jul 07 '17 at 12:47
  • 2
    This is one of the reasons why we didn't go with `XUnit`. It's great for normal unit tests, but I feel it's not ideal for a UI test framework. – mrfreester Jul 07 '17 at 16:43

0 Answers0