test: move robotjs-based devtools paste test to main runner (#20095)

This commit is contained in:
Jeremy Apthorp 2019-09-04 14:44:13 -07:00 committed by GitHub
parent bff16e027a
commit 88e32ef231
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 62 deletions

View file

@ -3,10 +3,10 @@ import { AddressInfo } from 'net'
import * as chaiAsPromised from 'chai-as-promised'
import * as path from 'path'
import * as http from 'http'
import { BrowserWindow, ipcMain, webContents, session } from 'electron'
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
import { ifdescribe } from './spec-helpers';
import { BrowserWindow, ipcMain, webContents, session, clipboard } from 'electron'
import { emittedOnce } from './events-helpers'
import { closeAllWindows } from './window-helpers'
import { ifdescribe, ifit } from './spec-helpers'
const { expect } = chai
@ -951,4 +951,58 @@ describe('webContents module', () => {
w.loadFile(path.join(fixturesPath, 'pages', 'c.html'))
})
})
describe('devtools window', () => {
let hasRobotJS = false
try {
// We have other tests that check if native modules work, if we fail to require
// robotjs let's skip this test to avoid false negatives
require('robotjs')
hasRobotJS = true
} catch (err) { /* no-op */ }
afterEach(closeAllWindows)
// NB. on macOS, this requires that you grant your terminal the ability to
// control your computer. Open System Preferences > Security & Privacy >
// Privacy > Accessibility and grant your terminal the permission to control
// your computer.
ifit(hasRobotJS)('can receive and handle menu events', async () => {
const w = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true } })
w.loadFile(path.join(fixturesPath, 'pages', 'key-events.html'))
// Ensure the devtools are loaded
w.webContents.closeDevTools()
const opened = emittedOnce(w.webContents, 'devtools-opened')
w.webContents.openDevTools()
await opened
await emittedOnce(w.webContents.devToolsWebContents, 'did-finish-load')
w.webContents.devToolsWebContents.focus()
// Focus an input field
await w.webContents.devToolsWebContents.executeJavaScript(`
const input = document.createElement('input')
document.body.innerHTML = ''
document.body.appendChild(input)
input.focus()
`)
// Write something to the clipboard
clipboard.writeText('test value')
const pasted = w.webContents.devToolsWebContents.executeJavaScript(`new Promise(resolve => {
document.querySelector('input').addEventListener('paste', (e) => {
resolve(e.target.value)
})
})`)
// Fake a paste request using robotjs to emulate a REAL keyboard paste event
require('robotjs').keyTap('v', process.platform === 'darwin' ? ['command'] : ['control'])
const val = await pasted
// Once we're done expect the paste to have been successful
expect(val).to.equal('test value', 'value should eventually become the pasted value')
})
})
})