1

I'm currently testing in NightmareJS using different viewports and agents. I'm automating visits to a site that has a lot of complex scripting and styles. I'm not able to sufficiently duplicate the agent and view in a separate test browser such that the site displays the same content for me as it does when I'm visiting with Nightmare. For this reason, I need to be able to right-click elements in Nightmare's Electron window so that I can inspect specific elements in the DOM.

The problem is that right-click is disabled in the Electron window. I found this answer which describes adding code to the render process:

// Importing this adds a right-click menu with 'Inspect Element' option
const remote = require('remote')
const Menu = remote.require('menu')
const MenuItem = remote.require('menu-item')

let rightClickPosition = null

const menu = new Menu()
const menuItem = new MenuItem({
  label: 'Inspect Element',
  click: () => {
    remote.getCurrentWindow().inspectElement(rightClickPosition.x, rightClickPosition.y)
  }
})
menu.append(menuItem)

window.addEventListener('contextmenu', (e) => {
  e.preventDefault()
  rightClickPosition = {x: e.x, y: e.y}
  menu.popup(remote.getCurrentWindow())
}, false)

Note: this code would probably need to be altered as mentioned in the referenced answer but where would I add such code when working with Nightmare?

The main script isn't the same as the Electron render process script AFAIK. As for that sort of solution, this would probably be a better one but how to invoke?

Community
  • 1
  • 1
xendi
  • 2,332
  • 5
  • 40
  • 64
  • From the same post you linked seems like the API has changed a bit https://electron.atom.io/docs/api/menu/#render-process – m0meni May 02 '17 at 03:58
  • Yes but I just need to know how I might do such a thing in Nightmare. – xendi May 02 '17 at 04:03
  • Ah I misread your question sorry :/. Short of downloading the source from github, and modifying this file https://github.com/segmentio/nightmare/blob/e68d0f2ec7e32da9d2f353eb3ed19e86adce0f6d/lib/nightmare.js, I'm not sure. Hopefully someone else can help you there. – m0meni May 02 '17 at 04:05

1 Answers1

0

Currently there is no available api for right click in nightmare, reference issue:

https://github.com/segmentio/nightmare/issues/346

A possible solution could be to use Electron directly bypassing nighmarejs.

Example (no tested):

webpage.sendEvent('contextmenu')
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • NightmareJS wraps Electron, not Phantom. I'll accept the answer though. There is a fork that wraps Phantom but I wouldn't use it as Phantom is very slow compared to Electron. – xendi May 04 '17 at 23:44