Search code examples
c#atata

Atata - Unable to locate elements from table rows


Currently i am struggling to find an elements from a table row:

enter image description here

I am trying to locate it with:

detailPage.Users.Rows[x => x.Project == "Unassigned"].Project.Should.Equal("Unassigned");

I receive this error:

Atata.AssertionException: 'Invalid "Users" table's "Project == "Unassigned"" row's "Project" element content. Expected: should equal "Unassigned" Actual: null

This is the full table HTML:

<table class="table table-bordered table-hover table-responsive" id="js-fixed-table">
                <thead class="header-static" style="opacity: 1;">
                    <tr>
                        <th class="text-center vertical-align-middle column-fixed" rowspan="2" style="left: 0px; background: none;">Name</th>
                        <th class="text-center vertical-align-middle" rowspan="2">E-mail</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Start date</th>
                                                    <th class="text-center vertical-align-middle" rowspan="2">Location</th>
                        <th class="text-center vertical-align-middle" rowspan="2">City</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Company</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Work Category</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Position</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Expertise Level</th>
                                                    <th class="text-center vertical-align-middle" rowspan="2">Client</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Project</th>

                        <th class="text-center vertical-align-middle" rowspan="2">Project work (paid)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Project work (unpaid)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Overtime</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Holiday Hours</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Paid time off</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Unpaid time off</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Sick leave</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Business trip</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Special time off</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Paid time off (paid by client)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Total</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Official Company Working Hours</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Available Hours</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Availability</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Billable Hours</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Billability</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Rate (original currency)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Currency</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Exchange Rate</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Revenue (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Revenue Total (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Weighted AR (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Net Salary (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Total Employee Cost (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Additional Remuneration (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Direct Project Cost (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Direct Cost (BGN)</th>

                        <th class="text-center vertical-align-middle" rowspan="2">Gross Margin (BGN)</th>
                        <th class="text-center vertical-align-middle" rowspan="2">Gross Margin Percent (BGN)</th>
                    </tr>
                </thead>
                <tbody><tr style="color:" title="" class="missing-rate-row">
                    <td data-toggle="modal" data-target="#user-details-modal" data-id="160" class="vertical-align-middle cursor-pointer column-fixed" rowspan="2" style="left: 0px; background: none;">
                        Ivan Garlanov
                    </td>
                                            <td class="vertical-align-middle" rowspan="2">[email protected]</td>
                    <td class="vertical-align-middle" rowspan="2">2016-07-07</td>
                                            <td class="vertical-align-middle" rowspan="2">Nikola Tesla</td>
                    <td class="vertical-align-middle" rowspan="2">Nis</td>

                    <td class="vertical-align-middle" rowspan="2">OneStar</td>
                    <td class="vertical-align-middle" rowspan="2">Direct</td>
                    <td class="vertical-align-middle" rowspan="2">Admin Operations</td>
                    <td class="vertical-align-middle" rowspan="2">Architect</td>

                    <td class="vertical-align-middle">PrismaSoft</td>
                    <td class="vertical-align-middle">BioFruit</td>

                    <td class="vertical-align-middle text-right">8.00</td>
                    <td class="vertical-align-middle text-right">8.00</td>
                    <td class="vertical-align-middle text-right">0</td>
                    <td class="vertical-align-middle text-right">0</td>
                        <td class=" vertical-align-middle text-right">
                            8
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                    <td class="vertical-align-middle text-right">

                        8
                    </td>
                    <td class="vertical-align-middle text-right" rowspan="2">32.00</td>
                    <td class="vertical-align-middle text-right" rowspan="2">176</td>
                    <td class="vertical-align-middle text-right" rowspan="2">24.00</td>
                    <td class="vertical-align-middle text-right" rowspan="2">0.14</td>
                    <td class="vertical-align-middle text-right" rowspan="2">16.00</td>
                    <td class="vertical-align-middle text-right" rowspan="2">66.67%</td>
                    <td class="vertical-align-middle text-right">112.56</td>
                    <td class="vertical-align-middle">EUR</td>
                    <td class="vertical-align-middle text-right">1.95583</td>
                    <td class="vertical-align-middle text-right">3522.23</td>
                    <td class="vertical-align-middle text-right" rowspan="2">3522.23</td>
                    <td class="vertical-align-middle text-right" rowspan="2">220.14</td>
                    <td class="vertical-align-middle text-right" rowspan="2">1600.00</td>
                    <td class="vertical-align-middle text-right" rowspan="2">2057.89</td>
                    <td class="vertical-align-middle text-right" rowspan="2">200.00</td>
                    <td class="vertical-align-middle text-right">1693.42</td>
                    <td class="vertical-align-middle text-right" rowspan="2">2257.89</td>
                    <td class="vertical-align-middle text-right" rowspan="2">1264.34</td>
                    <td class="vertical-align-middle text-right" rowspan="2">0.36</td>
                </tr>
                <tr style="color:" title="" class="missing-rate-row">
                                            <td class="vertical-align-middle">Unassigned</td>
                    <td class="vertical-align-middle">Unassigned</td>

                    <td class=" vertical-align-middle text-right">
                        0
                    </td>
                    <td class=" vertical-align-middle text-right">
                        0
                    </td>
                    <td class="vertical-align-middle text-right">
                        0
                    </td>
                    <td class="vertical-align-middle text-right">
                        0
                    </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            8
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                    <td class="vertical-align-middle text-right">

                        0
                    </td>
                                            <td class="vertical-align-middle text-right">
                        0.00
                    </td>
                    <td class="vertical-align-middle">
                        BGN
                    </td>
                    <td class="vertical-align-middle text-right">
                        1.00000
                    </td>
                    <td class="vertical-align-middle text-right">

                    </td>
                    <td class="vertical-align-middle text-right">
                        564.47
                    </td>
                </tr>
                <tr style="color:" title="" class="missing-rate-row">
                    <td class="vertical-align-middle column-fixed" rowspan="1" style="left: 0px; background: none;">
                        Total
                    </td>
                                            <td class="vertical-align-middle" rowspan="1"></td>
                    <td class="vertical-align-middle" rowspan="1"></td>
                                            <td class="vertical-align-middle" rowspan="1"></td>
                    <td class="vertical-align-middle" rowspan="1"></td>

                    <td class="vertical-align-middle" rowspan="1"></td>
                    <td class="vertical-align-middle" rowspan="1"></td>
                    <td class="vertical-align-middle" rowspan="1"></td>
                    <td class="vertical-align-middle" rowspan="1"></td>

                    <td class="vertical-align-middle"></td>
                    <td class="vertical-align-middle"></td>

                    <td class="vertical-align-middle text-right">8.00</td>
                    <td class="vertical-align-middle text-right">8.00</td>
                    <td class="vertical-align-middle text-right">0</td>
                    <td class="vertical-align-middle text-right">0</td>
                        <td class=" vertical-align-middle text-right">
                            8
                        </td>
                        <td class=" vertical-align-middle text-right">
                            8
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                        <td class=" vertical-align-middle text-right">
                            0
                        </td>
                    <td class="vertical-align-middle text-right">

                        8
                    </td>
                    <td class="vertical-align-middle text-right" rowspan="1">32.00</td>
                    <td class="vertical-align-middle text-right" rowspan="1">176</td>
                    <td class="vertical-align-middle text-right" rowspan="1">24.00</td>
                    <td class="vertical-align-middle text-right" rowspan="1">0.14</td>
                    <td class="vertical-align-middle text-right" rowspan="1">16.00</td>
                    <td class="vertical-align-middle text-right" rowspan="1">66.67%</td>
                    <td class="vertical-align-middle text-right"></td>
                    <td class="vertical-align-middle"></td>
                    <td class="vertical-align-middle text-right"></td>
                    <td class="vertical-align-middle text-right"></td>
                    <td class="vertical-align-middle text-right" rowspan="1">3522.23</td>
                    <td class="vertical-align-middle text-right" rowspan="1">220.14</td>
                    <td class="vertical-align-middle text-right" rowspan="1">1600.00</td>
                    <td class="vertical-align-middle text-right" rowspan="1">2057.89</td>
                    <td class="vertical-align-middle text-right" rowspan="1">200.00</td>
                    <td class="vertical-align-middle text-right">2257.89</td>
                    <td class="vertical-align-middle text-right" rowspan="1">2257.89</td>
                    <td class="vertical-align-middle text-right" rowspan="1">1264.34</td>
                    <td class="vertical-align-middle text-right" rowspan="1">0.36</td>
                </tr>
            </tbody></table>

Solution

  • Standard Atata attributes for table cell finding FindByColumnHeaderAttribute and FindByColumnIndexAttribute will not work for such table as rows have different count of <td> cell elements because of rowspan.

    I created separate sample project https://github.com/atata-framework/atata-samples/tree/master/TableWithRowSpannedCells in atata-samples repository to experiment with such table and provide a solution. I ended up with 3 approaches for your table. So follow the link to check the sources and details about them.

    Here is the first and straightforward approach using finding by XPath:

    using Atata;
    
    namespace AtataSamples.TableWithRowSpannedCells
    {
        using _ = TableUsingXPathPage;
    
        [Url("table-with-row-spanned-cells")]
        public class TableUsingXPathPage : Page<_>
        {
            public Table<UserRow, _> Users { get; private set; }
    
            public class UserRow : TableRow<_>
            {
                [FindByXPath(XPathTo.RowSpannedCell, Index = 0)]
                public Text<_> Name { get; private set; }
    
                [FindByXPath(XPathTo.RowSpannedCell, Index = 2)]
                [Format("yyyy-MM-dd")]
                public Date<_> StartDate { get; private set; }
    
                [FindByXPath(XPathTo.RowSpannedCell, Index = 8)]
                public Text<_> ExpertiseLevel { get; private set; }
    
                [FindByXPath(XPathTo.NonRowSpannedCell, Index = 0)]
                public Text<_> Client { get; private set; }
    
                [FindByXPath(XPathTo.NonRowSpannedCell, Index = 1)]
                public Text<_> Project { get; private set; }
    
                [FindByXPath(XPathTo.NonRowSpannedCell, Index = 16)]
                public Number<_> DirectProjectCost { get; private set; }
    
                [FindByXPath(XPathTo.RowSpannedCell, Index = 22)]
                public Number<_> GrossMarginPercent { get; private set; }
    
                private static class XPathTo
                {
                    public const string RowSpannedCell = "(self::*[td[@rowspan]] | preceding-sibling::tr[td[@rowspan]])[last()]/td[@rowspan]";
    
                    public const string NonRowSpannedCell = "td[not(@rowspan)]";
                }
            }
        }
    }
    

    The idea is to detect and split <td> cell elements on two types: with and without rowspan attribute.

    • Cells without rowspan can be found in scope of <tr>: "td[not(@rowspan)]" (as a note, this is relative XPath that will execute for particular <tr>)
    • Cells with rowspan can be missing in tr and in this case you need to find this cells in nearest preceding sibling <tr> that contains rowspan: "(self::*[td[@rowspan]] | preceding-sibling::tr[td[@rowspan]])[last()]/td[@rowspan]"

    So you find a cell by using one of XPath'es depending on type of the cell and specify it's index in scope of the rows of the same type.

    Check out Table with Row-Spanned Cells sample for other approaches and details.