Poc
Poc

Reputation: 59

WebdriverIO automation testing when JavaScript is disabled

Is there a way in WebdriverIO framework to launch the browser with JavaScript disabled?

I want to automate a scenario with JavaScript being disabled. But, when I manually disable the JavaScript in Chrome, or Firefox and run the WDIO scripts, the browser always opens with JavaScript enabled.

Upvotes: 0

Views: 1352

Answers (1)

iamdanchiv
iamdanchiv

Reputation: 4112

Not anymore. (but you have a workaround below)

This used to be easily achieved using the chromium switches. But considering all driver implementations (chromedriver, geckodriver, etc.) now require JavaScript to drive your spawned browser instance, it's no longer possible.

It was achieved via chromeOptions arguments/switches:

capabilities: [{

        maxInstances: 2,

        browserName: config[env].browser,
        chromeOptions: {

            args: ['--disable-javascript',
                   '--disable-javascript-harmony-shipping'
            ]
        }
    }]

!!! LATER EDIT: You can achieve this by loading a custom profile.

  1. Start your WebdriverIO test case, but add a browser.debug() after you load your page;
  2. In the address bar, type chrome://settings/content and in the modal, check the Do not allow any site to run JavaScript. Click Done. Now go to a random page and notice JavaScript has been blocked on it: enter image description here

  3. Now we have to save this custom profile and load it each time you start a WebdriverIO test case. Type chrome://version in your address bar. Notice the Profile Path value. Copy the content 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, extensions installed, accounts saved... in our case, JavaScript disabled option) on THIS current instance.

  4. Now all we 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'
        ]
    }
    

Now all you have to do is run your test cases with this custom profile and JavaScript will be blocked on all websites. Hope this is the behavior you are looking for as there is no other way to achieve this behavior.

Cheers!

Upvotes: 1

Related Questions