16
16
17
17
using System ;
18
18
using System . Drawing ;
19
+ using System . Drawing . Imaging ;
19
20
using System . IO ;
20
21
using Microsoft . VisualStudio . TestTools . UnitTesting ;
21
22
using OpenQA . Selenium . Appium . Windows ;
@@ -40,13 +41,55 @@ public static void ClassCleanup()
40
41
[ TestMethod ]
41
42
public void GetElementScreenshot ( )
42
43
{
43
- WindowsElement element = session . FindElementByAccessibilityId ( "AddAlarmButton" ) ;
44
- var screenshot = element . GetScreenshot ( ) ;
45
- using ( MemoryStream msScreenshot = new MemoryStream ( screenshot . AsByteArray ) )
44
+ WindowsDriver < WindowsElement > desktopSession = null ;
45
+
46
+ try
46
47
{
47
- Image screenshotImage = Image . FromStream ( msScreenshot ) ;
48
- Assert . IsTrue ( screenshotImage . Height > 0 ) ;
49
- Assert . IsTrue ( screenshotImage . Width > 0 ) ;
48
+ // Locate the AlarmPivotItem element in Alarms & Clock app to be captured
49
+ WindowsElement alarmPivotItem1 = session . FindElementByAccessibilityId ( "AlarmPivotItem" ) ;
50
+ OpenQA . Selenium . Screenshot alarmPivotItemScreenshot1 = alarmPivotItem1 . GetScreenshot ( ) ;
51
+
52
+ // Save the AlarmPivotItem screenshot capture locally on the machine running the test
53
+ alarmPivotItemScreenshot1 . SaveAsFile ( @"ScreenshotAlarmPivotItem.png" , ImageFormat . Png ) ;
54
+
55
+ // Using the Desktop session, locate the same AlarmPivotItem element in Alarms & Clock app to be captured
56
+ desktopSession = Utility . CreateNewSession ( CommonTestSettings . DesktopAppId ) ;
57
+ WindowsElement alarmPivotItem2 = desktopSession . FindElementByAccessibilityId ( "AlarmPivotItem" ) ;
58
+ OpenQA . Selenium . Screenshot alarmPivotItemScreenshot2 = alarmPivotItem2 . GetScreenshot ( ) ;
59
+
60
+ // Using the Desktop session, locate the Alarms & Clock app top level window to be captured
61
+ WindowsElement alarmsClockWindowTopWindow = desktopSession . FindElementByName ( "Alarms & Clock" ) ;
62
+ OpenQA . Selenium . Screenshot alarmsClockWindowTopWindowScreenshot = alarmsClockWindowTopWindow . GetScreenshot ( ) ;
63
+
64
+ using ( MemoryStream msScreenshot1 = new MemoryStream ( alarmPivotItemScreenshot1 . AsByteArray ) )
65
+ using ( MemoryStream msScreenshot2 = new MemoryStream ( alarmPivotItemScreenshot2 . AsByteArray ) )
66
+ using ( MemoryStream msScreenshot3 = new MemoryStream ( alarmsClockWindowTopWindowScreenshot . AsByteArray ) )
67
+ {
68
+ // Verify that the element screenshot has a valid size
69
+ Image screenshotImage1 = Image . FromStream ( msScreenshot1 ) ;
70
+ Assert . AreEqual ( alarmPivotItem1 . Size . Height , screenshotImage1 . Height ) ;
71
+ Assert . AreEqual ( alarmPivotItem1 . Size . Width , screenshotImage1 . Width ) ;
72
+
73
+ // Verify that the element screenshot captured using the Alarms & Clock session
74
+ // is identical in size with the one taken using the desktop session
75
+ Image screenshotImage2 = Image . FromStream ( msScreenshot2 ) ;
76
+ Assert . AreEqual ( screenshotImage1 . Height , screenshotImage2 . Height ) ;
77
+ Assert . AreEqual ( screenshotImage1 . Width , screenshotImage2 . Width ) ;
78
+
79
+ // Verify that the element screenshot is smaller in size compared to the application top level window
80
+ Image screenshotImage3 = Image . FromStream ( msScreenshot3 ) ;
81
+ Assert . AreEqual ( alarmsClockWindowTopWindow . Size . Height , screenshotImage3 . Height ) ;
82
+ Assert . AreEqual ( alarmsClockWindowTopWindow . Size . Width , screenshotImage3 . Width ) ;
83
+ Assert . IsTrue ( screenshotImage3 . Height > screenshotImage1 . Height ) ;
84
+ Assert . IsTrue ( screenshotImage3 . Width > screenshotImage1 . Width ) ;
85
+ }
86
+ }
87
+ finally
88
+ {
89
+ if ( desktopSession != null )
90
+ {
91
+ desktopSession . Quit ( ) ;
92
+ }
50
93
}
51
94
}
52
95
@@ -81,12 +124,64 @@ public void GetElementScreenshotError_StaleElement()
81
124
[ TestMethod ]
82
125
public void GetScreenshot ( )
83
126
{
84
- var screenshot = session . GetScreenshot ( ) ;
85
- using ( MemoryStream msScreenshot = new MemoryStream ( screenshot . AsByteArray ) )
127
+ WindowsDriver < WindowsElement > notepadSession = null ;
128
+ WindowsDriver < WindowsElement > desktopSession = null ;
129
+
130
+ try
86
131
{
87
- Image screenshotImage = Image . FromStream ( msScreenshot ) ;
88
- Assert . IsTrue ( screenshotImage . Height > 0 ) ;
89
- Assert . IsTrue ( screenshotImage . Width > 0 ) ;
132
+ // Launch and capture a screenshot of a maximized Notepad application. The steps below intentionally use
133
+ // the Notepad application window to fully cover the Alarms & Clock application. This setup demonstrates
134
+ // that capturing Alarms & Clock screenshot afterward will implicitly bring its window to foreground.
135
+ notepadSession = Utility . CreateNewSession ( CommonTestSettings . NotepadAppId ) ;
136
+ notepadSession . Manage ( ) . Window . Maximize ( ) ;
137
+ System . Threading . Thread . Sleep ( TimeSpan . FromSeconds ( 1 ) ) ;
138
+ OpenQA . Selenium . Screenshot notepadScreenshot = notepadSession . GetScreenshot ( ) ;
139
+
140
+ // Capture a screenshot of Alarms & Clock application
141
+ // This implicitly brings the application window to foreground
142
+ OpenQA . Selenium . Screenshot alarmsClockScreenshot = session . GetScreenshot ( ) ;
143
+
144
+ // Save the application screenshot capture locally on the machine running the test
145
+ alarmsClockScreenshot . SaveAsFile ( @"ScreenshotAlarmsClockApplication.png" , ImageFormat . Png ) ;
146
+
147
+ // Capture the entire desktop using the Desktop session
148
+ desktopSession = Utility . CreateNewSession ( CommonTestSettings . DesktopAppId ) ;
149
+ OpenQA . Selenium . Screenshot desktopScreenshot = desktopSession . GetScreenshot ( ) ;
150
+
151
+ // Save the desktop screenshot capture locally on the machine running the test
152
+ desktopScreenshot . SaveAsFile ( @"ScreenshotDesktop.png" , ImageFormat . Png ) ;
153
+
154
+ using ( MemoryStream msScreenshot1 = new MemoryStream ( alarmsClockScreenshot . AsByteArray ) )
155
+ using ( MemoryStream msScreenshot2 = new MemoryStream ( notepadScreenshot . AsByteArray ) )
156
+ using ( MemoryStream msScreenshot3 = new MemoryStream ( desktopScreenshot . AsByteArray ) )
157
+ {
158
+ // Verify that the Alarms & Clock application screenshot has a valid size
159
+ Image screenshotImage1 = Image . FromStream ( msScreenshot1 ) ;
160
+ Assert . AreEqual ( session . Manage ( ) . Window . Size . Height , screenshotImage1 . Height ) ;
161
+ Assert . AreEqual ( session . Manage ( ) . Window . Size . Width , screenshotImage1 . Width ) ;
162
+
163
+ // Verify that the maximized Notepad application screenshot has a valid size
164
+ Image screenshotImage2 = Image . FromStream ( msScreenshot2 ) ;
165
+ Assert . AreEqual ( notepadSession . Manage ( ) . Window . Size . Height , screenshotImage2 . Height ) ;
166
+ Assert . AreEqual ( notepadSession . Manage ( ) . Window . Size . Width , screenshotImage2 . Width ) ;
167
+
168
+ // Verify that the application screenshot is smaller in size compared to the entire desktop
169
+ Image screenshotImage3 = Image . FromStream ( msScreenshot3 ) ;
170
+ Assert . IsTrue ( screenshotImage2 . Height >= screenshotImage1 . Height ) ;
171
+ Assert . IsTrue ( screenshotImage2 . Width >= screenshotImage1 . Width ) ;
172
+ }
173
+ }
174
+ finally
175
+ {
176
+ if ( notepadSession != null )
177
+ {
178
+ notepadSession . Quit ( ) ;
179
+ }
180
+
181
+ if ( desktopSession != null )
182
+ {
183
+ desktopSession . Quit ( ) ;
184
+ }
90
185
}
91
186
}
92
187
0 commit comments