// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Windows; using OpenQA.Selenium.Interactions; using System.Collections.ObjectModel; using System.Drawing; namespace CalculatorUITestFramework { public class HistoryPanel { public WindowsElement HistoryButton => this.session.TryFindElementByAccessibilityId("HistoryButton"); public WindowsElement ListViewItem => this.session.FindElementByClassName("ListViewItem"); public WindowsElement ClearHistoryButton => this.session.TryFindElementByAccessibilityId("ClearHistory"); private WindowsDriver session => WinAppDriver.Instance.CalculatorSession; private WindowsElement HistoryLabel => this.session.TryFindElementByAccessibilityId("HistoryLabel"); private WindowsElement HistoryListView => this.session.TryFindElementByAccessibilityId("HistoryListView"); private WindowsElement HistoryFlyout => this.session.TryFindElementByAccessibilityId("HistoryFlyout"); /// /// Opens the History Pane by clicking the History pivot label. /// public void OpenHistoryPanel() { this.ResizeWindowToDisplayHistoryLabel(); this.HistoryLabel.Click(); } /// /// Gets all of the history items listed in the History Pane. /// /// A readonly collection of history items. public ReadOnlyCollection GetAllHistoryListViewItems() { OpenHistoryPanel(); return this.HistoryListView.FindElementsByClassName("ListViewItem"); } /// /// Opens the History Pane and clicks the delete button if it is visible. /// public void ClearHistory() { this.HistoryLabel.Click(); string source = this.session.PageSource; if (source.Contains("ClearHistory")) { this.ClearHistoryButton.Click(); } } /// /// If the History label is not displayed, resize the window /// Two attempts are made; the label is not found a "not found" exception is thrown /// public void ResizeWindowToDisplayHistoryLabel() { // Put the calculator in the upper left region of the screen WinAppDriver.Instance.CalculatorSession.Manage().Window.Position = new Point(8, 8); GrowWindowToShowHistoryLabel(WinAppDriver.Instance.CalculatorSession.Manage().Window.Size.Width); } ///// ///// If the History button is not displayed, resize the window ///// public void ResizeWindowToDisplayHistoryButton() { // Put the calculator in the upper left region of the screen WinAppDriver.Instance.CalculatorSession.Manage().Window.Position = new Point(8, 8); ShrinkWindowToShowHistoryButton(WinAppDriver.Instance.CalculatorSession.Manage().Window.Size.Width); } /// /// Opens the History Flyout. /// public void OpenHistoryFlyout() { this.ResizeWindowToDisplayHistoryButton(); CalculatorApp.EnsureCalculatorHasFocus(); CalculatorApp.Window.SendKeys(Keys.Control + "h" + Keys.Control); Actions moveToHistoryFlyout = new Actions(WinAppDriver.Instance.CalculatorSession); moveToHistoryFlyout.MoveToElement(HistoryFlyout); moveToHistoryFlyout.Perform(); } /// /// Gets all of the History items listed in the History Flyout. /// /// A read only collection of History items. public ReadOnlyCollection GetAllHistoryFlyoutListViewItems() { OpenHistoryFlyout(); return this.HistoryListView.FindElementsByClassName("ListViewItem"); } /// /// Increases the size of the window until History label for the History panel is visible /// private void GrowWindowToShowHistoryLabel(int width) { if (width > 2100) { throw new NotFoundException("Could not the History Label"); } if (!this.session.PageSource.Contains("HistoryLabel")) { var height = WinAppDriver.Instance.CalculatorSession.Manage().Window.Size.Height; WinAppDriver.Instance.CalculatorSession.Manage().Window.Size = new Size(width, height); //give window time to render new size System.Threading.Thread.Sleep(10); GrowWindowToShowHistoryLabel(width + 100); } } /// /// Decreases the size of the window until History button is visible /// private void ShrinkWindowToShowHistoryButton(int width) { if (width < 200) { throw new NotFoundException("Could not find the History Button"); } if (!this.session.PageSource.Contains("HistoryButton")) { var height = WinAppDriver.Instance.CalculatorSession.Manage().Window.Size.Height; WinAppDriver.Instance.CalculatorSession.Manage().Window.Size = new Size(width, height); //give window time to render new size System.Threading.Thread.Sleep(10); ShrinkWindowToShowHistoryButton(width - 100); } } } }