nightwatch - debugging with node.js and Chrome - node.js

Is there a way to debug nightwatch even within the command chains, or debug the injected code in chrome DevTools?
For instance, I want to debug the "window" object:
I use chrome Version 59.0.3071.115.
According to ChromeDriver - WebDriver for Chrome the DevTools is always disconnected from ChromeDriver as soon as the DevTools opens. Meaning, if I inject the code within the execute command (images) the DevTools will close and I have to reopen it again? Meaning, I cannot even debug it in the front end?
Thanks!

Apparently, the only way to debug or set breakpoints within the command queue is by means of callbacks, as shown in the following example.
Setting a breakpoint to inspect a page in browser
Sometimes it is important to inspect a page in browser in the middle
of a test case run. For example, to verify used selectors. To pause
execution at the right moment set a breakpoint inside a callback:
browser.perform(function () {
console.log('dummy statement'); // install a breakpoint here });
The example where taken from https://github.com/nightwatchjs/nightwatch/wiki/Understanding-the-Command-Queue.
Except:
Except for the execute command, because nightwatch injects the specified script directly into the browser, which will be executed right there. Moreover, chrome only allows one DevTools per page, hence it will try to reopen the DevTools each time a command must be executed.
DevTools window keeps closing
This is normal.
When you open the DevTools window, ChromeDriver is automatically
disconnected. When ChromeDriver receives a command, if disconnected,
it will attempt to close the DevTools window and reconnect.
Chrome's DevTools only allows one debugger per page. As of 2.x,
ChromeDriver is now a DevTools debugging client. Previous versions of
ChromeDriver used a different automation API that is no longer
supported in Chrome 29.
If you need to inspect something in DevTools, the best you can do now
is pause your test so that ChromeDriver won't close DevTools. When you
are done inspecting things in Chrome, you can unpause your test and
ChromeDriver will close the window and continue.
Source: https://sites.google.com/a/chromium.org/chromedriver/help/devtools-window-keeps-closing

Related

Cannot debug popup html and js. Despite having a `debugger;`

I created an extension that sendMessage's a variable into the content script. Which then returns data back to the popup JS code. Thing is, it stopped working.
I've been trying to debug this problem via inspect popup but it opens a window and immediately just closes. I've added a debugger; everywhere even at the beginning of the popup js and nothing happens. Nada.
Does anyone know how to really properly debug a popup js code?

Chrome Extension callback when restarted or shut down

Is there a chrome extension callback to detect when an extension is restarted or the browser is closed?
I see chrome.runtime.onSuspend but can not get that to be called.
Is there a chrome extension callback to detect when an extension is restarted
Yes, chrome.runtime.onStartup will fire when the extension is loaded for any reason, e.g. the browser is started or the extension is restarted.
or the browser is closed?
No, you don't get any event in this case. Chrome won't wait for extensions if the last window of the browser is requested to close.
chrome.runtime.onSuspend is only for the non-persistent event pages unloading while the browser continues to run.

When you execute chrome browser using Chrome DevTools Protocol, where does such session store stuff like history, cookies and extensions added?

I realized today that you can merge Chrome DevTools Protocol with Selenium in order to automate some very specific parts of a process within a website.
for instance: after some initial conditions have met, automate the process of uploading some files to an account and etc...
According to the official repository you use a sentence like the following on cmd to create a new chrome session with your user data:
chrome.exe --remote-debugging-port=9222 --user-data-dir:"C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data"
So in my case, the sentence above generates the following output:
The thing is, in my original session I had some Chrome extensions added, and I know that If I were to work only with Selenium using its chromedriver.exe, I could easily add an extension (which must be compressed as a .crx file) by using the following sentence:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options() #the variable that will store the selenium options
opt.add_extension(fr'{extension_path}')
But it seems that Chrome DevTools Protocol can't just add as much Options as Selenium, so I would have to install all my extensions in this pseudo session of mine again, no problem.
But, after installing such extensions, will those keep installed and ready for use after I execute again chrome.exe --remote-debugging-port=9222 --user-data-dir:"C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data", and if so, where?
Or if not, does that mean that I would have to re install those every single time I need to do tests with Chrome DevTools Protocol and my Chrome extensions? Thanks in advice.
Can confirm, a session opened with Chrome DevTools Protocol somehow stores permanently the extensions you re installed. It also remembers if you used some specific credentials for logging in to some sites.

Mute a tab or entire browser with selenium in NodeJS (firefox)

How would I go about muting a tab or entire browser with NodeJS selenium-webdriver module in firefox
I can't find any docs or any methods on how to do this for javascript
Any ideas?

Devtools chrome extension: Run script at start of every frame/page load?

I'm writing a devtools Chrome extension with a dev panel. The devtools script can use chrome.devtools.inspectedWindow.reload to reload the page and run a script before other scripts run (in every frame in the inspected window). However, the injected script is no longer run for subsequent page reloads and newly created frames.
How can I inject a script into the inspected window that is run at the start of every subsequent page load in the inspected window?
I know I can use a content script that runs at document_start, but that would run the script at the start of each page load regardless of whether the dev panel is open - and the script is intensive, so I'd like to avoid running the script when it's not needed.
Could the devtools script somehow listen for the beginning of page loads in the inspected window and respond by running a script in the page's context?
One option that you can use to avoid running the script when it's not needed, as you have said, is programmatic injection. As discussed:
Inserting code into a page programmatically is useful when your JavaScript or CSS code shouldn't be injected into every single page that matches the pattern — for example, if you want a script to run only when the user clicks a browser action's icon.
To insert code into a page, you must have the following:
your extension must have cross-origin permissions for the page.
It also must be able to use the chrome.tabs module.
You can get both kinds of permission using the manifest file's permissions field.
Once you have permissions set up, you can inject JavaScript into a page by calling tabs.executeScript.
As also discussed in chrome.devtools.inspectedWindow in executing code in the inspected window, use the tabs.executeScript method unless you need the specific functionality that the eval method provides.
However in Communicating Between Extension Components, please note that:
The DevTools page can't call tabs.executeScript directly. To inject a content script from the DevTools page, you must retrieve the ID of the inspected window's tab using the inspectedWindow.tabId property and send a message to the background page. From the background page, call tabs.executeScript to inject the script.
You may go through the given documentations for more information and examples.

Resources