cmpl
cmpl

Reputation: 85

selenium test, switch off browser connection during test and switch it on again

I'm running a test using Selenium Webdriver (Java) and half way through the test I want to set my browser to offline, execute a couple of steps and turn the browser connection on again. Is there an easy way to do this, or maybe change the browser proxy to an non existent one (emulate offline) and set back to something valid again? I need to keep browser cache, browser local storage area and browser cookies between online, offline and online again.

Thanks

Upvotes: 6

Views: 3888

Answers (3)

Rom1
Rom1

Reputation: 3207

I am struggling with this exact problem.

If you are willing to look beyond selenium, then chrome-remote-interface may foot the bill.

Here is a relevant code snippet

const CDP = require('chrome-remote-interface');
const fs = require('fs');

CDP(async (client) => {
    function setOffline(){
      return Network.emulateNetworkConditions({offline: true,
          latency: 100,
              downloadThroughput: 750 * 1024 / 8,
              uploadThroughput: 250 * 1024 / 8});
    }

    const {Page, Network} = client;
    try {
        await Page.enable();
        await Network.enable();
        await setOffline();
        await Page.navigate({url: 'https://github.com'});
        await Page.loadEventFired();
        const {data} = await Page.captureScreenshot();
        fs.writeFileSync('scrot.png', Buffer.from(data, 'base64'));
    } catch (err) {
        console.error(err);
    } finally {
        await client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

Upvotes: 0

nannan
nannan

Reputation: 11

if u are using java and windows 7 then u can call cmd and pass release to turn off the network and using /renew to turn on the network. it works for me . Process p = Runtime.getRuntime().exec("cmd /c ipconfig /release");
p = Runtime.getRuntime().exec("cmd /c ipconfig /renew");

Upvotes: 1

Storm Blast
Storm Blast

Reputation: 31

You may be able fake it by setting the WebDrivers PageLoadTimeout to zero.

In C# this worked for me:

driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0));

I'm Guessing in Java it would be something like this:

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);

After you're done doing that you could turn it back up to 30 seconds or something as some posts have indicated the default is.

Source for the pageLoadTimeout: https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html

And the default time: https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading

Upvotes: 1

Related Questions