Electron testing - WebdriverIO switch between the devtools window and main app window - javascript

We need to test Electron App.
We are using Spectron which is using ChromeDriver and WebdriverIO (Selenium 2.0 bindings for NodeJS).
Problem: Our application starts with open dev tools window than main application window is shown. Webdriver connects to dev tools window instead of the main window. We are unable to switch to main window.
Example code:
var app = new Application({
path: cfg.pathToElectron,
args: [cfg.pathToSA]
});
app.start().then(function(){
app.client // <- this is dev tools window instead of main window
// this closes the dev tools which is ok but we need to switch to main window
app.client.close();
// things like this doesn't help
app.client.execute('xxx.getCurrentWindow().closeDevTools()');
});
Any ideas how to switch from dev tools to main window?

You know that feeling when you ask a question and than immediately find the answer?
The solution is to call windowByindex() from the Spectron API. You need to call the API functions from the Spectron for this, not the functions from the Webdriver.
So solution to our problem is:
app.start().then(function(){
app.client.windowByIndex(1);
});

Related

WebDriver Sampler in Jmeter - switch to new window, allow mic and cam in dialog box, perform actions, close new window, return back to initial window

I'm working on following scenario with WebDriver Sampler in Jmeter on javascript:
perform several requests in main window;
for 1 specific transaction:
2.1. - switch from initial window to newly created window (both windows have the same title),
2.2. - allow mic and cam (browser dialog box appears after new window is open and blocks activity of some page elements until cam and mic will not be allowed),
2.3. - find elements on page and perform actions with them (I wrote javascript snippets for that),
2.4. - close this 2nd window;
return back to initial main window;
proceed with requests in main window.
For steps 1 and 4 I have in js code all I need.
For step3, I suppose to use solution:
//switch back to initial main window:
WDS.browser.switchTo().defaultContent();
But I'm still stack on 2.1 and 2.2.
What is the best solution with 2.1, 2.2 and 2.4?
For 2.2. I tried to use "Set Preferences" section in Firefox WebDriver config and populate it with following:
Disadvantages:
1st - unfortunatelly, it doesn't work, and 2nd - I need these preferences working with Chrome, not only in FF.
Could you provide helpful tips, please, with 2.1, 2.2, 2.4?
I don't think this modal popup is a "window" controllable by WebDriver, it's rather a native window of the operating system hence you won't be able to interact with it using WebDriver functions, the options are in:
Simulate keyboard inputs using java.awt.Robot class
Locating the window using underlying operating system methods and sending the corresponding message to it, the entry point is JNI
Suppress these popups on browser startup level:
for Firefox you can load a custom profile with camera and mic allowed for the site you're testing
for Chromium and derivatives you can set the following ChromeOptions
use-fake-ui-for-media-stream
use-fake-device-for-media-stream
Unfortunately both approaches are not available out of the box for the Firefox Driver Config and Chrome Driver Config so you will have to switch to JSR223 Sampler and Groovy language for implementing your test scenario

How can I turn off the workbox browser console messages?

Very simply, I would like to disable the display of the repeated workbox messages that appear in my browser console while I am debugging. For instance, I don't need to see:
WorkBox: Using NetworkFirst to respond to '/fonts/KFOlCnqEu92Fr1MmEU9fBBc-.woff'
It clutters my FireFox console and it is something I dislike very much. If you like it, fine, please don't try to change my mind about the benefit of such useless (to me) messages.
Do you know how to turn it off?
For info sake, I am using Quasar and Vue to create a SPA - not even a PWA.
Thanks.
Simply add self.__WB_DISABLE_DEV_LOGS = true at the top of your service worker (sw.js) file.
Contrarily to what answers posted here say, the solution is not:
to unregister your service worker to get rid of the messages. Your app may need it to run properly
to add workbox.setConfig({debug: false}) unless knowing what it does:
it switches between a production build and a debug build. workbox automatically selects the debug build when running on localhost.
For me worked:
Console -> Application tab -> Service workers -> sw.js unregister
You can use workbox.setConfig({ debug: false }); in order to use production build and remove extra logging, otherwise adjust your web console log level filtering accordingly.
Doc : https://developers.google.com/web/tools/workbox/guides/troubleshoot-and-debug
You add this setting in your service worker definition file, after the import. For example:
importScripts(`https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js`);
if (workbox) {
console.log(`Yay! Workbox is loaded 😁`);
} else {
console.log(`Boo! Workbox didn't load 😬`);
}
// Switch debug logging on/off here. Default is on in dev and off in prod.
workbox.setConfig({debug: false});
For more information on this see https://developers.google.com/web/tools/workbox/guides/configure-workbox#configure_debug_builds_vs_production_builds
Thanks to the answer provided by Antonina K, I was able to locate an answer for FireFox. In case anyone else needs this. As Antonina mentioned, in Chrome, the console has an application tab that has references to all the service workers used by the browser. FireFox does not have the tab (or, at least my version does not).
In FireFox, open a new tab and place about:serviceworkers in the address bar. Scroll through the list to find the workbox service worker. For me, it was listed as localhost:8080. I deregistered that worker and I no longer see the multitude of workbox messages in my console. I can finally debug my app again!
Here is the link that I referenced to fix the problem:
Manage Service Workers in FireFox and Chrome

How to open browser with open console [duplicate]

I want to ask how to open the Chrome developer Console during selenium tests execution. Currently, when tests are executing, and I open the console manually hitting F12, the tests stop responding immediately and fails after some time.
Can anyone tell me how can I initiate my tests with developer console opened, so I can catch/observe the console errors that occur during test execution.
Use --auto-open-devtools-for-tabs:
This flag makes Chrome auto-open DevTools window for each tab. It is intended to be used by developers and automation to not require user interaction for opening DevTools.
Source
How to use
Note: this answer does not apply to current versions of Chrome.
You can't. The Chrome driver uses the Chrome remote debugging protocol to communicate with the browser. This is the same protocol that the developer console uses also. Unfortunately, Chrome is designed so that only one client can be attached using the protocol at a time, so that means either the developer tools, or the driver, but not both simultaneously.
Have you tried simulating the key press events for the shortcut of opening the dev tools in Chrome?
String openDevTools = Keys.chord(Keys.ALT, Keys.CONTROL, "i");
driver.findElement(By.ByTagName("body")).sendKeys(openDevTools);
This is not ideal and in a rigorous testing regime you would need platform detection to ensure you are covering both Mac and Windows. I would absolutely recommend avoiding this (even if it works), but it's a possible as a work-around if you really must.
I have a feeling it may also lose focus of the window itself if you do this. If this is the case, you'd need something like the following: -
String parentHandle = driver.getWindowHandle(); // get the current window handle
// do your dev tool stuff here
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps.
Useful link if it does get you anywhere: How to handle the new window in Selenium WebDriver using Java?
Edit: Just re-read the question and don't think this will work anyway. Your unit tests should capture errors in the logic of your code. Your selenium tests should only test user journeys and capture errors when the user journey is cut short. You should never be testing code logic/error throwing through a selenium test.
This is working for me in webdriver.io (wdio.conf.js)
const configs = {
chrome : {
maxInstances: "5",
browserName: "chrome",
chromeOptions: {
args: ['--window-size=1280,800', '--auto-open-devtools-for-tabs'],
binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
}
},
firefox : {
maxInstances: "5",
browserName: "firefox"
},
headless : {
maxInstances: "5",
browserName: "chrome",
chromeOptions: {
args: ['--headless', '--disable-gpu', '--window-size=1280,800'],
binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
}
},
}

How to add "open link in app mode" to right-click menu in chrome?

app mode: chrome window without navigation panel(address+tab bars). Run this in terminal
google-chrome --app=http://stackoverflow.com/
I want to open a website in app mode directly from chrome. Is there an extension that adds such option? If not how do I write a small extension that does just that? I never wrote a chrome extension but I have some experience with html and javascript. Thanks
Edit: Main issue is chrome.windows.create has no "app" option for CreateType. I guess we can't do anything about it.
There is a way using chrome.management API.
chrome.management.generateAppForLink("http://stackoverflow.com/", "Stack Overflow", function(info) {
chrome.management.setLaunchType(info.id, "OPEN_AS_WINDOW", function() {
chrome.management.launchApp(info.id);
})
});
Note that the above code requires a user gesture (which is undocumented). For examples, see Invoking activeTab. Activating a context menu should be sufficient as a gesture.
However, this will create an app in the app launcher permanently. On the plus side, it will not create duplicates for the same URL/Title.
You can call chrome.management.uninstall(id), but it will require a confirmation from the user.

gInitialPages is not defined bootstrapped extensions

I am working on Firefox extension that overwrite new tab page and I need to hide my page URL from address bar. I use this code:
if (gInitialPages.indexOf(NEW_TAB_URL)===-1) gInitialPages.push(NEW_TAB_URL);
It works correctly in XUL Overlay code, but I'm getting an error when I try to make my application restartless and move this code to bootstrap.js:
gInitialPages is not defined
So, how can I use gInitialPages (or anything similar) in bootstrapped extensions?
Bootstrapped/restartless extensions do NOT automagically run in the context of (a) window(s).
bootstrap.js runs in an own context, only once per application instance, not in the browser window.
You'll need to:
Manually enumerate all existing browser windows.
Listen for new browser windows as they are opened.
And then manipulate the variable in those windows.
See Mardak's example on how you could do that, in particular watchWindows and unload.

Categories