How to select specific data in dynamic table using protractor?

拥有回忆 提交于 2021-02-11 15:42:49

问题


I am trying to automate some scenarios using protractor where we need to verify whether the data is updating in dynamic table.

Please find below

HTML Code: enter image description here

Table in page: enter image description here


回答1:


It can be done by verifying that element is present in the DOM with the added Group ID or Group Name.

For Group ID:

element(by.xpath("*//table//tbody//tr//td[1]//p[text()='Amanda Test Group']")).isDisplayed()

For Group name:

element(by.xpath("*//table//tbody//tr//td[2]//p[text()='Amanda Group']")).isDisplayed()




回答2:


I'm assuming you're using Angular2+, yes?

In your HTML Template, you are probably using an *ngFor directive to populate the table dynamically. Add an index to the *ngFor (it's best practices for updating the DOM) in order to add a dynamic id to each element:

    <tr *ngFor="let user of user; index as u" id="user-{{u + 1}}">
        <td id="userName-{{u + 1}}">
            {{user.firstName}} {{user.userName}}<br />
            {{user.userName}}
        </td>
        <td id="userRoles-{{ u + 1 }}">
            <span id="role-{{u + 1}}-{{ r + 1 }}" *ngFor="let role of user.roles; index as r">
                {{ role.toUpperCase() + ', '}}
            </span>
        </td>
        <!- Omitted code -->
    </tr>

In your Page Object:

// Get first user on the table
get firstUser() {
    return element(by.id('user-1');
}

// Get a specific user by id
public getUser(index: number) {
    return element(by.id(`user-${index}`);
}

// Get all of the attributes for a single user by id
get userAttributes(index: number) {
    return element.all(by.id(`user-${index}`);
}

I am not a fan of xpath selectors. Yes, they are faster. But in code that is dynamic or changes frequently, they are the most fragile of selectors. There is no reason your dynamic data cannot have a dynamic ID that clearly identifies each portion of the code you need.

Good luck!



来源:https://stackoverflow.com/questions/61497565/how-to-select-specific-data-in-dynamic-table-using-protractor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!