Search code examples
c#seleniumatata

Atata - Unable to locate element, using Table<> class


When I trying to refer to some element in table, using Table<> class, I get this error:

Message: OpenQA.Selenium.NoSuchElementException : Unable to locate element: By.XPath: .//td[1]/descendant-or-self::a
Context element:
Tag: tr
Location: {X=62,Y=273}
Size: {Width=1140, Height=37}
Text: Order Date User Address Origin Address Destination My Reference POD Status

A table source:

<table class="table table-striped">
    <tr class="text-nowrap">
        <th>Order</th>
        <th>Date</th>
        <th>Customer</th>
        <th>User</th>
        <th>Address Origin</th>
        <th>Address Destination</th>
        <th>My Reference</th>
        <th>POD</th>
        <th>Status</th>
    </tr>
        <tr>
            <td class="text-nowrap">
                <a href="/Customer/Request/Show/180305-NQHHGU">180305-NQHHGU</a>
            </td>
            <td>05.03.2018</td>
            <td>Merchant Advance (2M7)</td>
            <td>Barry Manilow</td>
            <td>757 RUE GUY MONTREAL</td>
            <td>242 LAVERENDRYE AVE CHURCHILL</td>
            <td></td>
            <td>
            </td>
            <td class="text-nowrap">…</td>
        </tr>

Page object source:

public class OrdersPage : BasePage<_>
{
    public Table<OrdersTableRow, _> Orders { get; private set; }

    public class OrdersTableRow : TableRow<_>
    {
        [FindByColumnHeader("Order")]
        public LinkDelegate<ShipmentOrderPage, _> Order { get; private set; }

        public Date<_> Date { get; private set; }

        public Text<_> Customer { get; private set; }

        public Text<_> User { get; private set; }
        …
        …
    }
}

And I'm trying to do something like that in test:

    Go.To<OrdersPage>().
        Orders.Rows[x => x.Order.Content().Value == order.OrderNumber].Order();

I think it's about my table haven't <thead> tag. Have any Idea?


Solution

  • You are right. Out of the box Table control works by default with <table> that contains <th> elements inside thead/tr. Such row is skipped when Atata handles regular/data rows.

    You can check that TableRow class contains the following control definition:

    [ControlDefinition("tr[parent::table or parent::tbody]", ComponentTypeName = "row")]
    

    In your case the first row with headers was considered like a regular row, and Atata tried to find a link in this row, which is missing there.

    But in Atata you can reconfigure such things easily. Just overwrite [ControlDefinition] of OrdersTableRow class as follows:

    [ControlDefinition("tr[td]", ComponentTypeName = "row")]
    public class OrdersTableRow : TableRow<_>
    {
    //...
    }
    

    This way Orders.Rows will process only <tr> elements that have <td> element inside, skipping the first row.