Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set capability on already running selenium webdriver

Tags:

java

selenium

In selenium test step (like a button click) i want to prevent the selenium waiting for page finish loading. I cant throw the load Exception because then i cant work with the page anymore. Its possible to do a simmilar thing like this:

DesiredCapabilities dr = DesiredCapabilities.chrome();
dr.setCapability("pageLoadStrategy", "none");
WebDriver driver = new RemoteWebDriver(new URL("...."), dr);

What I want is like "dr.setCapability("pageLoadStrategy", "none");" but just for one specifique step.

Does anyone know a way to do this?

like image 398
t-dias Avatar asked Sep 14 '17 16:09

t-dias


People also ask

How do I set desired capabilities in Chrome Selenium?

To declare Desired Capabilities in Selenium automation testing using Grid, we can use the setCapability method from the DesiredCapabilities class to set the different types of capabilities of the browser (Ex. Chrome, IE, Firefox, Edge) platform name (Ex. Windows, macOS, etc.).

Can Selenium interact with already opened browser?

New Selenium IDEWe can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

What is use of desired capabilities in Selenium WebDriver?

It gives facility to set the properties of browser. Such as to set BrowserName, Platform, Version of Browser. Mostly DesiredCapabilities class used when do we used Selenium Grid. We have to execute mutiple TestCases on multiple Systems with different browser with Different version and Different Operating System.


1 Answers

Capabilities are no longer editable once the browser is launched. One way to temporary disable the waiting is to implement your own get with a script injection.

Something like this:

// 
// loads the page and stops the loading without exception after 2 sec if 
// the page is still loading.
//

load(driver, "https://httpbin.org/delay/10", 2000); 
public static void load(WebDriver driver, String url, int timeout) {
  ((JavascriptExecutor)driver).executeScript(
    "var url = arguments[0], timeout = arguments[1];"
    "window.setTimeout(function(){window.location.href = url}, 1);" +
    "var timer = window.setTimeout(window.stop, timeout);" +
    "window.onload = function(){window.clearTimeout(timer)}; "
    , url, timeout);
}
like image 141
Florent B. Avatar answered Oct 12 '22 04:10

Florent B.