protected override string[] GetTestList() { // Get the variable we're testing against ScenarioVariable <string[]> testListVar = Scenario.Lists[_variable]; if (testListVar == null) { Log.Error("Could not find variable \"" + _variable + "\""); return(null); } // Filter the test list down to what the user wants us to find List <string> testList = new List <string>(); if (_verifyItems.Length != 0) { foreach (int index in _verifyItems) { if (index >= _verifyItems.Length) { Log.Error("Verify Items refers to an invalid item", "The Verify Items property refers to an item that's not in the source list."); return(null); } testList.Add(testListVar.Value[index]); } } return(testList.ToArray()); }
public override void Body() { Log log = Log.Current; ScenarioVariable <DataTable> table = Scenario.Tables[_tableName]; if (table == null) { throw new ArgumentException("Table '" + _tableName + "' is not recognized"); } DataTable dataTable = table.Value as DataTable; int j = 0; foreach (DataRow row in dataTable.Rows) { StringBuilder extra = new StringBuilder(); extra.AppendLine("Variables:"); for (int i = 0; i < dataTable.Columns.Count; i++) { string name = dataTable.Columns[i].ColumnName; if (_prefixTableName) { name = _tableName + "." + name; } Scenario.Variables.AddOrReplace(new QAliber.TestModel.Variables.ScenarioVariable <string>(name, row[i].ToString(), this)); extra.AppendFormat(" {0}: {1}", name, row[i].ToString()).AppendLine(); } if (log != null) { log.StartFolder("Iteration on row '" + j + "'", extra.ToString()); } base.Body(); if (log != null) { log.EndFolder(); } if (exitTotally) { break; } if (branchesToBreak > 0) { branchesToBreak--; break; } j++; } }
public string[] GetVerifyColumnsOptions() { ScenarioVariable <DataTable> testTableVar = Scenario.Tables[_variable]; if (testTableVar == null) { return(new string[0]); } return(testTableVar.Value.Columns.Cast <DataColumn>().Select(col => col.ColumnName).ToArray()); }
public string[] GetVerifyItemsOptions() { ScenarioVariable <string[]> testListVar = Scenario.Lists[_variable]; if (testListVar == null) { return(new string[0]); } return(testListVar.Value); }
public string[] GetVerifyRowsOptions() { ScenarioVariable <DataTable> testTableVar = Scenario.Tables[_variable]; if (testTableVar == null) { return(new string[0]); } return(testTableVar.Value.Rows.Cast <DataRow>().Select(row => string.Join(", ", row.ItemArray.Select(item => (item as string) ?? string.Empty))).ToArray()); }
private void TryToUpdateValueLabel() { try { if (scenario.Variables[varTextBox.Text] != null) { curValDynLabel.Text = scenario.Variables[varTextBox.Text].Value.ToString(); return; } Regex listRegex = new Regex(@"([^\[]+)\[([0-9]+)\]"); Regex tableRegex = new Regex(@"([^\[]+)\[([0-9]+),([0-9]+)\]"); Match match = listRegex.Match(varTextBox.Text); if (match.Success) { ScenarioVariable <string[]> list = scenario.Lists[match.Groups[1].Value]; if (list != null) { int index = int.Parse(match.Groups[2].Value); string[] col = list.Value as string[]; curValDynLabel.Text = col[index]; return; } } match = tableRegex.Match(varTextBox.Text); if (match.Success) { ScenarioVariable <DataTable> table = scenario.Tables[match.Groups[1].Value]; if (table != null) { int rowIndex = int.Parse(match.Groups[2].Value); int colIndex = int.Parse(match.Groups[3].Value); DataTable dataTable = table.Value as DataTable; curValDynLabel.Text = dataTable.Rows[rowIndex][colIndex].ToString(); return; } } curValDynLabel.Text = ""; } catch (Exception ex) { curValDynLabel.Text = ex.Message; } }
public override void Body() { Log log = Log.Current; ScenarioVariable <string[]> list = Scenario.Lists[listName]; if (list == null) { throw new ArgumentException("List '" + listName + "' is not recognized"); } string[] vals = list.Value; foreach (string obj in vals) { Scenario.Variables.AddOrReplace(new QAliber.TestModel.Variables.ScenarioVariable <string>(listName + ".Current", obj, this)); if (log != null) { log.StartFolder("Iteration on item '" + obj.ToString() + "'", null); } base.Body(); if (log != null) { log.EndFolder(); } if (exitTotally) { break; } if (branchesToBreak > 0) { branchesToBreak--; break; } } }
private string ReplaceTableForMatch(Match match) { string key = match.Groups[1].Value; ScenarioVariable <DataTable> table = tables[key]; if (table != null) { DataTable dataTable = table.Value as DataTable; if (dataTable != null) { int rowIndex = dataTable.Rows.Count, colIndex = dataTable.Columns.Count; if (int.TryParse(match.Groups[2].Value, out rowIndex) && int.TryParse(match.Groups[3].Value, out colIndex)) { if (rowIndex < dataTable.Rows.Count && colIndex < dataTable.Columns.Count) { return(dataTable.Rows[rowIndex][colIndex].ToString()); } } } } return(match.Value); }
public override void Body() { ActualResult = QAliber.RemotingModel.TestCaseResult.Failed; _resultTable = null; // Get the variable we're testing against ScenarioVariable <DataTable> testTableVar = Scenario.Tables[_variable]; if (testTableVar == null) { Log.Error("Could not find variable \"" + _variable + "\""); return; } // Filter the test table down to what the user wants us to find DataTable testTable = testTableVar.Value.Clone(); if (_verifyRows.Length == 0) { // Grab all rows testTable.Merge(testTableVar.Value); } else { foreach (int rowIndex in _verifyRows) { if (rowIndex >= testTableVar.Value.Rows.Count) { Log.Error("Verify Rows refers to an invalid row", "The Verify Rows property refers to a row that's not in the source table."); return; } testTable.Rows.Add(testTableVar.Value.Rows[rowIndex].ItemArray); } } if (_verifyColumns.Length != 0) { HashSet <string> verifyColumns = new HashSet <string>(_verifyColumns); string[] verifyColumnsNotFound = verifyColumns.Except(testTable.Columns.Cast <DataColumn>().Select(col => col.ColumnName)).ToArray(); if (verifyColumnsNotFound.Length != 0) { Log.Error("Verify Columns refers to an invalid column", "The Verify Columns property refers to one or more invalid columns: " + string.Join(", ", verifyColumnsNotFound)); return; } foreach (DataColumn column in testTable.Columns.Cast <DataColumn>().ToArray()) { if (!verifyColumns.Contains(column.ColumnName)) { testTable.Columns.Remove(column); } } } // Capture the grid UIControlBase c = UIControlBase.FindControlByPath(_control); if (c == null || !c.Exists) { Log.Error("Control not found", _control); return; } IGridPattern grid = c.GetControlInterface <IGridPattern>(); if (grid == null) { Log.Error("Can't capture grid", "Couldn't find an appropriate way to get at grid data."); return; } string[] headers; string[][] rows = grid.CaptureGrid(out headers); if (_requireHeaders && headers == null) { LogFailedByExpectedResult("No headers in target grid", "Target grid didn't have any headers, and Require Headers was set."); return; } bool columnsUnique = true; if (headers != null && headers.Distinct().Count() != headers.Length) { columnsUnique = false; Log.Warning("Duplicate column names", "Could not produce an output table because there are two columns with the same name."); } // Create the grid result table if (columnsUnique) { DataTable table = new DataTable("Captured grid"); if (headers != null) { headers = Array.ConvertAll(headers, header => XPath.EscapeLiteral(header)); foreach (string header in headers) { table.Columns.Add(header, typeof(string)); } } else { int columnCount = grid.ColumnCount; for (int i = 0; i < columnCount; i++) { table.Columns.Add("Column " + i.ToString(CultureInfo.CurrentUICulture), typeof(string)); } } rows = Array.ConvertAll(rows, row => Array.ConvertAll(row, cell => XPath.EscapeLiteral(cell ?? string.Empty))); foreach (string[] row in rows) { table.Rows.Add(row); } _resultTable = table; } // Go ahead and log the capture string rowText = string.Join("\r\n", rows.Select(row => "[" + string.Join(", ", row) + "]")); if (headers != null) { rowText = "[" + string.Join(", ", headers) + "]\r\n" + rowText; } Log.Info("Captured grid", rowText); // Shortcut out on some special cases if (rows.Length < testTable.Rows.Count) { LogFailedByExpectedResult("Not enough rows in target grid", string.Format("Target grid had {0} rows, expected at least {1}.", rows.Length, testTable.Rows.Count)); return; } if (!_allowExtraRows && rows.Length > testTable.Rows.Count) { LogFailedByExpectedResult("Too many rows in target grid", string.Format("Target grid had {0} rows, expected {1}.", rows.Length, testTable.Rows.Count)); return; } if (rows.Length != 0) { if (rows[0].Length < testTable.Columns.Count) { LogFailedByExpectedResult("Not enough columns in target grid", string.Format("Target grid had {0} columns, expected at least {1}.", rows[0].Length, testTable.Columns.Count)); return; } if (!_allowExtraColumns && rows[0].Length > testTable.Columns.Count) { LogFailedByExpectedResult("Too many columns in target grid", string.Format("Target grid had {0} columns, expected {1}.", rows[0].Length, testTable.Columns.Count)); return; } } // Filter out any columns we don't care about if (headers != null) { // Filter by column name List <string> testColumns = testTable.Columns.Cast <DataColumn>() .Select(col => col.ColumnName).ToList(); int[] keptColumns = new int[testColumns.Count]; for (int index = 0; index < headers.Length; index++) { int foundIndex = testColumns.IndexOf(headers[index]); if (foundIndex != -1) { testColumns[foundIndex] = null; keptColumns[foundIndex] = index; } else if (!_allowExtraColumns) { LogFailedByExpectedResult("Found extra column \"" + headers[index] + "\" in grid", string.Empty); return; } } // keptColumns as created will reorder the columns into the expected order; // if the user wants us to verify the column order as well, we have to re-sort // into the original order as found in the target grid if (_verifyColumnOrder) { Array.Sort <int>(keptColumns); } headers = keptColumns.Select(i => headers[i]).ToArray(); rows = rows.Select(row => keptColumns.Select(i => row[i]).ToArray()).ToArray(); } // Finally, verify rows if (_verifyRowOrder) { List <HashedRow> extraRows = new List <HashedRow>(); HashedRow[] targetRows = rows.Select(row => new HashedRow(row)).ToArray(); var testRows = testTable.Rows.Cast <DataRow>() .Select(row => new HashedRow(row.ItemArray.Cast <string>().ToArray())); int targetRowIndex = 0; foreach (var testRow in testRows) { // We must find this row in the target to proceed bool matched = false; int expectedPosition = targetRowIndex; while (targetRowIndex < targetRows.Length) { if (targetRows[targetRowIndex].Equals(testRow)) { // Row found, stop here targetRowIndex++; matched = true; break; } // Target row which does not match extraRows.Add(targetRows[targetRowIndex]); targetRowIndex++; } if (matched) { continue; } // Made it to the end of the target rows without finding it if (extraRows.Contains(testRow)) { LogFailedByExpectedResult("Row in wrong place", "This row was in the wrong position while \"Verify Row Order\" was on: [" + string.Join(", ", testRow) + "]\r\n\r\nIt was expected at or after row " + (expectedPosition + 1).ToString() + "."); } else { LogFailedByExpectedResult("Expected row missing", "Could not find this row in the target grid: [" + string.Join(", ", testRow) + "]\r\n\r\nIt was expected at or after row " + (expectedPosition + 1).ToString() + "."); if (extraRows.Count != 0 && !_allowExtraRows) { LogFailedByExpectedResult("Found extra row(s)", "Found these rows in the target grid while \"Allow Extra Rows\" was off:\r\n\r\n" + string.Join("\r\n", extraRows.Select(row => "[" + string.Join(", ", row) + "]"))); } } return; } if (extraRows.Count != 0 && !_allowExtraRows) { LogFailedByExpectedResult("Found extra row(s)", "Found these rows in the target grid while \"Allow Extra Rows\" was off:\r\n\r\n" + string.Join("\r\n", extraRows.Select(row => "[" + string.Join(", ", row) + "]"))); return; } } else { // Unordered find; make sure there are the right number of each kind of row List <HashedRow> missingRows = new List <HashedRow>(); List <HashedRow> targetRows = rows.Select(row => new HashedRow(row)).ToList(); var testRows = testTable.Rows.Cast <DataRow>() .Select(row => new HashedRow(row.ItemArray.Cast <string>().ToArray())); foreach (var testRow in testRows) { int index = targetRows.IndexOf(testRow); if (index == -1) { missingRows.Add(testRow); } else { targetRows.RemoveAt(index); } } if (missingRows.Count != 0) { LogFailedByExpectedResult("Expected row(s) missing", "These rows were expected but not found in the target grid:\r\n\r\n" + string.Join("\r\n", missingRows.Select(row => "[" + string.Join(", ", row) + "]"))); return; } // Make sure there are none left if we aren't supposed to allow extra rows if (!_allowExtraRows && targetRows.Count != 0) { LogFailedByExpectedResult("Found extra row(s)", "Found these rows in the target grid while \"Allow Extra Rows\" was off:\r\n\r\n" + string.Join("\r\n", targetRows.Select(row => "[" + string.Join(", ", row) + "]"))); return; } } ActualResult = QAliber.RemotingModel.TestCaseResult.Passed; }
protected virtual void SetVariables() { if (_scenario != null) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this); if (_outputProperties != null) { PropertyDescriptor foundProp = null; foreach (string name in _outputProperties.Keys) { foundProp = props.Find(name, false); string variableName = _outputProperties[name]; if (foundProp != null) { object val = foundProp.GetValue(this); DataTable table = val as DataTable; ICollection list = val as ICollection; if (table != null) { ScenarioVariable <DataTable> tableVar = _scenario.Tables[variableName]; if (tableVar != null) { tableVar.Value = table; } else { _scenario.Tables.AddOrReplace(new ScenarioTable(variableName, table, this)); } } else if (list != null) { string[] stringList = list.Cast <object>().Select( obj => (obj == null) ? "(null)" : obj.ToString()).ToArray(); ScenarioVariable <string[]> l = _scenario.Lists[variableName]; if (l != null) { l.Value = stringList; } else { _scenario.Lists.AddOrReplace(new ScenarioVariable <string[]>(variableName, stringList, this)); } } else if (val != null) { ScenarioVariable <string> v = _scenario.Variables[variableName]; if (v != null) { v.Value = val.ToString(); } else { _scenario.Variables.AddOrReplace(new ScenarioVariable <string>(variableName, val.ToString(), this)); } } } } } } }