Skip to content

Windows screenshot doesn't work when os.tmpdir() has a space #320

@zixinyin

Description

@zixinyin

Describe the bug
Windows screenshot doesn't work when os.tmpdir() has a space.
More specifically, when the second and third args here both have a space in the path, the screenshot will fail with

Command failed: cmd.exe /c C:\TEMP\Path With Space\screenCapture\screenCapture_1.3.2.bat C:\TEMP\Path With Space\20251018-8268-rgxryz.jyytc.jpg
'C:\TEMP\Path' is not recognized as an internal or external command,
operable program or batch file.

To Reproduce

  1. set up the repo on a Windows machine
  2. npm run test:windows-space
const os = require('os');
const path = require('path');
const sinon = require('sinon');
const { existsSync, unlinkSync, mkdirSync } = require('fs');

// Test configuration
const TEMP_DIR_PATH = 'C:\\TEMP\\Path With Space';
const SCREENSHOT_FILENAME = 'screenshot-test.jpg';

async function runTest() {
  console.log('Running: screenshot test with spaces in path (Windows)');
  
  // Skip test if not on Windows
  if (process.platform !== 'win32') {
    console.log('Not on Windows, skipping test');
    return;
  }
  
  // Setup test directory
  try {
    !existsSync(TEMP_DIR_PATH) && mkdirSync(TEMP_DIR_PATH, { recursive: true });
  } catch (error) {
    console.error(`Error creating directory: ${error.message}`);
    return;
  }

  // Setup test
  const tmpDirStub = sinon.stub(os, 'tmpdir').returns(TEMP_DIR_PATH);
  const screenshot = require('./');
  const fullFilePath = path.join(os.tmpdir(), SCREENSHOT_FILENAME);
  
  try {
    // Run test
    const screenshotPath = await screenshot({ filename: fullFilePath });
    console.log('Screenshot saved at:', screenshotPath);
    
    // Verify result
    if (!existsSync(fullFilePath)) {
      throw new Error(`Screenshot file not found at: ${fullFilePath}`);
    }
    console.log('✓ Test passed');
  } catch (error) {
    console.error('✗ Test failed:', error.message);
    process.exit(1);
  } finally {
    // Cleanup
    [
      fullFilePath,
      path.join(process.cwd(), SCREENSHOT_FILENAME)
    ].forEach(file => existsSync(file) && unlinkSync(file));
    tmpDirStub.restore();
  }
}

runTest();

Expected behavior
User can take screenshots on Windows even when their os.tmpdir() has a space. The test above passes.

Environment (please complete the following information):

  • OS: Windows
  • Engine: Node
  • Version: All

Additional context
I will create a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions