Skip to content

Commit

Permalink
Merged PR 887398: Enhance WebDriverAPI Screenshot unit test to cover …
Browse files Browse the repository at this point in the history
…more scenarios

Enhance WebDriverAPI Screenshot unit test to include more scenarios
Save screenshot captures locally to accommodate manual verification
  • Loading branch information
timotiusmargo committed Sep 22, 2017
2 parents ec19c10 + 0dbb617 commit 9db1a2d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
9 changes: 9 additions & 0 deletions Tests/WebDriverAPI/ElementSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public void GetElementSize()
// Clear button is always bigger than Mem button
Assert.IsTrue(clearButton.Size.Width > memButton.Size.Width);
Assert.IsTrue(clearButton.Size.Height > memButton.Size.Height);

WindowsElement applicationWindow = session.FindElementByClassName("ApplicationFrameWindow");
Assert.IsNotNull(applicationWindow);
Assert.AreEqual(session.Manage().Window.Size.Width, applicationWindow.Size.Width);
Assert.AreEqual(session.Manage().Window.Size.Height, applicationWindow.Size.Height);

// Application top level window is always bigger than its button
Assert.IsTrue(applicationWindow.Size.Width > clearButton.Size.Width);
Assert.IsTrue(applicationWindow.Size.Height > clearButton.Size.Height);
}

[TestMethod]
Expand Down
14 changes: 12 additions & 2 deletions Tests/WebDriverAPI/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ public void MouseClick()
session.Mouse.ContextClick(appNameTitle.Coordinates);
Thread.Sleep(TimeSpan.FromSeconds(1.5));
WindowsDriver<WindowsElement> desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
Assert.IsNotNull(desktopSession.FindElementByName("System").FindElementByName("Minimize"));
clearButton.Click(); // Dismiss the context menu
try
{
Assert.IsNotNull(desktopSession.FindElementByName("System").FindElementByName("Minimize"));
clearButton.Click(); // Dismiss the context menu
}
finally
{
if (desktopSession != null)
{
desktopSession.Quit();
}
}
}

[TestMethod]
Expand Down
117 changes: 106 additions & 11 deletions Tests/WebDriverAPI/Screenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
Expand All @@ -40,13 +41,55 @@ public static void ClassCleanup()
[TestMethod]
public void GetElementScreenshot()
{
WindowsElement element = session.FindElementByAccessibilityId("AddAlarmButton");
var screenshot = element.GetScreenshot();
using (MemoryStream msScreenshot = new MemoryStream(screenshot.AsByteArray))
WindowsDriver<WindowsElement> desktopSession = null;

try
{
Image screenshotImage = Image.FromStream(msScreenshot);
Assert.IsTrue(screenshotImage.Height > 0);
Assert.IsTrue(screenshotImage.Width > 0);
// Locate the AlarmPivotItem element in Alarms & Clock app to be captured
WindowsElement alarmPivotItem1 = session.FindElementByAccessibilityId("AlarmPivotItem");
OpenQA.Selenium.Screenshot alarmPivotItemScreenshot1 = alarmPivotItem1.GetScreenshot();

// Save the AlarmPivotItem screenshot capture locally on the machine running the test
alarmPivotItemScreenshot1.SaveAsFile(@"ScreenshotAlarmPivotItem.png", ImageFormat.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("AlarmPivotItem");
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();
}
}
}

Expand Down Expand Up @@ -81,12 +124,64 @@ public void GetElementScreenshotError_StaleElement()
[TestMethod]
public void GetScreenshot()
{
var screenshot = session.GetScreenshot();
using (MemoryStream msScreenshot = new MemoryStream(screenshot.AsByteArray))
WindowsDriver<WindowsElement> notepadSession = null;
WindowsDriver<WindowsElement> desktopSession = null;

try
{
Image screenshotImage = Image.FromStream(msScreenshot);
Assert.IsTrue(screenshotImage.Height > 0);
Assert.IsTrue(screenshotImage.Width > 0);
// 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", ImageFormat.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", ImageFormat.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();
}
}
}

Expand Down

0 comments on commit 9db1a2d

Please sign in to comment.