Things have changed. (but there's a workaround below)
In the past, it was easy to do this using the chromium switches. However, with all driver implementations (chromedriver
, geckodriver
, etc.) now requiring JavaScript to control your launched browser instance, it's no longer possible.
This used to be done through chromeOptions
arguments/switches:
capabilities: [{
maxInstances: 2,
browserName: config[env].browser,
chromeOptions: {
args: ['--disable-javascript',
'--disable-javascript-harmony-shipping'
]
}
}]
!!! UPDATE: You can still achieve this by using a custom profile.
- Start your WebdriverIO test case, but add a
browser.debug()
after you load your page;
In the address bar, type chrome://settings/content and in the modal, check the Do not allow any site to run JavaScript. Click Done. Visit a random page and see that JavaScript has been blocked on it: https://i.stack.imgur.com/Er2um.png
To save this custom profile and load it each time you start a WebdriverIO test case, type chrome://version in your address bar. Note the value of Profile Path. Copy the contents of the folder (e.g.: For
C:\Users\<yourUserName>\Desktop\scoped_dir18256_17319\Default
, copy the scoped_dir18256_17319 folder on your Desktop). This folder contains all the actions (search history, installed extensions, saved accounts... including the disabled JavaScript option) in THIS current instance.
All you need to do is add the path to that folder in your wdio.config.js
file as a chromeOptions
argument:
chromeOptions: {
//extensions: ['./browserPlugins/Avira-SafeSearch-Plus_v1.5.1.crx'],
args: [ '--user-data-dir=/Users/<yourUserName>/Desktop/scoped_dir18256_17319'
]
}
Simply run your test cases with this custom profile and JavaScript will be blocked on all websites. I hope this meets your requirements since there is no other way to accomplish this behavior.
Cheers!