-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathWhiteTestBase.cs
More file actions
190 lines (169 loc) · 6.39 KB
/
WhiteTestBase.cs
File metadata and controls
190 lines (169 loc) · 6.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Castle.Core.Logging;
using TestStack.White.Configuration;
using TestStack.White.InputDevices;
using TestStack.White.ScreenObjects;
using TestStack.White.UIItems;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.UITests.Infrastructure;
using TestStack.White.UITests.Screens;
using Xunit;
namespace TestStack.White.UITests
{
public abstract class WhiteTestBase
{
readonly ILogger logger = CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WhiteTestBase));
readonly List<Window> windowsToClose = new List<Window>();
readonly string screenshotDir;
WindowsFramework? currentFramework;
internal Keyboard Keyboard;
protected WhiteTestBase()
{
screenshotDir = "c:\\FailedTestsScreenshots";
if (!Directory.Exists(screenshotDir))
Directory.CreateDirectory(screenshotDir);
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
}
protected Window MainWindow { get; private set; }
protected MainScreen MainScreen { get; private set; }
protected Application Application { get; private set; }
protected ScreenRepository Repository { get; private set; }
[Fact]
public void Automate()
{
CoreAppXmlConfiguration.Instance.LoggerFactory = new ConsoleFactory(LoggerLevel.Debug);
var frameworksToRun = SupportedFrameworks();
foreach (var framework in frameworksToRun)
{
currentFramework = framework;
using (SetMainWindow(framework))
{
try
{
ExecuteTestRun(framework);
}
catch (TestFailedException)
{
throw;
}
catch (Exception ex)
{
throw new TestFailedException(string.Format("Failed to run test for {0}", framework), ex);
}
}
}
currentFramework = null;
}
protected void RunTest(Action testAction, params WindowsFramework[] runFor)
{
if (!runFor.Any() || runFor.Any(r => r == currentFramework))
{
try
{
testAction();
}
catch (Exception ex)
{
string path2 = string.Empty;
try
{
path2 = testAction.Method.Name + ".png";
var filename = Path.Combine(screenshotDir, path2);
new ScreenCapture().CaptureScreenShot().Save(filename, ImageFormat.Png);
Trace.WriteLine(string.Format("Screenshot taken: {0}", filename));
}
catch (Exception)
{
Trace.TraceError(string.Format("Failed to save screenshot to directory: {0}, filename: {1}", screenshotDir, path2));
}
throw new TestFailedException(string.Format("Failed to run {0} for {1}. Details:\r\n\r\n{2}",
testAction.Method.Name, currentFramework, ex), ex);
}
}
}
protected abstract void ExecuteTestRun(WindowsFramework framework);
private IDisposable SetMainWindow(WindowsFramework framework)
{
try
{
Keyboard = Keyboard.Instance;
var configuration = TestConfigurationFactory.Create(framework);
Application = configuration.LaunchApplication();
Repository = new ScreenRepository(Application);
MainWindow = configuration.GetMainWindow(Application);
MainScreen = configuration.GetMainScreen(Repository);
return new ShutdownApplicationDisposable(this);
}
catch (Exception e)
{
logger.Error("Failed to launch application and get main window", e);
if (Application != null)
Application.Close();
throw;
}
}
protected abstract IEnumerable<WindowsFramework> SupportedFrameworks();
protected IEnumerable<WindowsFramework> AllFrameworks()
{
yield return WindowsFramework.Wpf;
yield return WindowsFramework.WinForms;
yield return WindowsFramework.Silverlight;
}
private class ShutdownApplicationDisposable : IDisposable
{
private readonly WhiteTestBase testBase;
public ShutdownApplicationDisposable(WhiteTestBase testBase)
{
this.testBase = testBase;
}
public void Dispose()
{
foreach (var window in testBase.windowsToClose)
{
if (!window.IsClosed)
window.Close();
}
testBase.windowsToClose.Clear();
testBase.MainWindow.Close();
testBase.Application.Dispose();
testBase.Application = null;
testBase.MainWindow = null;
}
}
protected Window StartScenario(string scenarioButtonId, string windowTitle)
{
MainWindow.Get<Button>(scenarioButtonId).Click();
var window = MainWindow.ModalWindow(windowTitle);
windowsToClose.Add(window);
return window;
}
protected void SelectOtherControls()
{
MainWindow.Tabs[0].SelectTabPage(2);
}
protected void SelectInputControls()
{
MainWindow.Tabs[0].SelectTabPage(1);
}
protected void SelectListControls()
{
MainWindow.Tabs[0].SelectTabPage(0);
}
protected void SelectDataGridTab()
{
MainWindow.Tabs[0].SelectTabPage(3);
}
protected void SelectPropertyGridTab()
{
MainWindow.Tabs[0].SelectTabPage(4);
}
}
}