【问题标题】:Protractor - How do I access the child elements within a parent element量角器 - 如何访问父元素中的子元素
【发布时间】:2018-09-11 18:03:10
【问题描述】:

我正在尝试访问包含在一行中的标签。

此代码有效,但是它会找到该类型的所有标签,而不是特定/父行中的一个:

expect(element(by.xpath("//span[@ng-click='location.viewAudit(audit)'][contains(@class, 'source-name')]")).isPresent()); //label for the source name

这是我失败的代码:

var tableRow = element(by.xpath("//tr[contains(@class, 'ng-scope')][@ng-repeat='audit in location.displayedProfileAudits']")).getWebElement();
expect(tableRow.element(by.xpath("//span[@ng-click='location.viewAudit(audit)'][contains(@class, 'source-name')]")).isPresent()); //label for the source name 

这是返回的错误:

Message: TypeError: tableRow.element is not a function

完整的步骤定义:

Then('I should see that a location audit source row has a label for the source', function (callback) {
    browser.wait(EC.visibilityOf(listingsPageObj.locationProfileListViewContainer), timeouts.EC_TIMEOUT).then(() => {
        browser.wait(EC.visibilityOf(listingsPageObj.locationProfileListViewTable), timeouts.EC_TIMEOUT).then(() => {
            browser.wait(() => {
                return listingsPageObj.locationProfileListViewTableHeaders.count().then(cnt => (cnt > 0)); //which means that there are audit results displayed
            }).then(() => {
                //find a row in the list of displayed audits
                var tableRow = element(by.xpath("//tr[contains(@class, 'ng-scope')][@ng-repeat='audit in location.displayedProfileAudits']")).getWebElement();
                //then verify, within that row, there is a label for the source name
                expect(tableRow.element(by.xpath("//span[@ng-click='location.viewAudit(audit)'][contains(@class, 'source-name')]")).isPresent()); //label for the source name 

                callback();
            });
        });
    });
});

【问题讨论】:

  • 您需要拨打getWebElement吗?
  • 嗯显然不是...谢谢。

标签: angularjs protractor element qa


【解决方案1】:

您需要在 xpath 的标题处添加一个点 .,如下所示。这里的. 表示当前节点,即前一个tableRow,这将改变当前节点tableRow 内的搜索上下文。如果标题中没有.,// 意味着搜索上下文是整个页面,这就是为什么您会找到该类型的所有标签的原因。

其实你可以把tableRow.element...里面的tableRow去掉,如果//span[....]的标题没有.的话

expect(tableRow.element(by.xpath(
    ".//span[@ng-click='location.viewAudit(audit)'][contains(@class, 'source-name')]"))
.isPresent());

【讨论】:

  • 谢谢永。我选择了您的答案,因为我很欣赏有关在节点内搜索的额外信息。
【解决方案2】:
Then('I should see that a location audit source row has a label for the source', function (callback) {
    browser.wait(EC.visibilityOf(listingsPageObj.locationProfileListViewContainer), timeouts.EC_TIMEOUT).then(() => {
        browser.wait(EC.visibilityOf(listingsPageObj.locationProfileListViewTable), timeouts.EC_TIMEOUT).then(() => {
            browser.wait(() => {
                return listingsPageObj.locationProfileListViewTableHeaders.count().then(cnt => (cnt > 0)); //which means that there are audit results displayed
            }).then(() => {
                //find a row in the list of displayed audits
                var tableRow = element(by.xpath("//tr[contains(@class, 'ng-scope')][@ng-repeat='audit in location.displayedProfileAudits']"));
                //then verify, within that row, there is a label for the source name
                expect(tableRow.element(by.xpath("//span[@ng-click='location.viewAudit(audit)'][contains(@class, 'source-name')]")).isPresent()); //label for the source name 
                callback();
            });
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多