【问题标题】:Adding global variables via a require Common Function file通过 require Common Function 文件添加全局变量
【发布时间】:2019-06-16 07:32:42
【问题描述】:

我正在做一个关于切换到 Protractor 和 Jasmine 来执行我们的自动化脚本的 POC。我正在尝试构建一个初步框架,但在将我的概念转化为现实时遇到了问题。

我设置了三个文件:conf.js、spec.js 和 cf.js。 Conf.js 是特定于测试计划的配置文件,spec.js 包含实际测试,而 cf.js 包含我将在所有测试计划中使用的常用功能。我试图在 cf.js 中包含一个变量,以包含要在 browser.get 调用中使用的起始 URL。到目前为止,我还无法让它发挥作用。我尝试在 //commonfunctions// 函数声明之前在 cf.js 中声明它,以及在函数本身中声明它。这样做的正确方法是什么?

cf.js

    var commonfunctions = function () {
      global.StartPage = 'http://google.com/';
      this.ccClick = function (clickElement) {
        browser.wait(protractor.ExpectedConditions.visibilityOf(clickElement),
         this.defaultWait);



  browser.wait(protractor.ExpectedConditions.elementToBeClickable(clickElement),
     this.defaultWait);
        clickElement.click();
    };
    // Common text search
      this.ConfirmText = function(testElement, compareString) {
        browser.wait(protractor.ExpectedConditions.visibilityOf(testElement), 
10000);
        expect(testElement.getText()).toEqual(compareString);
      };
    };
    module.exports = new commonfunctions();

spec.js

  beforeEach(function() {
    browser.waitForAngularEnabled(false);
    browser.get(commonfunctions.StartPage);
  });

目前,它没有导航到网页。

【问题讨论】:

    标签: javascript protractor


    【解决方案1】:

    这应该是可能的,我之前发布过类似的答案,但如果您对该方法有进一步的担忧,请告诉我。我采取的方法是要求 onPrepare 中的通用函数文件作为全局变量。这样,在所有测试中都可以访问从文件中导出的任何内容。

    Storing global variable in a separate file for Protractor Tests

    【讨论】:

    • 谢谢!从 ASP 和 VBScript 开发切换到 Javascript 和 Selenium 让我头晕目眩。这效果很好。
    • 我知道那种痛苦,我自己也做了类似的转变。我犯的错误是试图在不了解它所依赖的任何技术(jasmine、NodeJS、JS、webdriver、HTML)的情况下跳入 Protractor,这使得故障排除非常痛苦。希望你比我一开始学到的更多。
    【解决方案2】:

    你在下面的代码中犯了一个错误:

    // cf.js
    var commonfunctions = function () {
          global.StartPage = 'http://google.com/';
    
    // spec.js
    beforeEach(function() {
      browser.waitForAngularEnabled(false);
      browser.get(commonfunctions.StartPage);
    });
    
    // you define `StartPage` to a global variable, not a property of `commonfunctions`, 
    thus you shouldn't refer it from `commonfunctions`, but from `global` as following:
    
    browser.get(global.StartPage)
    

    或者您将StartPage 定义为commonfunctions 的属性

    // cf.js
    var commonfunctions = function () {
       this.StartPage = 'http://google.com/';
       // use `this` at here, rather than `global`
    
    // spec.js
    beforeEach(function() {
      browser.waitForAngularEnabled(false);
      browser.get(commonfunctions.StartPage);
    });
    

    【讨论】:

      【解决方案3】:

      将以下内容添加到您的config.js

      baseUrl: 'http://google.com/',

      如下在你的测试中使用它

      browser.get(browser.baseUrl);

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2015-01-16
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 2013-06-18
        • 1970-01-01
        相关资源
        最近更新 更多