Protractor POM中出现spec.clickGoButton不是函数错误求助
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.jpshould beadditionTest.jsso 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
goButtonID 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.jsto trigger Protractor's environment (don't run it directly with Node).
内容的提问来源于stack exchange,提问作者Mano Kugan
相关产品推荐
相关产品推荐

