forked from machine/machine.specifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeleniumResultSupplementer.cs
80 lines (71 loc) · 2.38 KB
/
SeleniumResultSupplementer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Selenium;
namespace Machine.Specifications.SeleniumSupport
{
public abstract class SeleniumResultSupplementer : ISupplementSpecificationResults
{
protected abstract string ImagesPath { get; }
protected abstract ISelenium Selenium { get; }
protected virtual bool SuppressFullPageScreenshotErrors { get { return false; } }
public Result SupplementResult(Result result)
{
if (result.Status != Status.Failing)
return result;
Guid guid = Guid.NewGuid();
string pageScreenshotPath = Path.Combine(ImagesPath, guid.ToString() + "-full-page-screenshot.png");
string screenshotPath = Path.Combine(ImagesPath, guid.ToString() + "-screenshot.png");
string htmlPath = Path.Combine(ImagesPath, guid.ToString() + ".html");
Dictionary<string, string> supplement = new Dictionary<string, string>();
try
{
Selenium.CaptureEntirePageScreenshot(pageScreenshotPath, "");
supplement["img-full-page-screenshot"] = pageScreenshotPath;
}
catch (Exception err)
{
if (!SuppressFullPageScreenshotErrors)
{
Console.Error.WriteLine("Problem capturing Selenium img-full-page-screenshot");
Console.Error.WriteLine(err);
}
}
try
{
Selenium.CaptureScreenshot(screenshotPath);
supplement["img-screenshot"] = screenshotPath;
}
catch (Exception err)
{
Console.Error.WriteLine("Problem capturing Selenium img-screenshot");
Console.Error.WriteLine(err);
}
try
{
supplement["text-log"] = Selenium.RetrieveLastRemoteControlLogs();
}
catch (Exception err)
{
Console.Error.WriteLine("Problem capturing Selenium text-log");
Console.Error.WriteLine(err);
}
try
{
using (var writer = new StreamWriter(htmlPath))
{
writer.Write(Selenium.GetHtmlSource());
}
supplement["html-source"] = htmlPath;
}
catch (Exception err)
{
Console.Error.WriteLine("Problem capturing Selenium html-source");
Console.Error.WriteLine(err);
}
return Result.Supplement(result, "Selenium", supplement);
}
}
}