jedrek cienicki
jedrek cienicki

Reputation: 153

Protractor - X is not a function

In Protractor I'm trying to run a function from another page inside my spec file.

My spec file:

let TablePage = require("./../pages/TablePage");
let table_page = new TablePage();
let protractor = require("protractor");
let browser = protractor.browser;

   describe('Login', function() {
        beforeEach(function() {
            browser.ignoreSynchronization = true;
            browser.waitForAngular();
            jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
            browser.get("/budget");
        });

        it('should get values from list', function(){
            table_page.getPriceValuesFromList();
        });

The other file form which i get the function (TablePage.js):

let protractor = require("protractor");
let browser = protractor.browser;
let number = 0;
let prices = [];

let TablePage = (function () {
  function TablePage() {

  }

  TablePage.prototype.getPriceValuesFromList = function () {
    for (number = 1; number < 100; number++) {
      let locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
      browser.findElement(By.xpath(locator)).then(function (err) {
        prices[number] = element(By.xpath(locator)).getText();
        console.log(prices[number])
      }, function (err) {
        if (err) {
          break;
        }
      })
    }
  };

  return TablePage;

});
module.exports = TablePage;

I get an error: table_page.getPriceValuesFromList is not a function

Do you know what's wrong? I was doing it this way in the other project and it was working. The real typeof() this function is undefined

You can also check if the function will work - it should get values from one row of the table, save it in array and go to the next row until the value inside row is not found - Save values form the column

Upvotes: 0

Views: 111

Answers (1)

Bouke
Bouke

Reputation: 1577

There are a couple of lines which shouldn't be there. TablePage should be defined as follows:

function TablePage() { }

// Removed lines which were here.

TablePage.prototype.getPriceValuesFromList = function () {
    for (number = 1; number < 100; number++) {
        let locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
        browser.findElement(By.xpath(locator)).then(function (err) {
            prices[number] = element(By.xpath(locator)).getText();
            console.log(prices[number])
        }, function (err) {
            if (err) {
                break;
            }
        })
    }
};

// And removed the return TablePage();

As an object orientalist I prefer implementation using classes:

class TablePage {

    getPriceValuesFromList() {
        for (number = 1; number < 100; number++) {
            let locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
            browser.findElement(By.xpath(locator)).then(function (err) {
                prices[number] = element(By.xpath(locator)).getText();
                console.log(prices[number])
            }, function (err) {
                if (err) {
                    break;
                }
            })
        }
    };

};

Upvotes: 1

Related Questions