0

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)

Andy Ye
  • 1
  • 2

3 Answers3

0

As In a group I assume that test run consecutively in one chrome session. Probably your test end up and next one expect another page. Just ensure you navigate always to right place at beggining- try to use setup and tearDown attributes.

If you operate on data first test could leave mess on the page- then add clearing methods

0

Stale element exceptions means your locating element is there but your driver instance could not interest with that ,there are 2 ways to resolve this

1.By using refresh method in selenium 2.By for loop you can resolve(looping until element is click)

Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

Added clean table method
_contactsTableContext.Clear();

Andy Ye
  • 1
  • 2