【问题标题】:Protractor - How to get the text of the <a> tag that was clicked量角器 - 如何获取被点击的 <a> 标签的文本
【发布时间】:2016-10-20 03:00:37
【问题描述】:

也就是说,我刚刚成功选择了一个下拉选项;但是,我现在想断言选项 text 是预期值。

请注意,我在下面选择了带有elementSwitcher.element(by.linkText(option)).click() 的下拉选项值:

this.selectDropdown = function (name, option, uniqueId) {
        
        var elem = element(by.id(uniqueId));

        var elementSwitcher = elem.element(by.css("div[uib-dropdown]"));        
        elementSwitcher.element(by.css("button[uib-dropdown-toggle]")).click();
                
        elementSwitcher.element(by.linkText(option)).click().then(function () {
            var el = elementSwitcher.element(by.tagName('a')).getText();
            console.log('*** JUST CLICKED. el text =  ', el);
        });               

    };

呈现的 HTML 看起来像这样:

<div class="btn-group dropdown open" uib-dropdown="">
 <ul class="dropdown-menu accounts-dropdown" uib-dropdown-menu="" aria-labelledby="simple-dropdown">
   <li ng-repeat="accountChoice in ctrl.accountOptions" class="ng-scope">
     <a ng-click="ctrl.selectOption(accountChoice)" class="ng-binding">Assets</a>
   </li>
 </ul>
</div>

问题是我无法访问我期望的Assets 文本,而我的console.log() 告诉我:

W/element - more than one element found for locator By(css selector,
a) - the first result will be used
  *** JUST CLICKED. el text =   ElementFinder {
 browser_:
  ProtractorBrowser {
    controlFlow: [Function],

再次,我想单击下拉元素(a 标记),然后获取被单击的同一 a 标记的 text 部分。

不胜感激。

**** 更新 ****

根据 Alex 在下面的回答,我发布了我的最终辅助函数:

    /**
     * Helper function to select an angular-ui dropdown option, and return the <a> link text which was selected.
     * 
     * @param name
     * @param option
     * @param uniqueId
     * 
     * @returns {Promise} A promise that returns the link text when resolved.
     */
    this.selectUibDropDownOption = function (name, option, uniqueId) {
        var deferred = protractor.promise.defer();

        var elem = element(by.id(uniqueId));      // i.e. by.id('drpAccount')

        var elementSwitcher = elem.element(by.css("div[uib-dropdown]"));     
        elementSwitcher.element(by.css("button[uib-dropdown-toggle]")).click();
        
        // i.e. by.linkText('Liabilities and Equity')
        //elementSwitcher.element(by.linkText(option)).click();  // also works, but doesn't get link text

        var link = elementSwitcher.element(by.linkText(option));

        // Important to first get the text of the <a> tag, then click on the link.
        link.getText().then(function (linkText) {            
            link.click();            
            deferred.fulfill(linkText);
        });
 
        return deferred.promise;
    };

从调用函数,我的describe/it 被定义:

var specItem = it(item.name, function () {             
    item.search.map(function (srchItem) {

        // i.e. { "name": "Account", "option": "Assets", "uniqueId": "drpAccount" },
	var optionText = pageObjects.selectUibDropDownOption(srchItem.name, srchItem.option, srchItem.uniqueId);
	
	optionText.then(function (linkText) {
	    console.log('** Clicked on: ', linkText);
	    expect(srchItem.option).toEqual(linkText);
	});
    });
});

【问题讨论】:

    标签: selenium-webdriver protractor e2e-testing


    【解决方案1】:

    您在控制台上看到的是一个承诺的字符串表示 - getText() 返回一个承诺。如果你想查看实际的链接文本值,resolve the promise

    elementSwitcher.element(by.tagName('a')).getText().then(function (linkText) {
        console.log('*** JUST CLICKED. el text =  ', linkText);
    });
    
    // or elementSwitcher.element(by.tagName('a')).getText().then(console.log);
    

    此外,如果您收到“过时元素引用”或“未找到元素”错误,您可能需要在实际点击链接之前获取文本。


    或者,为什么不这样做-通过链接文本找到链接,获取文本并单击它:

    var link = elementSwitcher.element(by.linkText(option));
    link.getText().then(function (linkText) {
        console.log(linkText);
        link.click();
    });
    

    【讨论】:

    • 对。我忘了解决 getText() 承诺。让我试试你更新的方法,因为 linkText 参数没有打印到控制台。它只是说 *** JUST CLICKED. el text = 。使用我的方法,by.tagName('a') 返回的元素不止一个。
    • 经过反复试验,我在您的第二种方法link.getText().then 中取得了进展。
    • @bob.mazzo 唷,我在等你来解决这个问题:) 问题的当前状态是什么?谢谢。
    • 我之前发表过评论,正在等待回复,但不知何故,我努力通过它并设法让它发挥作用。太感谢了。 :-)
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多