我在使用ES6类导出和导入时遇到两个问题。首先,在导入时,无法从导入的类访问方法。其次,在通过visual studio代码执行代码时,我面临以下错误"语法错误:意外的令牌导入"。请提供更正或建议。
以下是我的班级文件" Login_pageobj.js"
export default class Login_pageobj {
constructor() {
//userName
this.enterUserName = element(by.xpath('//input[@id="username"]'));
//Password
this.enterPassword = element(by.xpath('//input[@id="password"]'));
//Login Button
this.clickLoginBtn = element(by.buttonText('LOGIN'));
//invalid UserName Password Error
this.inValErrMsg = element(by.xpath('//div[@class="subtext"]'));
}
//Please enter a user name (required). method
isDisplyedUserNameErrorMsg() {...}
以下代码是My Protractor spec文件Login_spec.js
import Login_pageobj from '../ES6_pageobj/login_pageobj';
describe('Login Page Validation', function() {
it('Launch Commercial URL',function(){
browser.get('http://applicationurl');
browser.manage().window().maximize();
expect(browser.getTitle()).toBe('LoginWeb');
});
it('Login Button should Disabled',function(){
console.log('----------------------------------------------------');
let lgin_pgobj = new login_pageobj();
lgin_pgobj.verifyLgnBtnEnabled();//***unable to access this method***
});
});
通过Visual Studio代码调试器运行此代码时,我遇到以下错误。
Debugging with inspector protocol because Node.js v8.9.1 was detected.
node --inspect-brk=23169 node_modules\protractor\bin\protractor
e:\CommercialPOC/conf.js --suite es6
Debugger listening on ws://127.0.0.1:23169/e3dfd326-8468-4a3a-a2e3-3094d97c5571
(node:4708) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
(node:4708) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
warning.js:18
[22:00:06] I/launcher - Running 1 instances of WebDriver
logger.js:155
[22:00:06] I/direct - Using ChromeDriver directly...
logger.js:155
null
conf.js:48
[22:00:25] E/launcher - Error:
e:\commercialPOC\ES6_moduleScenarios\0Login_spec.js:1
logger.js:155
(function (exports, require, module, __filename, __dirname) { import
Login_pageobj from '../ES6_pageobj/login_pageobj';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at e:\CommercialPOC\node_modules\jasmine\lib\jasmine.js:93:5
[22:00:25] E/launcher - Process exited with error code 100
答案 0 :(得分:0)
你应该使用这样的import
结构:
const Login_pageobj = require('../ES6_pageobj/login_pageobj')