在两个 Javascript 文件之间共享一个常量值
Sharing a Constant value between two Javascript files
总的来说,我是 JS 和 Node 的新手。我正在尝试使用 Puppeteer 来简单地获取
标签的文本值并将其保存为常量。然后,我尝试在我的 Mocha 测试所在的 'base' class (index.js) 中使用该值。出于某种原因,我正在挣扎。我正在使用异步。
我的文件结构是:
这是我的 Puppeteer 脚本:
//customerChoices.js
module.exports = async(page) => {
const frame = page.frames().find(frame => frame.name() === 'iframe');
const saveYourChoicesButton = await frame.$('body > div > div > div > form > footer > div > button.permissions-block__submit');
await saveYourChoicesButton.click({});
await page.waitForSelector('.page-title');
const confirmationMessageText = await frame.$eval('.submission-response__copy > p', e => e.textContent);
return confirmationMessageText
};
这是我的 index.js 脚本,我在其中尝试导入常量 confirmationMessageText 并在测试中使用它:
const confMessage = require('./test/uiTests/customerChoices');
const expect = require('chai').expect;
const puppeteer = require('puppeteer');
const _ = require('lodash');
const chai = require('chai');
describe('Update customer choices', function() {
it('test all customer choices', async function() {
const url = _.get(url, `${env}`);
await page.goto(url);
await customerChoices(page);
const cm = awaitCustomerChoices(page);
expect(cm).to.equal('Thank you. Your choices have been updated.');
expect(cm).to.equal('Thank you. Your choices have been updated.');
console.log(confirmationMessageText);
});
我不清楚为什么 confirmationMessageText 是“谢谢。您的选择已更新。”来自 Puppeteer 脚本,但 'undefined' 来自 index.js?
如果它有用,我的 package.json 看起来像:
"engines": {
"node": ">=6"
},
"dependencies": {
"chai": "^4.1.2",
"lodash": "^4.17.10",
"mocha": "^5.2.0",
"moment": "^2.22.2",
"newman": "^4.0.1",
"puppeteer": "^1.6.2",
"yargs": "^12.0.1",
"express": "^4.16.4",
"supertest": "^3.3.0"
},
"devDependencies": {
"chai-as-promised": "^7.1.1",
"express": "^4.16.4",
"supertest": "^3.3.0"
}
}
module.exports
不应异步更改,特别是如果它应该在函数调用时更改。 CommonJS 模块被评估一次,confMessage
是 async(page) => {...}
函数。
该函数应该 return 结果:
module.exports = async(page) => {
...
return confirmationMessageText;
};
并像这样使用:
const cm = await customerChoices(page);
总的来说,我是 JS 和 Node 的新手。我正在尝试使用 Puppeteer 来简单地获取
标签的文本值并将其保存为常量。然后,我尝试在我的 Mocha 测试所在的 'base' class (index.js) 中使用该值。出于某种原因,我正在挣扎。我正在使用异步。
我的文件结构是:
这是我的 Puppeteer 脚本:
//customerChoices.js
module.exports = async(page) => {
const frame = page.frames().find(frame => frame.name() === 'iframe');
const saveYourChoicesButton = await frame.$('body > div > div > div > form > footer > div > button.permissions-block__submit');
await saveYourChoicesButton.click({});
await page.waitForSelector('.page-title');
const confirmationMessageText = await frame.$eval('.submission-response__copy > p', e => e.textContent);
return confirmationMessageText
};
这是我的 index.js 脚本,我在其中尝试导入常量 confirmationMessageText 并在测试中使用它:
const confMessage = require('./test/uiTests/customerChoices');
const expect = require('chai').expect;
const puppeteer = require('puppeteer');
const _ = require('lodash');
const chai = require('chai');
describe('Update customer choices', function() {
it('test all customer choices', async function() {
const url = _.get(url, `${env}`);
await page.goto(url);
await customerChoices(page);
const cm = awaitCustomerChoices(page);
expect(cm).to.equal('Thank you. Your choices have been updated.');
expect(cm).to.equal('Thank you. Your choices have been updated.');
console.log(confirmationMessageText);
});
我不清楚为什么 confirmationMessageText 是“谢谢。您的选择已更新。”来自 Puppeteer 脚本,但 'undefined' 来自 index.js?
如果它有用,我的 package.json 看起来像:
"engines": {
"node": ">=6"
},
"dependencies": {
"chai": "^4.1.2",
"lodash": "^4.17.10",
"mocha": "^5.2.0",
"moment": "^2.22.2",
"newman": "^4.0.1",
"puppeteer": "^1.6.2",
"yargs": "^12.0.1",
"express": "^4.16.4",
"supertest": "^3.3.0"
},
"devDependencies": {
"chai-as-promised": "^7.1.1",
"express": "^4.16.4",
"supertest": "^3.3.0"
}
}
module.exports
不应异步更改,特别是如果它应该在函数调用时更改。 CommonJS 模块被评估一次,confMessage
是 async(page) => {...}
函数。
该函数应该 return 结果:
module.exports = async(page) => {
...
return confirmationMessageText;
};
并像这样使用:
const cm = await customerChoices(page);