Selenium test, disconnect browser connection during test and enable it again

I run the test using Selenium Webdriver (Java) and halfway through the test. I want to disable my browser, follow a few steps and re-enable the connection to the browser. Is there an easy way to do this, or perhaps change the browser proxy to nonexistent (offline emulation) and return to something real again? I need to save browser cache, browser local storage area and browser cookies between online, offline and online again.

thanks

+6
source share
3 answers

Perhaps you can fake it by setting the desired WebDrivers PageLoadTimeout.

In C #, this worked for me:

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

I'm sure in Java it will be something like this:

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

After you finish doing this, you can return it for 30 seconds or something else, as some messages indicated that it was the default.

Source for the LoadTimeout page: 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
source

if u uses java and windows 7 then you can call cmd and pass release to disconnect the network and use / update 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");

+1
source

I am struggling with this exact problem.

If you are ready to look beyond selenium, then chrome-remote-interface can substitute an account.

Here is the 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); }); 
0
source

Source: https://habr.com/ru/post/987489/


All Articles