/// <summary> /// Gets the Method Info from the derived class methods which is having the test number /// </summary> /// <param name="testNumber">test case number</param> /// <param name="testDetails">If the method is found the corresponding method test details will be extracted</param> /// <returns></returns> private MethodInfo GetTestMethod(int testNumber, ref TestDetailsAttribute testDetails) { // walk thru all the methods and return the method which is having the test number as user requested foreach (MethodInfo method in GetType().GetMethods()) { object[] attrs = method.GetCustomAttributes(new TestDetailsAttribute().GetType(), false); if (attrs.Length > 0) { // since test methods are having only the TestDetails type custom attributes, so we can cast to this type testDetails = (TestDetailsAttribute)attrs[0]; if (testDetails.Id == testNumber) { return(method); } } } return(null); }
/// <summary> /// Runs the test from the derived class which has the test number /// </summary> /// <param name="executionData">Plugin execution data</param> /// <param name="testNumber">test case number to be executed</param> /// <param name="ipAddress">IP Address of Printer</param> /// <param name="productFamily"><see cref=" ProductFamilies"/></param> public void RunTest(PluginExecutionData executionData, int testNumber, IPAddress ipAddress, ProductFamilies productFamily) { bool previousResult; _executedTests.TryGetValue(testNumber, out previousResult); // Run only if the test is failed in the previous run if (!previousResult) { TestDetailsAttribute testDetails = null; DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now; bool result = false; try { // extract the method from the current executing assembly and class MethodInfo info = GetTestMethod(testNumber, ref testDetails); if (null != info) { TraceFactory.Logger.Info(START_TAG + testNumber); _firmwareVersion = GetPrinterFirmwareVersion(ipAddress, productFamily); // get the start time startTime = DateTime.Now; UpdateStatus("Test {0} started".FormatWith(testNumber)); result = (bool)info.Invoke(this, null); UpdateStatus("Test {0} completed".FormatWith(testNumber)); // get the end time endTime = DateTime.Now; } } catch (Exception ex) { // Normally, we do not like catching all exceptions, but in this situation we don't know // what kind of exception will be thrown by the plug-ins. TraceFactory.Logger.Info("Test case thrown exception : " + testNumber); TraceFactory.Logger.Info("Error Information : " + ex.Message); TraceFactory.Logger.Debug(ex.InnerException?.StackTrace == null ? string.Empty : ex.InnerException.StackTrace); endTime = DateTime.Now; result = false; } finally { TraceFactory.Logger.Info(END_TAG + testNumber); TraceFactory.Logger.Debug("Test result: {0}".FormatWith(result ? "Passed" : "Failed")); // get log data from the log file. var logData = GetLogData(testNumber, executionData.SessionId); _executedTests.Remove(testNumber); _executedTests.Add(testNumber, result); // Tests for Wired and Wireless remain same for Print and IPconfiguratio plug-in; hence based on 'NetworkConnectivity' selection on plug-in, wired/ wireless mode is updated on the reports. // For other plug-in's where NetworkConnectivity is not set, wired/ wireless mode is taken from the 'TestDetailsAttribute' if (NetworkConnectivity == ConnectivityType.None) { // save the report after every test run, this will enable to the live report data SaveReport(executionData, testNumber, testDetails.Category, testDetails.Description, startTime, endTime, result, logData, testDetails.Protocol.ToString(), testDetails.Connectivity.ToString(), _firmwareVersion, testDetails.PrintProtocol.ToString()); } else { SaveReport(executionData, testNumber, testDetails.Category, testDetails.Description, startTime, endTime, result, logData, testDetails.Protocol.ToString(), NetworkConnectivity.ToString(), _firmwareVersion, testDetails.PrintProtocol.ToString()); } } } }
/// <summary> /// Loads the test case from the current assembly using the reflection /// Read the attributes like id, category, description. /// </summary> public bool LoadTestCases(Type type, Collection <int> selectedTests = null, PluginType plugin = PluginType.Default, Collection <PrintTestData> testDurationDetails = null) { if (!plugin.Equals(PluginType.Default)) { _pluginType = plugin; } _type = type; PrintTestData Testdata = null; selectAll_CheckBox.Checked = false; if (plugin == PluginType.Print) { // Hide and show the columns based on the plug-in type durationDataGridViewTextBoxColumn.Visible = true; connectivityDataGridViewTextBoxColumn.Visible = false; printProtocolDataGridViewTextBoxColumn.Visible = false; portNumberDataGridViewTextBoxColumn.Visible = false; testCaseDetails_DataGrid.ScrollBars = ScrollBars.Vertical; } if (plugin == PluginType.IPConfiguration) { connectivityDataGridViewTextBoxColumn.Visible = false; } if (null == type || null == productCategory_ComboBox.SelectedItem) { return(false); } ProductFamilies selectedCategory = (ProductFamilies)Enum.Parse(typeof(ProductFamilies), productCategory_ComboBox.SelectedItem.ToString()); // clear the previous rows before adding any new rows cTCTestCaseDetails.TestCaseDetails.Clear(); // walk thru all the methods inside the class and check if the method has TestDetails // then add to the data set. foreach (MethodInfo methodInfo in type.GetMethods()) { object[] attrs = methodInfo.GetCustomAttributes(new TestDetailsAttribute().GetType(), false); if (attrs.Length > 0) { // since we are having only the TestDetails type custom attributes so we can cast to this type TestDetailsAttribute details = (TestDetailsAttribute)attrs[0]; if (details.ProductCategory.HasFlag(selectedCategory)) { // create the row data CTCTestCaseDetailsDataSet.TestCaseDetailsRow row = cTCTestCaseDetails.TestCaseDetails.NewTestCaseDetailsRow(); row.IsSelected = selectedTests != null && selectedTests.Contains(details.Id); if (plugin.Equals(PluginType.Print) || _pluginType.Equals(PluginType.Print)) { bool durationAssigned = false; uint duration = 0; if (testDurationDetails != null && testDurationDetails.Count > 0) { var res = from data in testDurationDetails where data.TestId.Equals(details.Id) select new { Duration = data.Duration }; foreach (var item in res) { duration = Convert.ToUInt32(item.Duration); row.Duration = duration; durationAssigned = true; break; } } if (!durationAssigned) { row.Duration = details.PrintDuration; } if (details.PrintDuration > 0)//row.IsSelected && { Testdata = new PrintTestData(); Testdata.TestId = (int)details.Id; Testdata.Duration = (int)details.PrintDuration; _TestWithDuration.Add(Testdata); } row.PrintProtocol = details.PrintProtocol.ToString(); row.PortNumber = details.PortNumber; } row.Category = details.Category; row.ID = (uint)details.Id; row.Description = details.Description; row.Protocol = details.Protocol.ToString(); row.Connectivity = details.Connectivity.ToString(); cTCTestCaseDetails.TestCaseDetails.AddTestCaseDetailsRow(row); } } } return(true); }