如何在运行时突出显示量角器中的元素

How to highlight element in protractor at run time

本文关键字:量角器 元素 显示 运行时      更新时间:2023-09-26

在我的量角器框架中,我想突出显示UI中找到的元素。我尝试使用以下代码,如果我使用 locators(id,name,className,linkText,xpath) 它工作正常。当我使用locators (buttonText,repeater,model,binding)以下代码不起作用并抛出"定位器无效"错误。

而不是"browser.driver.findElement(locator);",如果我使用"element(locator);"代码不起作用并抛出

"致命错误:CALL_AND_RETRY_LAST分配失败 - 进程超出 记忆"

节点版本---2.15.1

量角器版本 ---3.2.2

我如何调用函数:-

highlightElement(by.linkText('log In')); ---工作正常

highlightElement(by.buttonText('Place order')); -- 抛出错误

highlightElement = function(locator){
                    console.log("highlight--");
                    console.log("locator---:"+locator);
                    var ele = browser.driver.findElement(locator);
                    return browser.driver.executeScript("arguments[0].setAttribute('style', arguments[1]);",ele, "color: Red; border: 2px solid red;").
                        then(function(resp){
                        browser.sleep(2000);
                        return ele;
                    },function(err){
                        console.log("error is :"+err);
                    });

                };

请帮助我是否有任何其他方法可以突出显示量角器中的元素。

问候

迪帕克·库马尔·苏萨拉

你非常接近。 函数正在使用的所有这些定位符都是直接从 webDriver 继承的。 此列表包括 - className, css, id, linkTest, js, name, partialLinkText, tagName, and xpath ;

其他不起作用的是量角器的原型遗传。 此列表包括 - addLocator, binding, exactBinding, model, buttonText, partialButtonText, repeater, exactRepeater, cssContainingText, options, and deepCss .

类似地 - 函数findElement中的调用继承自 webDriver。 所以你本质上是在调用不支持的findElement(by.buttonText())(量角器的element(by.buttonText())会起作用)。

就您的代码而言,我修改了一些东西,它似乎对我有用:

highlightElement = function(el){
  console.log("highlight--");
  console.log("locator---:"+el.locator());
  return browser.driver.executeScript("arguments[0].setAttribute('style', arguments[1]);",el.getWebElement(), "color: Red; border: 2px solid red;").
  then(function(resp){
    browser.sleep(2000);
    return el;
  },function(err){
    console.log("error is :"+err);
  });
};

注意 el.locator()el.getWebElement(),而不是在函数var ele = browser.driver.findElement(locator);中声明元素,你可以简单地将元素作为参数传递给它。 为了提供参考,这是我的示例代码:

describe('Protractor Demo App', function() {
  it('changes the color', function() {
    browser.get('http://juliemr.github.io/protractor-demo/');
    var ely = element(by.model('first')); // using model
    highlightElement(ely); // works with model
    browser.sleep(5000); // just to see the header change color
    expect(ely.getAttribute('style')).toContain('color: red');
  });
});

来源: https://angular.github.io/protractor/#/api