Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

selenium

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

like image 692
cmpl Avatar asked May 19 '15 08:05

cmpl


People also ask

How do I go back to browser using Selenium?

We can also move back in the browser with the help of a Javascript Executor in Selenium. It has the execute_script() method which allows Selenium to run Javascript commands. We have to execute the Javascript command window. history.go(-1) to go back to the previous page.

Can we use Selenium to work with an already open browser session?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

What is difference between close and quit in Selenium?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.

How do I switch to second window in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.


2 Answers

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

like image 137
Storm Blast Avatar answered Sep 19 '22 22:09

Storm Blast


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

like image 31
nannan Avatar answered Sep 16 '22 22:09

nannan