【问题标题】:Protractor - Dynamically build input for "element" function量角器 - 为“元素”功能动态构建输入
【发布时间】:2018-06-14 09:48:24
【问题描述】:

我是 Protractor 的新手,我正在尝试编写一个接受 2 个值并且必须返回这样的元素的函数

  this.getinput = function(x, y) {
     var text123 = "by." + x + "('"  + y + "')"
     return element(text123);

所以这里的 x 和 y 可以是任何值,例如

   x: "buttonText" y:"save"
   x: "class"      y: ".css"

这行得通

     element(by.buttonText('save'))

但如果我尝试通过函数中的变量发送相同的内容,则会失败为“无效元素定位器”。有没有更好的方法来动态完成这项工作?

【问题讨论】:

    标签: jasmine protractor


    【解决方案1】:

    将对象设置为映射by API,如下所示:

    var byMap = {
        buttontext: by.buttonText,
        css: by.css,
        xpath: by.xpath,
        id: by.id,
        tagname: by.tagName,
        name: by.name,
        linktext: by.linkText,
    };
    
    function buildLocator(using, value) {
        return byMap[using.toLowerCase()](value);
    }
    
    function getInput(using, value) {
        return element(buildLocator(using, value));
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2015-06-28
      • 1970-01-01
      • 2015-09-09
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多