Actor extensions for Selenium - Galad/tranquire GitHub Wiki

HighlightTargets

You can highglight the html elements in a page when they are being resolved. It is useful when debugging a test.

var actor = new Actor("John").HighlightTargets();

SlowSelenium

You can slow down the actions that uses Selenium. It is useful to use this feature with HighlighTargets in order to properly see the elements highlighting.

var actor = new Actor("John").SlowSelenium(TimeSpan.FromSeconds(1));

WithSeleniumReporting

This extension method offers a simplified way to configure reporting for Tranquire. It combines TakeScreenshots and WithReporting extension methods. It returns a ISeleniumReporter instance that can be used to get the report and save the screenshots on the disk.

new Actor("John")
        .WithSeleniumReporting(
              new SeleniumReportingConfiguration(@"C:\Screenshots", "Screenshots_{0:00}")
                  .AddTextObservers(new DebugObserver(), new ConsoleObserver()),              
              out var seleniumReporter
              );

See TakeScreenshot and WithReporting for more details on how reporting works.

The available transformations of the configurations are:

  • AddTextObservers
  • WithCanNotify
  • WithTakeScreenshotStrategy

TakeScreenshots

You can take a Screenshot after each Selenium action. The following example save screenshots in the folder C:\Screenshots. The name of the screenshot will be Add a todo item_01.jpg, Add a todo item_20.jpg, etc.

var actor = new Actor("John").TakeScreenshots(@"C:\Screenshots", "Add a todo item");

The screenshot name can be a format string:

    var actor = new Actor("John").TakeScreenshots(@"C:\Screenshots", "Add a todo item - {0:000}.jpg");

You can also control when the screenshots are saved. Saving them takes some time so it is useful to save them only when you want to attach them to a test, for instance when the test fail. The following example shows a test (with NUnit) that attaches screenshots when the test fails, without saving them on the disk when the test pass.

[SetUp]
public void SetUp()
{
    this.screenshotObserver = new SaveScreenshotsToFileOnComplete(@"C:\Screenshots");
    this.actor = new Actor("John").TakeScreenshots("Add a todo item", screenshotObserver );
}

[Test]
public void AddAToDoItem()
{
    this.actor.When(Add.ToDoItem("Buy bread"));
    // do some more actions and assertions
}

[TearDown]
public void TearDown()
{
    if (TestContext.CurrentContext.Result.Outcome != ResultState.Success &&
        Directory.Exists(@"C:\Screenshots"))
    {
        // save the screenshots on the disk
        this.screenshotObserver.OnComplete();
        // attach all the screenshots in the directory to the test results
        foreach(var file in Directory.GetFiles(@"C:\Screenshots"))
        {
            TestContext.AddTestAttachment(file);
        }
    }
}

Adding the screenshots to the XML and HTML reports

If you enabled XML reports, you can add screenshots as attachments in order to display then in the HTML report. To enable that, just pass the instance of XmlDocumentObserver to the TakeScreenshot method using the adapter:

var xmlDocumentReporting = new XmlDocumentObserver();            
var screenshotObserver = new CompositeObserver<ScreenshotInfo>(
                new SaveScreenshotsToFileOnComplete(Path.Combine(GetTestDirectory(), "Screenshots")),
                new ScreenshotInfoToActionAttachmentObserverAdapter(xmlDocumentReporting)
                );
var actor = new Actor("John")
                    .WithReporting(xmlDocumentReporting)
                    .TakeScreenshots(screenshotName, screenshotObserver)                           
                    .CanUse(WebBrowser.With(driver));
// run the test
// ...

screenshotObserver.OnCompleted();
xmlDocumentReporting.GetXmlDocument().SaveFile("actionReport.xml");
File.WriteAllText("actionReport.html", xmlDocumentReporting.GetHtmlDocument());

TakeScreenshots strategy

You can optionally specify how and when the screenshots are taken, by passing a ITakeScreenshotStrategy. The available strategies are:

  • AlwaysTakeScreenshotStrategy. Take a screenshot when each action with a WebBrowser ability is executed.
  • TakeScreenshotOnErrorStrategy. Take a screenshot when any action throws an exception.
  • TakeSampleScreenshotsStrategy. Take a screenshot every x action with a WebBrowser ability is executed, and when the action throws an exception.