public ActionResult Index(HttpPostedFileBase files) { if (files == null) { return new EmptyResult(); } if (string.IsNullOrEmpty(files.FileName)) { return new EmptyResult(); } var allowedTypes = new[] { ".xlsx" }; var extension = Path.GetExtension(files.FileName).ToLower(); if (files.ContentLength > 0 && allowedTypes.Contains(extension)) { using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(files.InputStream)) { var testPages = new List<TestPage>(); excelReader.IsFirstRowAsColumnNames = true; DataSet result = excelReader.AsDataSet(); for (int i = 0; i < result.Tables.Count; i++) { var headers = result.Tables[i].Columns; if (headers.Count == 0) { break; } if (!headers[0].ColumnName.ToLowerInvariant().Equals("url")) { break; } foreach (DataRow row in result.Tables[i].Rows) { var testPage = new TestPage() { URL = row[0].ToString() }; for (int a = 1; a < row.ItemArray.Length; a++) { testPage.ElementList.Add(new Element { Name = headers[a].ToString(), ExpectedValue = row[a].ToString().Trim() }); } testPages.Add(testPage); } } TestPagesService.Process(testPages); } } return View(); }
private void selectFileButton_Click(object sender, RoutedEventArgs e) { // Open file dialog var openFileDialog = new OpenFileDialog() { Filter = "Excel files (*.xlsx)|*.xlsx" }; if (openFileDialog.ShowDialog() == true) { // Set label with file name selectedFileLabel.Content = openFileDialog.FileName; using (var file = new StreamReader(openFileDialog.FileName)) { // Check file size and extension var extension = System.IO.Path.GetExtension(openFileDialog.FileName).ToLower(); if (file.BaseStream.Length > 0 && allowedTypes.Contains(extension)) { // Read excel file using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(file.BaseStream)) { var testPages = new List<TestPage>(); excelReader.IsFirstRowAsColumnNames = true; DataSet result = excelReader.AsDataSet(); for (int i = 0; i < result.Tables.Count; i++) { var headers = result.Tables[i].Columns; if (headers.Count == 0) { break; } if (!headers[0].ColumnName.ToLowerInvariant().Equals("url")) { break; } foreach (DataRow row in result.Tables[i].Rows) { var testPage = new TestPage() { URL = row[0].ToString() }; for (int a = 1; a < row.ItemArray.Length; a++) { testPage.ElementList.Add(new Element { Name = headers[a].ToString(), ExpectedValue = row[a].ToString().Trim() }); } testPages.Add(testPage); } } TestPagesService.Process(testPages, _model.TestResults, testingProgress); } } } } }
private static void ProcessTestPage(TestPage testPage) { try { // Create page load (async) CQ.CreateFromUrlAsync(testPage.URL, responseSuccess => { var testResult = new TestResult() { URL = testPage.URL }; // Fix "stream close" var doc = responseSuccess.Dom; foreach (var element in testPage.ElementList) { // Check if element is supported if (_checkSupportedElements && !_supportedElements.Contains(element.Name.ToLowerInvariant())) { testResult.Cases.Add(new TestCase(element) {Unsupported = true}); } else { // Select element var foundElement = doc.Select(element.Name); if (foundElement != null && foundElement.Contents().Any()) { // Found, check if meets expected value var currentValue = foundElement.Contents()[0].NodeValue.Trim(); // Compare current value to expected // Note: Empty expected values are considered valid if (string.IsNullOrEmpty(element.ExpectedValue) || currentValue.Equals(element.ExpectedValue)) { System.Diagnostics.Debug.WriteLine("Valid"); } else { System.Diagnostics.Debug.WriteLine("Invalid, expected {0} received {1}", currentValue, element.ExpectedValue); testResult.Cases.Add(new TestCase(element) { ActualValue = currentValue }); } } } } // Prevent - "collection was modified enumeration operation may not execute" error. Syncs collection before modifying lock (((ICollection)TestResultsHub.GetTestResults().Results).SyncRoot) { TestResultsHub.GetTestResults().Results.Add(testResult); TestResultsHub.Instance.GetResults(); } }, responseFail => { // TODO }); } catch (UriFormatException) { System.Diagnostics.Debug.WriteLine("Invalid URL"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error: {0}", e.Message); } }
private static void ProcessTestPage(TestPage testPage, ObservableCollection<TestResult> testResults, ProgressBar testingProgress) { try { // Create page load (async) CQ.CreateFromUrlAsync(testPage.URL, responseSuccess => { var testResult = new TestResult() { URL = testPage.URL }; // Fix "stream close" var doc = responseSuccess.Dom; foreach (var element in testPage.ElementList) { // Check if element is supported if (_checkSupportedElements && !_supportedElements.Contains(element.Name.ToLowerInvariant())) { testResult.Cases.Add(new TestCase(element) { Unsupported = true }); } else { // Select element var foundElement = doc.Select(element.Name); if (foundElement != null && foundElement.Contents().Any()) { // Found, check if meets expected value var currentValue = foundElement.Contents()[0].NodeValue.Trim(); // Compare current value to expected // Note: Empty expected values are considered valid if (string.IsNullOrEmpty(element.ExpectedValue) || currentValue.Equals(element.ExpectedValue)) { System.Diagnostics.Debug.WriteLine("Valid"); } else { System.Diagnostics.Debug.WriteLine("Invalid, expected {0} received {1}", currentValue, element.ExpectedValue); testResult.Cases.Add(new TestCase(element) { ActualValue = currentValue }); } } } } // Set test icons based on results testResult.Icon = new TestResultIcon( testResult.Cases.Any() ? TestResultIcon.TestResultState.FAIL : TestResultIcon.TestResultState.PASS ); // Add results (invoke main ui thread) System.Windows.Application.Current.Dispatcher.Invoke(() => { testResults.Add(testResult); DoubleAnimation doubleanimation = new DoubleAnimation(testingProgress.Value + 100, new Duration(TimeSpan.FromSeconds(1))); testingProgress.BeginAnimation(System.Windows.Controls.Primitives.RangeBase.ValueProperty, doubleanimation); }); }, responseFail => { // TODO }); } catch (UriFormatException) { System.Diagnostics.Debug.WriteLine("Invalid URL"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error: {0}", e.Message); } }