api-web-utils-spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { BrowserWindow } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as path from 'node:path';
  4. import { defer } from './lib/spec-helpers';
  5. // import { once } from 'node:events';
  6. describe('webUtils module', () => {
  7. const fixtures = path.resolve(__dirname, 'fixtures');
  8. describe('getPathForFile', () => {
  9. it('returns nothing for a Blob', async () => {
  10. const w = new BrowserWindow({
  11. show: false,
  12. webPreferences: {
  13. contextIsolation: false,
  14. nodeIntegration: true,
  15. sandbox: false
  16. }
  17. });
  18. defer(() => w.close());
  19. await w.loadFile(path.resolve(fixtures, 'pages', 'file-input.html'));
  20. const pathFromWebUtils = await w.webContents.executeJavaScript('require("electron").webUtils.getPathForFile(new Blob([1, 2, 3]))');
  21. expect(pathFromWebUtils).to.equal('');
  22. });
  23. it('reports the correct path for a File object', async () => {
  24. const w = new BrowserWindow({
  25. show: false,
  26. webPreferences: {
  27. contextIsolation: false,
  28. nodeIntegration: true,
  29. sandbox: false
  30. }
  31. });
  32. defer(() => w.close());
  33. await w.loadFile(path.resolve(fixtures, 'pages', 'file-input.html'));
  34. const { debugger: debug } = w.webContents;
  35. debug.attach();
  36. try {
  37. const { root: { nodeId } } = await debug.sendCommand('DOM.getDocument');
  38. const { nodeId: inputNodeId } = await debug.sendCommand('DOM.querySelector', { nodeId, selector: 'input' });
  39. await debug.sendCommand('DOM.setFileInputFiles', {
  40. files: [__filename],
  41. nodeId: inputNodeId
  42. });
  43. const pathFromWebUtils = await w.webContents.executeJavaScript('require("electron").webUtils.getPathForFile(document.querySelector("input").files[0])');
  44. expect(pathFromWebUtils).to.equal(__filename);
  45. } finally {
  46. debug.detach();
  47. }
  48. });
  49. });
  50. });