|
| 1 | +//****************************************************************************** |
| 2 | +// |
| 3 | +// Copyright (c) 2016 Microsoft Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// This code is licensed under the MIT License (MIT). |
| 6 | +// |
| 7 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | +// THE SOFTWARE. |
| 14 | +// |
| 15 | +//****************************************************************************** |
| 16 | + |
| 17 | +using System; |
| 18 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 19 | +using OpenQA.Selenium.Remote; |
| 20 | + |
| 21 | +namespace CalculatorTest |
| 22 | +{ |
| 23 | + [TestClass] |
| 24 | + public class BasicScenarios |
| 25 | + { |
| 26 | + protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723/wd/hub"; |
| 27 | + protected static RemoteWebDriver CalculatorSession; |
| 28 | + protected static RemoteWebElement CalculatorResult; |
| 29 | + protected static string OriginalCalculatorMode; |
| 30 | + |
| 31 | + [ClassInitialize] |
| 32 | + public static void Setup(TestContext context) |
| 33 | + { |
| 34 | + // Launch the calculator app |
| 35 | + DesiredCapabilities appCapabilities = new DesiredCapabilities(); |
| 36 | + appCapabilities.SetCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"); |
| 37 | + appCapabilities.SetCapability("platformName", "Windows"); |
| 38 | + appCapabilities.SetCapability("deviceName", "WindowsPC"); |
| 39 | + CalculatorSession = new RemoteWebDriver(new Uri(WindowsApplicationDriverUrl), appCapabilities); |
| 40 | + Assert.IsNotNull(CalculatorSession); |
| 41 | + CalculatorSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); |
| 42 | + |
| 43 | + // Make sure we're in standard mode |
| 44 | + CalculatorSession.FindElementByXPath("//Button[starts-with(@Name, \"Menu\")]").Click(); |
| 45 | + OriginalCalculatorMode = CalculatorSession.FindElementByXPath("//List[@AutomationId=\"FlyoutNav\"]//ListItem[@IsSelected=\"True\"]").Text; |
| 46 | + CalculatorSession.FindElementByXPath("//ListItem[@Name=\"Standard Calculator\"]").Click(); |
| 47 | + |
| 48 | + // Use series of operation to locate the calculator result text element as a workaround |
| 49 | + // We currently cannot query element by automationId without using modified appium dot net driver |
| 50 | + // TODO: Use a proper appium/webdriver nuget package that allow us to query based on automationId |
| 51 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Clear\"]").Click(); |
| 52 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Seven\"]").Click(); |
| 53 | + CalculatorResult = CalculatorSession.FindElementByName("Display is 7 ") as RemoteWebElement; |
| 54 | + Assert.IsNotNull(CalculatorResult); |
| 55 | + } |
| 56 | + |
| 57 | + [ClassCleanup] |
| 58 | + public static void TearDown() |
| 59 | + { |
| 60 | + // Restore original mode before closing down |
| 61 | + CalculatorSession.FindElementByXPath("//Button[starts-with(@Name, \"Menu\")]").Click(); |
| 62 | + CalculatorSession.FindElementByXPath("//ListItem[@Name=\"" + OriginalCalculatorMode + "\"]").Click(); |
| 63 | + |
| 64 | + CalculatorResult = null; |
| 65 | + CalculatorSession.Dispose(); |
| 66 | + CalculatorSession = null; |
| 67 | + } |
| 68 | + |
| 69 | + [TestInitialize] |
| 70 | + public void Clear() |
| 71 | + { |
| 72 | + CalculatorSession.FindElementByName("Clear").Click(); |
| 73 | + Assert.AreEqual("Display is 0 ", CalculatorResult.Text); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void Addition() |
| 78 | + { |
| 79 | + CalculatorSession.FindElementByName("One").Click(); |
| 80 | + CalculatorSession.FindElementByName("Plus").Click(); |
| 81 | + CalculatorSession.FindElementByName("Seven").Click(); |
| 82 | + CalculatorSession.FindElementByName("Equals").Click(); |
| 83 | + Assert.AreEqual("Display is 8 ", CalculatorResult.Text); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void Combination() |
| 88 | + { |
| 89 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Seven\"]").Click(); |
| 90 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Multiply by\"]").Click(); |
| 91 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Nine\"]").Click(); |
| 92 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Plus\"]").Click(); |
| 93 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"One\"]").Click(); |
| 94 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Equals\"]").Click(); |
| 95 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Divide by\"]").Click(); |
| 96 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Eight\"]").Click(); |
| 97 | + CalculatorSession.FindElementByXPath("//Button[@Name=\"Equals\"]").Click(); |
| 98 | + Assert.AreEqual("Display is 8 ", CalculatorResult.Text); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void Division() |
| 103 | + { |
| 104 | + CalculatorSession.FindElementByName("Eight").Click(); |
| 105 | + CalculatorSession.FindElementByName("Eight").Click(); |
| 106 | + CalculatorSession.FindElementByName("Divide by").Click(); |
| 107 | + CalculatorSession.FindElementByName("One").Click(); |
| 108 | + CalculatorSession.FindElementByName("One").Click(); |
| 109 | + CalculatorSession.FindElementByName("Equals").Click(); |
| 110 | + Assert.AreEqual("Display is 8 ", CalculatorResult.Text); |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void Multiplication() |
| 115 | + { |
| 116 | + CalculatorSession.FindElementByName("Nine").Click(); |
| 117 | + CalculatorSession.FindElementByName("Multiply by").Click(); |
| 118 | + CalculatorSession.FindElementByName("Nine").Click(); |
| 119 | + CalculatorSession.FindElementByName("Equals").Click(); |
| 120 | + Assert.AreEqual("Display is 81 ", CalculatorResult.Text); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void Subtraction() |
| 125 | + { |
| 126 | + CalculatorSession.FindElementByName("Nine").Click(); |
| 127 | + CalculatorSession.FindElementByName("Minus").Click(); |
| 128 | + CalculatorSession.FindElementByName("One").Click(); |
| 129 | + CalculatorSession.FindElementByName("Equals").Click(); |
| 130 | + Assert.AreEqual("Display is 8 ", CalculatorResult.Text); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments