Angularjs 如何在嵌套函数中将变量传递给javascript

Angularjs 如何在嵌套函数中将变量传递给javascript,angularjs,protractor,Angularjs,Protractor,我有几个量角器/angularjs it块,它们重复我想放在函数中的相同代码位。我只想调用这个函数,而不是一遍又一遍地重复这个过程 it('should move directly to Draft', function() { posting_sum_page.locate_action_button.click(); posting_action_page.move_action.filter(function(elem) { return elem.getText().th

我有几个量角器/angularjs it块,它们重复我想放在函数中的相同代码位。我只想调用这个函数,而不是一遍又一遍地重复这个过程

it('should move directly to Draft', function() {
  posting_sum_page.locate_action_button.click();
  posting_action_page.move_action.filter(function(elem) {
    return elem.getText().then(function(text) {
      return text === 'Draft';
    });
  }).click();
});
块的这一部分是我想为其创建函数的重复部分。我是javascript新手,所以如何做到这一点让我感到困惑

return elem.getText().then(function(text) {
      return text === 'Draft';
    });
  }).click();

我需要能够用不同的变量替换“草稿”。我正在使用页面对象来完成这部分工作,我不确定A)如何创建这样的函数并传入文本&B)它应该放在规范端还是页面端?这对大多数人来说可能是非常基本的。但由于我是javascript新手,所以我很难理解这一点

也许是这样的

describe('...something...', function()
{
  var clickBtn;


  beforeEach(function()
  {
    clickBtn = function(testText)
    {
      return posting_action_page.move_action.filter(function(elem) 
      {
        return elem.getText().then(function(currentText) 
        {
          return currentText === testText;
        });
      }).click();
    };
  });


  it('should move directly to Draft', function() 
  {
    posting_sum_page.locate_action_button.click();
    expect(clickBtn('Draft')).toEqual('...something...');
  });
});

也许是这样的

describe('...something...', function()
{
  var clickBtn;


  beforeEach(function()
  {
    clickBtn = function(testText)
    {
      return posting_action_page.move_action.filter(function(elem) 
      {
        return elem.getText().then(function(currentText) 
        {
          return currentText === testText;
        });
      }).click();
    };
  });


  it('should move directly to Draft', function() 
  {
    posting_sum_page.locate_action_button.click();
    expect(clickBtn('Draft')).toEqual('...something...');
  });
});

如果只想重用返回块

it('should move directly to' + targetText, function() {
  posting_sum_page.locate_action_button.click();
  posting_action_page.move_action.filter(function(elem) {
    checkSameText(elem, targetText);
  }).click();
});

function checkSameText(el, targetText) {
    return el.getText().then(function(text) {
      return text === targetText;
    }); 
}

如果只想重用返回块

it('should move directly to' + targetText, function() {
  posting_sum_page.locate_action_button.click();
  posting_action_page.move_action.filter(function(elem) {
    checkSameText(elem, targetText);
  }).click();
});

function checkSameText(el, targetText) {
    return el.getText().then(function(text) {
      return text === targetText;
    }); 
}
我将把整个过滤函数提取到一个“helpers”模块中

helpers.js:

var Helpers = function () {
    this.filterByText = function (text) {
        return function (elem) {
            return elem.getText().then(function(actualText) {
                return actualText === text;
            });
        };
    }
}

module.exports = new Helpers();
测试中的用法:

var helpers = require("helpers");

describe("My Test", function () {
    it('should move directly to Draft', function() {
        posting_sum_page.locate_action_button.click();
        posting_action_page.move_action.filter(helpers.filterByText('Draft')).click();
    });
});
我将把整个过滤函数提取到一个“helpers”模块中

helpers.js:

var Helpers = function () {
    this.filterByText = function (text) {
        return function (elem) {
            return elem.getText().then(function(actualText) {
                return actualText === text;
            });
        };
    }
}

module.exports = new Helpers();
测试中的用法:

var helpers = require("helpers");

describe("My Test", function () {
    it('should move directly to Draft', function() {
        posting_sum_page.locate_action_button.click();
        posting_action_page.move_action.filter(helpers.filterByText('Draft')).click();
    });
});

我不确定发布动作页面的页面对象类型。移动动作,但我认为您需要的是使用或

//html:Draft
元素(by.buttonText('Draft'))。单击();
//html:草稿
元素(by.linkText('Draft'))。单击();

还有其他一些定位器可能会有所帮助,如.partialButtonText和.partialLinkText

by.partialButtonText
by.partialLinkText

我不确定发布动作页面.移动动作页面的页面对象类型是什么,但我认为您正在寻找的是使用或

//html:Draft
元素(by.buttonText('Draft'))。单击();
//html:草稿
元素(by.linkText('Draft'))。单击();

还有其他一些定位器可能会有帮助,如.partialButtonText的
和.partialLinkText的
谢谢大家的输入。alecxe您的解决方案效果最佳。非常感谢大家。感谢大家的投入。alecxe您的解决方案效果最佳。非常感谢大家。