6

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

cmpl
  • 85
  • 1
  • 9
  • maybe close the driver and create new one? – Dude May 19 '15 at 08:59
  • That wouldn't disable the connection of any driver. Rather using a closed driver causes errors. – Hubert Grzeskowiak May 19 '15 at 09:10
  • perhaps write some javascript with a timeout and inject this into the page with webdriver – Jay Byford-Rew May 19 '15 at 11:39
  • @kernowcode Can you elaborate a bit more? – cmpl May 19 '15 at 11:43
  • perhaps this may help http://stackoverflow.com/questions/16091243/does-chrome-have-a-work-offline-option perhaps you could set the flag via webdriver, do you test, then test it back. You can open a new tab with webdriver, switch tabs, then switch back. – Jay Byford-Rew May 19 '15 at 15:07
  • Following the idea from @kernowcode: for Mozilla there is a `network.online=true/false` https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Mozilla_networking_preferences. It is not very well documented what it does exactly, but maybe it helps. Also you could utilize a simple proxy you can disconnect at will. – sschrass May 20 '15 at 08:07
  • I tried to play with that flag and it doesn't seem to do anything. If you play with it manually it doesn't switch the browser to offline mode, at least not during the same browser session. Maybe it works if I close the browser and open it again, didn't even tried because if that is the way it works I will need to get around the same problem. Thanks – cmpl May 21 '15 at 10:12

3 Answers3

1

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

1

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");

nannan
  • 11
  • 1
  • 2
    or if just want to let chrome driver offline , then u can using an incorrect proxy' System.setProperty("webdriver.chrome.driver", "c:\\tools\\chromedriver.exe"); Proxy proxy = new Proxy(); proxy.setHttpProxy(proxyUrl); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, proxy); WebDriver driver = new ChromeDriver(capabilities); ' – nannan Dec 11 '15 at 09:19
0

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);
});
Rom1
  • 3,167
  • 2
  • 22
  • 39