diff --git a/Tests/WebDriverAPI/ElementSize.cs b/Tests/WebDriverAPI/ElementSize.cs index 8f669827..de255eec 100644 --- a/Tests/WebDriverAPI/ElementSize.cs +++ b/Tests/WebDriverAPI/ElementSize.cs @@ -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] diff --git a/Tests/WebDriverAPI/Mouse.cs b/Tests/WebDriverAPI/Mouse.cs index f7045eaa..85d59683 100644 --- a/Tests/WebDriverAPI/Mouse.cs +++ b/Tests/WebDriverAPI/Mouse.cs @@ -76,8 +76,18 @@ public void MouseClick() session.Mouse.ContextClick(appNameTitle.Coordinates); Thread.Sleep(TimeSpan.FromSeconds(1.5)); WindowsDriver 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] diff --git a/Tests/WebDriverAPI/Screenshot.cs b/Tests/WebDriverAPI/Screenshot.cs index 47f1a885..fc1b2ac0 100644 --- a/Tests/WebDriverAPI/Screenshot.cs +++ b/Tests/WebDriverAPI/Screenshot.cs @@ -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; @@ -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 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(); + } } } @@ -81,12 +124,64 @@ public void GetElementScreenshotError_StaleElement() [TestMethod] public void GetScreenshot() { - var screenshot = session.GetScreenshot(); - using (MemoryStream msScreenshot = new MemoryStream(screenshot.AsByteArray)) + WindowsDriver notepadSession = null; + WindowsDriver 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(); + } } }