Skip to content

Commit 0dbb617

Browse files
committed
Enhance WebDriverAPI Screenshot unit test to include more scenarios
Save screenshot captures locally to accommodate manual verification Add a case for getting top level window element size in WebDriverAPI
1 parent ec19c10 commit 0dbb617

File tree

3 files changed

+127
-13
lines changed

3 files changed

+127
-13
lines changed

Tests/WebDriverAPI/ElementSize.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public void GetElementSize()
5151
// Clear button is always bigger than Mem button
5252
Assert.IsTrue(clearButton.Size.Width > memButton.Size.Width);
5353
Assert.IsTrue(clearButton.Size.Height > memButton.Size.Height);
54+
55+
WindowsElement applicationWindow = session.FindElementByClassName("ApplicationFrameWindow");
56+
Assert.IsNotNull(applicationWindow);
57+
Assert.AreEqual(session.Manage().Window.Size.Width, applicationWindow.Size.Width);
58+
Assert.AreEqual(session.Manage().Window.Size.Height, applicationWindow.Size.Height);
59+
60+
// Application top level window is always bigger than its button
61+
Assert.IsTrue(applicationWindow.Size.Width > clearButton.Size.Width);
62+
Assert.IsTrue(applicationWindow.Size.Height > clearButton.Size.Height);
5463
}
5564

5665
[TestMethod]

Tests/WebDriverAPI/Mouse.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,18 @@ public void MouseClick()
7676
session.Mouse.ContextClick(appNameTitle.Coordinates);
7777
Thread.Sleep(TimeSpan.FromSeconds(1.5));
7878
WindowsDriver<WindowsElement> desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
79-
Assert.IsNotNull(desktopSession.FindElementByName("System").FindElementByName("Minimize"));
80-
clearButton.Click(); // Dismiss the context menu
79+
try
80+
{
81+
Assert.IsNotNull(desktopSession.FindElementByName("System").FindElementByName("Minimize"));
82+
clearButton.Click(); // Dismiss the context menu
83+
}
84+
finally
85+
{
86+
if (desktopSession != null)
87+
{
88+
desktopSession.Quit();
89+
}
90+
}
8191
}
8292

8393
[TestMethod]

Tests/WebDriverAPI/Screenshot.cs

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Drawing;
19+
using System.Drawing.Imaging;
1920
using System.IO;
2021
using Microsoft.VisualStudio.TestTools.UnitTesting;
2122
using OpenQA.Selenium.Appium.Windows;
@@ -40,13 +41,55 @@ public static void ClassCleanup()
4041
[TestMethod]
4142
public void GetElementScreenshot()
4243
{
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
4647
{
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+
}
5093
}
5194
}
5295

@@ -81,12 +124,64 @@ public void GetElementScreenshotError_StaleElement()
81124
[TestMethod]
82125
public void GetScreenshot()
83126
{
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
86131
{
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+
}
90185
}
91186
}
92187

0 commit comments

Comments
 (0)