I’m having trouble with ES6 imports in my JavaScript project. I have two files where I need to import a class from one file to another but I keep getting an error.
main.js
import { ProductPage } from '../pages/productPageClass';
const helpers = require("../helpers/helpers");
const expect = require('chai').expect;
const config = require('../config.json');
describe('Product page tests', function () {
const productPage = new ProductPage();
it(`Should load page with URL: ${config.baseUrl}`, async function () {
await productPage.openPage();
});
it('Should allow form submission with valid data', async function () {
await productPage.submitUserForm();
await helpers.clickElement(productPage.ui.buttons.submit);
});
if (productPage.hasInventoryIssue) {
it('Should handle inventory conflicts properly', async function () {
await browser.sleep(1500);
await productPage.removeAllItems();
await productPage.decreaseAllQuantities();
});
}
it('Should navigate to checkout when ready', async function () {
const checkoutBtn = productPage.ui.buttons.submit;
await helpers.waitForClickable(checkoutBtn);
await helpers.clickElement(checkoutBtn);
});
});
productPageClass.js
import { element, by } from 'protractor';
const helpers = require("../helpers/helpers");
const config = require('../config.json');
export class ProductPage {
get utilities() {
return {
quantityPattern: /^Error.*?(\d+)/
}
}
private get inputFields() {
return {
userEmail: element(by.id('userEmail')),
name: element(by.id('fullName')),
surname: element(by.id('surname')),
zipCode: element(by.id('zipCode')),
mainAddress: element(by.id('mainAddress')),
secondAddress: element(by.id('secondAddress')),
phoneNumber: element(by.id('phoneNumber')),
businessCheckbox: element(by.id('isBusiness')),
companyName: element(by.id('companyName')),
taxNumber: element(by.id('taxId')),
}
}
private get sections() {
return {
addressSection: element(by.css('div#addressSection.form-group.visible')),
companySection: element(by.id('companySection')),
}
}
get ui() {
return {
fields: this.inputFields,
sections: this.sections,
buttons: this.actionButtons
}
}
}
I keep getting SyntaxError: Cannot use import statement outside a module
when trying to run this. What’s the correct way to handle this import issue?