forked from microsoft/WinAppDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenshot.cs
202 lines (178 loc) · 9.48 KB
/
Screenshot.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
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
191
192
193
194
195
196
197
198
199
200
201
202
//******************************************************************************
//
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
using System;
using System.Drawing;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
namespace WebDriverAPI
{
[TestClass]
public class Screenshot : AlarmClockBase
{
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
[ClassCleanup]
public static void ClassCleanup()
{
TearDown();
}
[TestMethod]
public void GetElementScreenshot()
{
WindowsDriver<WindowsElement> desktopSession = null;
try
{
// Locate the AlarmPivotItem element in Alarms & Clock app to be captured
WindowsElement alarmPivotItem1 = session.FindElementByAccessibilityId(AlarmTabAutomationId);
OpenQA.Selenium.Screenshot alarmPivotItemScreenshot1 = alarmPivotItem1.GetScreenshot();
// Save the AlarmPivotItem screenshot capture locally on the machine running the test
alarmPivotItemScreenshot1.SaveAsFile(@"ScreenshotAlarmPivotItem.png", ScreenshotImageFormat.Png);
// Using the Desktop session, locate the same AlarmPivotItem element in Alarms & Clock app to be captured
desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
WindowsElement alarmPivotItem2 = desktopSession.FindElementByAccessibilityId(AlarmTabAutomationId);
OpenQA.Selenium.Screenshot alarmPivotItemScreenshot2 = alarmPivotItem2.GetScreenshot();
// Using the Desktop session, locate the Alarms & Clock app top level window to be captured
WindowsElement alarmsClockWindowTopWindow = desktopSession.FindElementByName("Alarms & Clock");
OpenQA.Selenium.Screenshot alarmsClockWindowTopWindowScreenshot = alarmsClockWindowTopWindow.GetScreenshot();
using (MemoryStream msScreenshot1 = new MemoryStream(alarmPivotItemScreenshot1.AsByteArray))
using (MemoryStream msScreenshot2 = new MemoryStream(alarmPivotItemScreenshot2.AsByteArray))
using (MemoryStream msScreenshot3 = new MemoryStream(alarmsClockWindowTopWindowScreenshot.AsByteArray))
{
// Verify that the element screenshot has a valid size
Image screenshotImage1 = Image.FromStream(msScreenshot1);
Assert.AreEqual(alarmPivotItem1.Size.Height, screenshotImage1.Height);
Assert.AreEqual(alarmPivotItem1.Size.Width, screenshotImage1.Width);
// Verify that the element screenshot captured using the Alarms & Clock session
// is identical in size with the one taken using the desktop session
Image screenshotImage2 = Image.FromStream(msScreenshot2);
Assert.AreEqual(screenshotImage1.Height, screenshotImage2.Height);
Assert.AreEqual(screenshotImage1.Width, screenshotImage2.Width);
// Verify that the element screenshot is smaller in size compared to the application top level window
Image screenshotImage3 = Image.FromStream(msScreenshot3);
Assert.AreEqual(alarmsClockWindowTopWindow.Size.Height, screenshotImage3.Height);
Assert.AreEqual(alarmsClockWindowTopWindow.Size.Width, screenshotImage3.Width);
Assert.IsTrue(screenshotImage3.Height > screenshotImage1.Height);
Assert.IsTrue(screenshotImage3.Width > screenshotImage1.Width);
}
}
finally
{
if (desktopSession != null)
{
desktopSession.Quit();
}
}
}
[TestMethod]
public void GetElementScreenshotError_NoSuchWindow()
{
try
{
var screenshot = Utility.GetOrphanedElement().GetScreenshot();
Assert.Fail("Exception should have been thrown because there is no such window");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void GetElementScreenshotError_StaleElement()
{
try
{
var screenshot = GetStaleElement().GetScreenshot();
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
}
}
[TestMethod]
public void GetScreenshot()
{
WindowsDriver<WindowsElement> notepadSession = null;
WindowsDriver<WindowsElement> desktopSession = null;
try
{
// Launch and capture a screenshot of a maximized Notepad application. The steps below intentionally use
// the Notepad application window to fully cover the Alarms & Clock application. This setup demonstrates
// that capturing Alarms & Clock screenshot afterward will implicitly bring its window to foreground.
notepadSession = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);
notepadSession.Manage().Window.Maximize();
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
OpenQA.Selenium.Screenshot notepadScreenshot = notepadSession.GetScreenshot();
// Capture a screenshot of Alarms & Clock application
// This implicitly brings the application window to foreground
OpenQA.Selenium.Screenshot alarmsClockScreenshot = session.GetScreenshot();
// Save the application screenshot capture locally on the machine running the test
alarmsClockScreenshot.SaveAsFile(@"ScreenshotAlarmsClockApplication.png", ScreenshotImageFormat.Png);
// Capture the entire desktop using the Desktop session
desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
OpenQA.Selenium.Screenshot desktopScreenshot = desktopSession.GetScreenshot();
// Save the desktop screenshot capture locally on the machine running the test
desktopScreenshot.SaveAsFile(@"ScreenshotDesktop.png", ScreenshotImageFormat.Png);
using (MemoryStream msScreenshot1 = new MemoryStream(alarmsClockScreenshot.AsByteArray))
using (MemoryStream msScreenshot2 = new MemoryStream(notepadScreenshot.AsByteArray))
using (MemoryStream msScreenshot3 = new MemoryStream(desktopScreenshot.AsByteArray))
{
// Verify that the Alarms & Clock application screenshot has a valid size
Image screenshotImage1 = Image.FromStream(msScreenshot1);
Assert.AreEqual(session.Manage().Window.Size.Height, screenshotImage1.Height);
Assert.AreEqual(session.Manage().Window.Size.Width, screenshotImage1.Width);
// Verify that the maximized Notepad application screenshot has a valid size
Image screenshotImage2 = Image.FromStream(msScreenshot2);
Assert.AreEqual(notepadSession.Manage().Window.Size.Height, screenshotImage2.Height);
Assert.AreEqual(notepadSession.Manage().Window.Size.Width, screenshotImage2.Width);
// Verify that the application screenshot is smaller in size compared to the entire desktop
Image screenshotImage3 = Image.FromStream(msScreenshot3);
Assert.IsTrue(screenshotImage2.Height >= screenshotImage1.Height);
Assert.IsTrue(screenshotImage2.Width >= screenshotImage1.Width);
}
}
finally
{
if (notepadSession != null)
{
notepadSession.Quit();
}
if (desktopSession != null)
{
desktopSession.Quit();
}
}
}
[TestMethod]
public void GetScreenshotError_NoSuchWindow()
{
try
{
Utility.GetOrphanedSession().GetScreenshot();
Assert.Fail("Exception should have been thrown because there is no such window");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
}
}