I have specflow test with many examples, so if I run tests separately all pass. But when I run them as a group only first pass well, other fails with OpenQA.Selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document
Scenario is to find first row by data in cell and verify items in menu by clicking cell with menu icon Here is class that i use to perform such scenario.
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();
}
}
what could be the problem? I'm new to writing such autotests, so don't scold the code too much)
Added clean table method
_contactsTableContext.Clear();