【问题标题】:Automation | Webdriverio | What is the use of using get before element name?自动化 |网络驱动程序 |在元素名称之前使用 get 有什么用?
【发布时间】:2021-06-17 15:29:07
【问题描述】:

在我的框架中,我有一个页面 xyz,其元素属性在 xyz.page.js

中定义
    get logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }

    logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }
  1. 使用“get”有什么目的吗?
  2. 这两个元素会被视为不同的元素吗?

【问题讨论】:

    标签: cucumber webdriver-io


    【解决方案1】:

    get 是 javascript 中的 getter 关键字。它允许您将变量绑定到属性。

    使用吸气剂:

        const obj = {
          log: ['a', 'b', 'c'],
          get latest() {
            if (this.log.length === 0) {
              return undefined;
            }
            return this.log[this.log.length - 1];
          }
        };
        
        console.log(obj.latest);
        // expected output: "c"
     
        console.log(obj);
        //prints the final object state

    没有吸气剂:

        const obj = {
          log: ['a', 'b', 'c'],
          latest() {
            if (this.log.length === 0) {
              return undefined;
            }
            return this.log[this.log.length - 1];
          }
        };
    
        console.log(obj.latest());
        // expected output: "c"
    
        console.log(obj);
        //prints the final object state

    在getter中,函数被解析为具有指定值的属性。虽然没有 getter,但它会被解析为一个具有 function 值的属性,您必须调用该函数来触发它

    或者简单地说,它充当自调用函数

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多