我有很多例子的specflow测试,所以如果我单独运行测试都通过了。但是当我将它们作为一个组运行时,只有第一次通过,其他失败并出现 OpenQA.Selenium.StaleElementReferenceException : stale element reference: element is not attach to the page document
场景是通过单元格中的数据查找第一行并通过单击带有菜单图标的单元格来验证菜单中的项目这是我用来执行此类场景的类。
public class ContactTable
{
private static readonly By ActionMenu = By.ClassName("inline-action-dialog");
static List<ContactTableContext> _сontactTableContext = new List<ContactTableContext>();
public static IWebElement EditActionButton { get; set; }
public static IWebElement DeleteActionButton { get; set; }
public static void ReadTable(IWebElement table)
{
var columns = table.FindElements(By.TagName("th"));
var rows = table.FindElements(By.TagName("tr"));
int rowIndex = 0;
foreach (var row in rows)
{
int columnIndex = 0;
var columnDatas = row.FindElements(By.TagName("td"));
foreach (var columnValue in columnDatas)
{
_сontactTableContext.Add(new ContactTableContext
{
RowNumber = rowIndex,
ColumnName = columns[columnIndex].Text != "" ?
columns[columnIndex].Text : columnIndex.ToString(),
ColumnValue = columnValue.Text,
ColSpecValues = columnValue.Text != "" ? null :
columnValue.FindElements(ActionMenu),
});
columnIndex++;
}
rowIndex++;
}
}
public static void PerformActionOnCell(string columnIndex, string refColunmName, string refColunmValue)
{
var row = _сontactTableContext.FirstOrDefault(table => table.ColumnName == refColunmName && table.ColumnValue == refColunmValue);
var rowNumber = row.RowNumber;
var cell = (from e in _reviewContactTableContext
where e.ColumnName == columnIndex && e.RowNumber == rowNumber && e.ColSpecValues != null
select e.ColSpecValues).FirstOrDefault();
var currenCell = cell?.First();
currenCell.Click();
GetActionMenu(currenCell);
}
public static void GetMenu(IWebElement cell)
{
var actionPanel = cell.FindElement(By.TagName("ul"));
if (actionPanel.Displayed)
{
var actionList = actionPanel.FindElements(By.TagName("span"));
EditButton = actionList.FirstOrDefault();
DeleteButton = actionList.LastOrDefault();
}
}
可能是什么问题呢?我是编写此类自动测试的新手,所以不要过多地责骂代码)