Javascript Protractor - Seeing outside functions a

2019-09-19 23:47发布

In specs/Test.js is a test definition: "regex2"

In pages/TablePage.js is a page object

in regex2 there is a try to use a function from TablePage.js

   it('regex2', function(){
            table_page.matchPriceRegex(table_page.workingBalanceField)
        });

it is saying table_page.matchPriceRegex is not a function

The function itself from TablePage.js:

var TablePage = (function () {

  function TablePage() {
    this.workingBalanceField = element(By.xpath('//*[@id="root"]/main/section/div/div/div[5]/div/div[1]'));
  }

  TablePage.prototype.matchPriceRegex = function (locator) {
    this.text = locator.getText();
    expect(this.text).toMatch("\d{0,3}?,?\d{0,3}?\.?\d{0,3}?");
  };
});
module.exports = TablePage;

The require's are incorporated with the spec file so it should see it

var TablePage = require("./../pages/TablePage");
var table_page = new TablePage();
var protractor = require("protractor");
var jasmine = require("jasmine-node");
var browser = protractor.browser;
var number = 0;

When in my IDE(WebStorm) I hold ctrl and click on the function name it redirects me correctly, as it sees it

The typeof the functions or variables form TablePage is undefined

Do you know where is the problem?

1条回答
欢心
2楼-- · 2019-09-20 00:39

The error comes from TablePage.js, it should be.

var TablePage = (function () {

  function TablePage() {
    this.workingBalanceField = element(By.xpath('//*[@id="root"]/main/section/div/div/div[5]/div/div[1]'));
  }

  TablePage.prototype.matchPriceRegex = function (locator) {
    this.text = locator.getText();
    expect(this.text).toMatch("\d{0,3}?,?\d{0,3}?\.?\d{0,3}?");
  };

  return TablePage; // return the class as outer function return value
})(); 
// `(function(...){})` return a function, you should use `()` to execute the
// return function to get the returned class: TablePage.

module.exports = TablePage;
查看更多
登录 后发表回答