您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

browserLogger.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import fs from 'node:fs';
  2. /**
  3. * A prefix to use for all messages we add to the console log.
  4. */
  5. export const LOG_PREFIX = '[MeetTest] ';
  6. /**
  7. * Initialize logger for a driver.
  8. *
  9. * @param {WebdriverIO.Browser} driver - The driver.
  10. * @param {string} name - The name of the participant.
  11. * @param {string} folder - The folder to save the file.
  12. * @returns {void}
  13. */
  14. export function initLogger(driver: WebdriverIO.Browser, name: string, folder: string) {
  15. // @ts-ignore
  16. driver.logFile = `${folder}/${name}.log`;
  17. driver.sessionSubscribe({ events: [ 'log.entryAdded' ] });
  18. driver.on('log.entryAdded', (entry: any) => {
  19. try {
  20. // @ts-ignore
  21. fs.appendFileSync(driver.logFile, `${entry.text}\n`);
  22. } catch (err) {
  23. console.error(err);
  24. }
  25. });
  26. }
  27. /**
  28. * Returns the content of the log file.
  29. *
  30. * @param {WebdriverIO.Browser} driver - The driver which log file is requested.
  31. * @returns {string} The content of the log file.
  32. */
  33. export function getLogs(driver: WebdriverIO.Browser) {
  34. // @ts-ignore
  35. if (!driver.logFile) {
  36. return;
  37. }
  38. // @ts-ignore
  39. return fs.readFileSync(driver.logFile, 'utf8');
  40. }
  41. /**
  42. * Logs a message in the logfile.
  43. *
  44. * @param {WebdriverIO.Browser} driver - The participant in which log file to write.
  45. * @param {string} message - The message to add.
  46. * @returns {void}
  47. */
  48. export function logInfo(driver: WebdriverIO.Browser, message: string) {
  49. // @ts-ignore
  50. if (!driver.logFile) {
  51. return;
  52. }
  53. try {
  54. // @ts-ignore
  55. fs.appendFileSync(driver.logFile, `${new Date().toISOString()} ${LOG_PREFIX} ${message}\n`);
  56. } catch (err) {
  57. console.error(err);
  58. }
  59. }