Class SaveScreenshot
- Namespace
- CSF.Screenplay.Selenium.Actions
- Assembly
- CSF.Screenplay.Selenium.dll
An action which saves a Selenium Screenshot object to disk as an asset file.
public class SaveScreenshot : IPerformable, ICanReport
- Inheritance
-
SaveScreenshot
- Implements
- Inherited Members
Examples
In this example, the action will save the screenshot with the name Shopping cart items, using the JPEG
file format.
Note that because of the internal workings of the GetAssetFilePaths mechanism, the precise file
name by which the asset is saved is indeterminate. The precise details of file naming are determined by
AssetPathProvider.
using OpenQA.Selenium;
using static CSF.Screenplay.Selenium.PerformableBuilder;
// Retrieved via (for example) the TakeScreenshot question
readonly Screenshot screenshot;
// Within the logic of a custom task, deriving from IPerformable
public async ValueTask PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
// ... other performance logic
await actor.PerformAsync(SaveTheScreenshot(screenshot)
.WithTheName("Shopping cart items")
.WithTheFormat(ScreenshotImageFormat.Jpeg),
cancellationToken);
// ... other performance logic
}
Remarks
Use this action via the builder method SaveTheScreenshot(Screenshot). Instead of this action, consider using the task TakeAndSaveScreenshot, via either of the builder methods TakeAndSaveAScreenshot() or TakeAndSaveAScreenshotIfSupported(). Use of this action (standalone) is only required if you wish to examine or interact with the Screenshot object. If all you want to achieve is to take the screenshot and save it, the task is likely to be more convenient.
Performing this action with a specified Screenshot object, such as one retrieved via the TakeScreenshot question, saves that Screenshot object to disk as an asset file
To perform this action, the actor must have the ability GetAssetFilePaths. If the actor does not have the ability then this action will throw an exception.
Constructors
SaveScreenshot(Screenshot, string, ScreenshotImageFormat)
Initializes a new instance of the SaveScreenshot class.
public SaveScreenshot(Screenshot screenshot, string name = null, ScreenshotImageFormat format = ScreenshotImageFormat.Png)
Parameters
screenshotScreenshotThe screenshot to save.
namestringA human-readable name for the screenshot, which identifies it within the performance.
formatScreenshotImageFormatAn optional format in which to save the screenshot; if omitted then PNG will be used by default.
Methods
GetReportFragment(Actor, IFormatsReportFragment)
Gets a fragment of a Screenplay report, specific to the execution (performables) or gaining (abilities) of the current instance, for the specified actor.
public ReportFragment GetReportFragment(Actor actor, IFormatsReportFragment formatter)
Parameters
actorActorAn actor for whom to write the report fragment
formatterIFormatsReportFragmentA report-formatting service
Returns
- ReportFragment
A human-readable report fragment.
Examples
For a performable which clicks a button (where the button itself has been constructor-injected into
the performable instance), then a suitable return value might be a formatted string such as
{Actor name} clicks {Button}, where the two placeholders indicated by braces: {} are
substituted with the actor's Name and a string representation of
the button.
For a performable which reads the temperature from a thermometer, a suitable return value might be a
string in the format {Actor name} reads the temperature.
For an ability which allows the actor to wash dishes then a suitable return value might be a string in the
format {Actor name} is able to wash the dishes.
Remarks
Implementers should return a string which indicates that the named actor is performing (present tense)
the performable, for types which also implement a performable interface. For types which represent
abilities, the implementer should return a string which indicates that the named actor is able to do
something. In particular for abilities, to make them easily recognisable in reports, it helps to stick
to the convention {Actor name} is able to {Ability summary}.
For performables which return a value (Questions, or Tasks which behave like Questions), there is no need to include the returned value within the report fragment. The framework will include the return value in the report and will format it via a different mechanism.
Good report fragments are concise. Be aware that report fragments for Tasks (which are composed from other performables) do not need to go into detail about what they do. Users reading Screenplay reports are able to drill-down into Tasks to see what they are composed from, so if the user is curious as to what the task does, it is easy to discover. It is also strongly recommended to avoid periods (full stops) at the end of a report fragment. Whilst report fragments tend to be complete sentences, punctuation like this is distracting and reports are seldom presented as paragraphs of prose.
PerformAsAsync(ICanPerform, CancellationToken)
Performs the action(s) are represented by the current instance.
public ValueTask PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
Parameters
actorICanPerformThe actor that is performing.
cancellationTokenCancellationTokenAn optional cancellation token by which to abort the performable.
Returns
- ValueTask
A task which completes when the performable represented by the current instance is complete.