【问题标题】:Using Protractor to get element titles in the DOM使用 Protractor 获取 DOM 中的元素标题
【发布时间】:2017-07-02 01:51:32
【问题描述】:

我正在设置页面对象以从 DOM 获取一些基本元素数据。

例如,在我的index.page.js 页面对象中,我想从.panel-top-two > .row > h1 访问title 元素,如下所示:

$('div.panels.panel-top-two > .row > h1').text()

我可以在我的页面对象文件中使用 jQuery,还是需要坚持使用 Protractor Locators ?我使用量角器有点新,所以我正在寻找一些方向。

这是我导出的示例页面对象文件。 this.getFirstPanelText 是一个有效的函数吗? :

module.exports = function () {
    this.button = element(by.id('myButton'));
    this.message = element(by.binding('messageText'));
    this.domain = "http://localhost:64174";

    this.get = function () {
        browser.get(this.domain + '/myPage.html');
    };

  this.getFirstPanelText = function () {
        return $('div.panels.panel-top-one > .row > h1').text();
    };
    this.clickButton = function () {
        this.button.click();
    };

    this.getTitle = function () {
        return browser.getTitle();
    };

    this.getMessageText = function () {
        return this.message.getText();
    };
};

感谢您的回答。

【问题讨论】:

    标签: jasmine protractor


    【解决方案1】:

    立即回答您的问题:不,您不能使用 jQuery。

    现在关于this.getFirstPanelText

    这部分是一个有效的功能,$ 很好,但text() 不是。如果您之所以询问是因为您在其他 Protractor 问题中看到了 $,请务必注意 这不是 jQuery。它是 Protractor 的一部分,巧合的是,它的语法与 jQuery 的元素定位器函数相同。所以不要尝试在你的代码中使用 jQuery 函数,它会抛出一个错误。

    $() 只是 element(by.css()) 的简写

    您可以参考我的answer on another question 了解有关 $ 用法的详细说明。

    【讨论】:

    • 你完全正确。我使用 $ 作为 jquery,我在这里看到是错误的。因此,如果我需要一些自定义选择,是否要返回 Document.getElementBy... () ?
    • 不,您不应该使用 document.getElementBy。量角器应该有足够的定位器来覆盖所有东西。 $ 中使用的定位器应该仍然有效。如果您可以发布 HTML,我可以添加一些示例
    • 谢谢,我可以。更具体地在早上跟进。
    • @Gunderson @bob.mazzo 在量角器配置文件中有一个noGlobals 选项!
    【解决方案2】:

    如果你将它设置为true,那么量角器在配置中提供了一个noGlobals 选项。您也可以使用您的 jquery 样式代码!它不应该与量角器的全局变量冲突。

    但我也同意@Gunderson,并建议不要将 jquery 代码与量角器混为一谈,因为我看不出有任何理由!

    exports.config = {
    
    seleniumAddress: env.seleniumAddress,
    
    framework: 'jasmine',
    
    specs: [
    'noGlobals/noGlobals_spec.js'
    ],
    
    noGlobals: true,
    };
    

    如需参考,请查看noGlobal conf file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-20
      • 2018-07-02
      • 2016-07-26
      • 1970-01-01
      • 2014-07-22
      • 2022-12-05
      • 2016-02-21
      • 1970-01-01
      相关资源
      最近更新 更多