Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string functionName = null;
            uint   port         = 0;
            int    numFailed    = 0;

            // Initialize log file
            logFile = new System.IO.StreamWriter(GetWorkingDirectory() + "log.txt");
            logFile.WriteLine("QXDM Automation Test Log");

            // Parse arguments
            waitForInputToExit = true;
            switch (args.Length)
            {
            case 0:
                ExitWithFail("Tried to run with improper syntax. Syntax is: QXDMAutomation <functionname> {port #} {nowait}", ExitCode.AUTOMATION_SETUP_ERROR);
                break;

            case 1:
                functionName = args[0];
                break;

            // 2nd argument is ambiguous (port or "nowait"?) so check by type
            case 2:
                functionName = args[0];

                uint tempPort = 0;
                if (uint.TryParse(args[1], out tempPort))
                {
                    port = tempPort;
                }
                else if (args[1] == waitForInput_arg)
                {
                    waitForInputToExit = false;
                }

                break;

            default:
                functionName = args[0];
                uint.TryParse(args[1], out port);
                if (args[2] == waitForInput_arg)
                {
                    waitForInputToExit = false;
                }
                break;
            }

            if (InitializeTestFunctions() == false)
            {
                ExitWithFail("Could not initialize test functions.", ExitCode.AUTOMATION_SETUP_ERROR);
            }

            QXDMAutoApplication qxdmApplication = null;

            try
            {
                qxdmApplication = (QXDMAutoApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("QXDM.QXDMAutoApplication");
            }
            catch (COMException e)
            {
                if (e.HResult == -2147221021)                                           // Running object not found
                {
                    qxdmApplication = new QXDMAutoApplication();                        // Attempt to start it
                }
                if (qxdmApplication == null)
                {
                    ExitWithFail("Error Starting QXDM", ExitCode.AUTOMATION_SETUP_ERROR);
                }
            }

            if (qxdmApplication != null)
            {
                Console.WriteLine("Interface Version: {0}", qxdmApplication.Get_Automation_Version());
                AutomationWindow automationWindow = qxdmApplication.GetAutomationWindow();
                automationWindow.SetVisible(true);

                Console.WriteLine("QXDM Version: {0}", automationWindow.GetQXDMVersion());

                QXDMAutomation.TestCase testCase = null;

                if (functionName.ToLower() == runall)                 // Running all test cases
                {
                    TestFunctionsIterator iterator = gTestFunctions.GetEnumerator();
                    while (iterator.MoveNext() == true)
                    {
                        CurrTestFunction current  = iterator.Current;
                        string           testName = current.Key;
                        testCase = current.Value;

                        // If no port is defined, only run test cases that do not require a COM port
                        if (port != 0 || testCase._requiresComPort == false)
                        {
                            if (RunTestCase(testCase, testName, automationWindow, port) == false)
                            {
                                numFailed++;
                            }
                        }
                    }
                }
                else if (gTestFunctions.TryGetValue(functionName.ToLower(), out testCase))                 // Run a single test case
                {
                    if (RunTestCase(testCase, functionName, automationWindow, port) == false)
                    {
                        numFailed++;
                    }
                }
                else
                {
                    LogEverywhere("Function " + functionName + " not found.");                     // Could not find test case
                    numFailed++;
                }

                automationWindow.Quit();

                // Exit with an error if any test cases failed.
                if (numFailed > 1)
                {
                    ExitWithFail("Two or more test cases failed.", ExitCode.MULT_TESTCASES_FAILED);
                }
                else if (numFailed == 1)
                {
                    ExitWithFail("A test case failed.", ExitCode.TESTCASE_FAILED);
                }

                LogEverywhere("\n. . .\n[SUCCESS] All test cases passed");

                if (waitForInputToExit)
                {
                    Console.Write("\n\nPress enter to exit.");
                    Console.Read();
                }
            }

            logFile.Close();
        }
Exemplo n.º 2
0
        static bool RunTestCase(QXDMAutomation.TestCase testCase, string functionName, AutomationWindow automationWindow, uint port)
        {
            bool usesComPort = testCase._requiresComPort;

            // Print to log
            string msg = "< RUNNING TEST CASE: " + functionName.ToUpper() + " >";

            LogEverywhere("");

            for (int i = 0; i < msg.Length; i++)
            {
                Console.Write("\\");
                logFile.Write("\\");
            }
            LogEverywhere("\n" + msg);

            string errorMessage   = "\t[ERROR] Test case \"" + functionName + "\" failed: ";
            string successMessage = "\t[PASS] Test case \"" + functionName + "\" succeeded";

            // Run setup function
            if (testCase._setup != null && testCase._setup(automationWindow) == false)
            {
                logFile.WriteLine(errorMessage + "Setup function failed");
                return(false);
            }

            // Set up COM stuff if needed
            if (usesComPort == true)
            {
                if (port != 0)
                {
                    if (automationWindow.SetComPort(port) == true)
                    {
                        if (automationWindow.GetServerState() == 2)
                        {
                            Console.WriteLine("Connected to port: {0}", port);
                        }
                        else
                        {
                            logFile.WriteLine(errorMessage + "Error connecting to port: " + port);
                            return(false);
                        }
                    }
                    else
                    {
                        logFile.WriteLine(errorMessage + "SetComPort Failed - port: " + port + " message: " + automationWindow.GetLastErrorString());
                        return(false);
                    }
                }
                else
                {
                    logFile.WriteLine(errorMessage + "Requires a connection");
                    return(false);
                }
            }

            // Run exec function
            if (testCase._exec != null && testCase._exec(automationWindow) == false)
            {
                logFile.WriteLine(errorMessage + "Exec function failed");
                return(false);
            }

            // Disconnect the port if in use
            if (usesComPort == true)
            {
                automationWindow.SetComPort(0);
            }

            // Run teardown (cleanup) function
            if (testCase._teardown != null && testCase._teardown(automationWindow) == false)
            {
                logFile.WriteLine(errorMessage + "Teardown function failed");
                return(false);
            }

            logFile.WriteLine(successMessage);
            return(true);
        }