You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Protractor POM中出现spec.clickGoButton不是函数错误求助

Fixing "spec.clickGoButton is not a function" in Protractor POM

Hey there! Let's walk through what's causing this error and fix it up for your Protractor POM project.

The Root Causes

You're hitting this error for a few key reasons:

  • You're not instantiating your page object – you exported a constructor function but tried to call its method directly as a static function.
  • Syntax errors in your page object file – missing quotes around the element ID, and incorrect placement of module.exports.
  • A typo in your test file extension – additionTest.jp should be additionTest.js so Node.js can properly require it.

Corrected Code

1. Fixed spec.js (Page Object)

First, we'll fix the element ID quote issue, move the export outside the constructor, and use a more descriptive name for clarity (a POM best practice):

// Renamed to CalculatorPage for better readability
var CalculatorPage = function() {
  // Add quotes around 'goButton' to treat it as a string ID
  this.goButton = element(by.id('goButton'));
  
  this.clickGoButton = function() {
    this.goButton.click();
  };
};

// Export the constructor function OUTSIDE the function body
module.exports = CalculatorPage;

2. Fixed additionTest.js (Test File)

Now we'll instantiate the page object before calling its method:

// Import the page object constructor
var CalculatorPage = require('./spec.js');

// Create an instance of the page object
var calculatorPage = new CalculatorPage();

function add(a, b) {
  // Call the method on the INSTANCE of the page object
  calculatorPage.clickGoButton();
}

Bonus: ES6 Class Version (Cleaner POM)

For a more modern and readable page object, you can use ES6 classes instead of constructor functions – this is widely recommended for Protractor POM setups:

Updated spec.js with ES6 Class

class CalculatorPage {
  constructor() {
    this.goButton = element(by.id('goButton'));
  }

  clickGoButton() {
    this.goButton.click();
  }
}

module.exports = CalculatorPage;

Corresponding additionTest.js

var CalculatorPage = require('./spec.js');
var calculatorPage = new CalculatorPage();

function add(a, b) {
  calculatorPage.clickGoButton();
}

Final Checks

  • Double-check that the goButton ID matches exactly what's in your HTML (it's case-sensitive!).
  • Make sure your Protractor config is set up correctly to run the test files.
  • Ensure you're running the test with protractor your-config-file.js to trigger Protractor's environment (don't run it directly with Node).

内容的提问来源于stack exchange,提问作者Mano Kugan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:18:31