コード例 #1
0
    public void VerifyInput()
    {
        //
        // Start the application we are testing
        //
        string sampleAppPath      = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), "SampleApp.exe");
        OutOfProcessSettings oops = new OutOfProcessSettings(new ProcessStartInfo(sampleAppPath), null, null);
        AutomatedApplication a    = AutomatedApplication.Start(oops);

        try
        {
            a.WaitForMainWindow(TimeSpan.FromSeconds(10));

            //
            // Discover various elements in the UI
            //
            AutomationElement inputTextBox  = AutomationUtilities.FindElementsById(a.MainWindow, "inputTextBox")[0];
            AutomationElement outputTextBox = AutomationUtilities.FindElementsById(a.MainWindow, "outputTextBox")[0];
            AutomationElement appendButton  = AutomationUtilities.FindElementsById(a.MainWindow, "appendButton")[0];

            //
            // Click on the input text box and simulate typing
            //
            string inputText    = "TestTest";
            string expectedText = inputText + "\n";

            WindowPattern winPattern = a.MainWindow.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
            Helpers.MoveToAndClick(inputTextBox);
            winPattern.WaitForInputIdle(1000);
            Microsoft.Test.Keyboard.Type(inputText);
            winPattern.WaitForInputIdle(1000);

            //
            // Now click the button
            //
            Helpers.MoveToAndClick(appendButton);
            winPattern.WaitForInputIdle(1000);

            //
            // Now, get the text of the outputTextBox and compare it against what's expected
            // Fail the test if expected != actual
            //
            TextPattern textPattern = outputTextBox.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
            string      actualText  = textPattern.DocumentRange.GetText(-1);

            //
            // Report the test result
            //
            Assert.AreEqual(expectedText, actualText);
        }

        finally
        {
            //
            // Close the tested application
            //
            a.Close();
        }
    }
コード例 #2
0
    public void VerifyWindowAppearance()
    {
        //
        // Start the application we are testing
        //
        string sampleAppPath   = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), "SampleApp.exe");
        AutomatedApplication a = new OutOfProcessApplication(new OutOfProcessApplicationSettings
        {
            ProcessStartInfo = new ProcessStartInfo(sampleAppPath),
            ApplicationImplementationFactory = new UIAutomationOutOfProcessApplicationFactory()
        });

        a.Start();

        try
        {
            a.WaitForMainWindow(TimeSpan.FromSeconds(10));
            Thread.Sleep(1000);  // Ensure that the Vista/Win7 window creation animation is complete

            var mainWindow = a.MainWindow as AutomationElement;


            //
            // Discover the checkbox in the UI, then click it
            //
            AutomationElement styleBox = AutomationUtilities.FindElementsById(mainWindow, "styleBox")[0];
            Helpers.MoveToAndClick(styleBox);

            //
            // Capture the window image and compare to the master image by generating a
            // diff image and processing the diff image with a tolerance map verifier
            //
            Snapshot toleranceMap = Snapshot.FromFile("ToleranceMap.png");
            Snapshot master       = Snapshot.FromFile("Master.png");
            Snapshot actual       = Snapshot.FromWindow((IntPtr)mainWindow.Current.NativeWindowHandle, WindowSnapshotMode.ExcludeWindowBorder);
            Snapshot difference   = actual.CompareTo(master);

            master.ToFile(@"Master-expected.png", ImageFormat.Png);
            actual.ToFile(@"Master-actual.png", ImageFormat.Png);
            difference.ToFile(@"Master-difference.png", ImageFormat.Png);

            //
            // Report the test result
            //
            SnapshotVerifier verifier = new SnapshotToleranceMapVerifier(toleranceMap);
            Assert.AreEqual(VerificationResult.Pass, verifier.Verify(difference));
        }
        finally
        {
            //
            // Close the tested application
            //
            a.Close();
        }
    }
コード例 #3
0
    public void VerifyWindowAppearance()
    {
        //
        // Start the application we are testing
        //
        string sampleAppPath      = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase), "SampleApp.exe");
        OutOfProcessSettings oops = new OutOfProcessSettings(new ProcessStartInfo(sampleAppPath), null, null);
        AutomatedApplication a    = AutomatedApplication.Start(oops);

        try
        {
            a.WaitForMainWindow(TimeSpan.FromSeconds(10));

            //
            // Discover the checkbox in the UI, then click it
            //
            AutomationElement styleBox = AutomationUtilities.FindElementsById(a.MainWindow, "styleBox")[0];
            Helpers.MoveToAndClick(styleBox);
            Thread.Sleep(1000);  // Ensure that the Vista/Win7 window creation animation is complete

            //
            // Capture the window image and compare to the master image by generating a
            // diff image and processing the diff image with a tolerance color verifier
            //
            Snapshot master     = Snapshot.FromFile(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Master0.png"));
            Snapshot actual     = Snapshot.FromWindow((IntPtr)a.MainWindow.Current.NativeWindowHandle, WindowSnapshotMode.ExcludeWindowBorder);
            Snapshot difference = actual.CompareTo(master);
            master.ToFile(@"Master0-expected.png", ImageFormat.Png);
            actual.ToFile(@"Master0-actual.png", ImageFormat.Png);
            difference.ToFile(@"Master0-difference.png", ImageFormat.Png);

            //
            // Report the test result
            //
            SnapshotVerifier verifier = new SnapshotColorVerifier(Color.Black, new ColorDifference(255, 18, 18, 18));
            Assert.AreEqual(VerificationResult.Pass, verifier.Verify(difference));
        }

        finally
        {
            //
            // Close the tested application
            //
            a.Close();
        }
    }
コード例 #4
0
        private void DrawEdgeTestApi(string fromId, string toId)
        {
            RunInBackground(
                delegate {
                var fromElement = AutomationUtilities.FindElementsById(GetGraphControlAutomationElement(), fromId);
                var toElement   = AutomationUtilities.FindElementsById(GetGraphControlAutomationElement(), toId);
                // Normally we would use GetClickablePoint here. However, the AutomationPeer takes the value returned and
                // then looks whether there really is either the element or a descendant at that point. And that goes
                // wrong as soon as there is an edge overlapping a node. The AutomationPeer sees the edge and determines
                // that the clickable point is invalid and throws an exception.
                // To work around that we use the same default logic AutomationPeers have for determining a clickable
                // point: Just take the center of the bounding box. It should work most of the time, and in this case we
                // don't particularly care whether an edge overlays the *target* node, for example. The edge doesn't react
                // in that case anyway. And even if it overlays the source node there's still a good chance that our
                // initial drag point will work.
                var fromBounds = fromElement[0].Current.BoundingRectangle;
                var toBounds   = toElement[0].Current.BoundingRectangle;
                var fromPoint  = new Point((int)(fromBounds.X + fromBounds.Width / 2),
                                           (int)(fromBounds.Y + fromBounds.Height / 2));
                var toPoint = new Point((int)(toBounds.X + toBounds.Width / 2),
                                        (int)(toBounds.Y + toBounds.Height / 2));

                Mouse.MoveTo(fromPoint);
                Thread.Sleep(50);
                Mouse.Down(MouseButton.Left);
                Thread.Sleep(50);
                // If we're trying to create a self-loop we have to drag outside the node first, otherwise it's just a
                // click that selects the node.
                if (fromPoint == toPoint)
                {
                    Mouse.MoveTo(new Point((int)fromBounds.X - 10, (int)fromBounds.Y - 10));
                    Thread.Sleep(50);
                    // Note: The following mini-move is necessary due to how yFiles does state transitions while finding a
                    // suitable target for drawing the edge. It just generates one additional MouseEvent on the node.
                    // This is also only necessary when drawing self-loops, so in many real-world cases it can be left out.
                    Mouse.MoveTo(new Point(fromPoint.X + 1, fromPoint.Y + 1));
                    Thread.Sleep(50);
                }
                Mouse.MoveTo(toPoint);
                Thread.Sleep(50);
                Mouse.Up(MouseButton.Left);
            },
                delegate { });
        }
コード例 #5
0
        public void VerifyInput()
        {
            AutomationElement rootElement;
            Process           appProcess = AutomationHelpers.StartProcess(new ProcessStartInfo("SampleApp.exe"), out rootElement);

            AutomationElement inputTextBox  = AutomationUtilities.FindElementsById(rootElement, "inputTextBox")[0];
            AutomationElement outputTextBox = AutomationUtilities.FindElementsById(rootElement, "outputTextBox")[0];
            AutomationElement button        = AutomationUtilities.FindElementsById(rootElement, "appendButton")[0];

            string inputText    = "TestTest";
            string expectedText = inputText + "\n";

            WindowPattern winPattern = (WindowPattern)rootElement.GetCurrentPattern(WindowPatternIdentifiers.Pattern);

            AutomationHelpers.MoveToAndClick(inputTextBox);
            winPattern.WaitForInputIdle(1000);
            Microsoft.Test.Keyboard.Type(inputText);
            winPattern.WaitForInputIdle(1000);
            AutomationHelpers.MoveToAndClick(button);
            winPattern.WaitForInputIdle(1000);

            object o;

            outputTextBox.TryGetCurrentPattern(TextPatternIdentifiers.Pattern, out o);
            TextPattern pattern    = (TextPattern)o;
            string      actualText = pattern.DocumentRange.GetText(-1);

            try
            {
                Assert.AreEqual(expectedText, actualText, "The text did not match. Expected: {0}  Actual: {1}", expectedText, actualText);
            }
            finally
            {
                AutomationHelpers.CloseWindow(rootElement);
                appProcess.WaitForExit();
            }
        }
コード例 #6
0
        public void VerifyWindowAppearance()
        {
            ApplicationDriver driver = new ApplicationDriver(typeof(SampleApp.App));

            driver.WaitForIdleUi();
            AutomationElement window        = AutomationUtilities.FindElementsById(AutomationElement.RootElement, "sampleAppWindow")[0];
            AutomationElement styleBox      = AutomationUtilities.FindElementsById(window, "styleBox")[0];
            AutomationElement captureRegion = AutomationUtilities.FindElementsById(window, "captureContainer")[0];

            AutomationHelpers.MoveToAndClick(styleBox);
            driver.WaitForIdleUi();

            try
            {
                // Capture the actual pixels from the bounds of the screen rectangle
                Snapshot actual = Snapshot.FromRectangle(AutomationHelpers.GetElementSize(captureRegion));
                LogFile(actual, "Actual.png");

                // Load the reference/master data from a previously saved file
                Snapshot master = Snapshot.FromFile(Path.Combine(TestContext.TestDeploymentDir, "Master0.png"));
                LogFile(master, "Master.png");

                // Log the outcome of the test, only on failure to save disk space.
                // For test stability in scenarios of varying window styles, consider:
                //  -cropping the capture region to eliminate the border rectangle
                //  -Testing on a well controlled test environment
                if (CompareImages(actual, master) == VerificationResult.Fail)
                {
                    Assert.Fail("Initial State test failed. Actual should look like Master image. Refer to logged images under:" + TestContext.TestLogsDir);
                }
            }
            finally
            {
                AutomationHelpers.CloseWindow(window);
                driver.Join();
            }
        }
コード例 #7
0
        public void FaultAppendText()
        {
            //This is a work-around, Xunit moves the reference dll's at run time so we need to call register
            //from the test, instead of TestApiCore.
            string processorArch = DetectProccessorArchitecture();

            ComRegistrar.Register(@".\FaultInjectionEngine\" + processorArch + @"\FaultInjectionEngine.dll");

            string sampleAppPath = "SampleApp.exe";

            //Create the FaultRule and FaultSession
            FaultRule rule = new FaultRule(
                "SampleApp.Window1.Append(string, string)",
                BuiltInConditions.TriggerOnEveryCall,
                BuiltInFaults.ReturnValueFault(""));

            FaultSession session = new FaultSession(rule);

            //Start the app
            ProcessStartInfo        psi     = session.GetProcessStartInfo(sampleAppPath);
            OutOfProcessApplication testApp = new OutOfProcessApplication(
                new OutOfProcessApplicationSettings
            {
                ProcessStartInfo = psi,
                ApplicationImplementationFactory = new UIAutomationOutOfProcessApplicationFactory()
            });

            testApp.Start();


            try
            {
                testApp.WaitForMainWindow(TimeSpan.FromSeconds(15));

                // Discover various elements in the UI
                AutomationElement mainWindowElement = (AutomationElement)testApp.MainWindow;
                AutomationElement inputTextBox      = AutomationUtilities.FindElementsById(mainWindowElement, "inputTextBox")[0];
                AutomationElement outputTextBox     = AutomationUtilities.FindElementsById(mainWindowElement, "outputTextBox")[0];
                AutomationElement appendButton      = AutomationUtilities.FindElementsById(mainWindowElement, "appendButton")[0];

                // Click on the input text box and simulate typing
                string inputText    = "TestTest";
                string expectedText = ""; //expected text should be nothing since we intercept the Append method

                WindowPattern winPattern = mainWindowElement.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
                Helpers.MoveToAndClick(inputTextBox);
                winPattern.WaitForInputIdle(inputWaitTime);
                Microsoft.Test.Input.Keyboard.Type(inputText);
                winPattern.WaitForInputIdle(inputWaitTime);

                // Now click the button
                Helpers.MoveToAndClick(appendButton);
                winPattern.WaitForInputIdle(inputWaitTime);

                // Now, get the text of the outputTextBox and compare it against what's expected
                // Fail the test if expected != actual
                TextPattern textPattern = outputTextBox.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                string      actualText  = textPattern.DocumentRange.GetText(-1);

                // Report the test result
                Assert.Equal <string>(expectedText, actualText);
            }
            finally
            {
                // Close the tested application
                testApp.Close();
            }
        }