2

I was able to make the following work in Java:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");

Now, I am using the Java port of Selenium2Library for Robot Framework. How can I do something similar to the one above? I've tried the following:

${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
${chrome_capabilities}=    Evaluate    sys.modules['selenium.webdriver'].DesiredCapabilities.CHROME    sys, selenium.webdriver
Call Method    ${chrome_options}    add_argument    disable-extensions
Set To Dictionary    ${chrome_capabilities}    ChromeOptions.CAPABILITY=${chrome_options}
Open Browser    https://www.google.com    Chrome    None    None        desired_capabilities=${chrome_capabilities}    None

There's a pop-up that appears every time I open a browser, that's why I need to disable chromeOptions. As mentioned above, I was able to make the pop-up disappear using the Java code. I just couldn't do the same with RF.

Thanks for your help.

tic
  • 83
  • 1
  • 9

3 Answers3

0

Pass the Chrome options via the desiredCapabilities argument to Open Browser as a JSON string. If you create a capabilities object in Java, use the toJson method to create a JSON object. Then use the getAsString method on the JSON object to get a string. Then pass that string as the value for desiredCapabilities. So, essentially you need to create a Java user keyword that returns a desired capabilities string.

# the below should return something similar to {'platform': 'ANY', 'browserName': 'chrome', 'version': '', 'chromeOptions': {'args': ['--disable-extensions'], 'extensions': []}, 'javascriptEnabled': True}
${desired caps}    Get Capabilities    # call custom keyword to get capabilities string
Open Browser    https://stackoverflow.com    gc    desiredCapabilities=${desired caps}

public String getCapabilities() {}
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-extensions");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    return capabilities.toJson().getAsString();
}
ombre42
  • 2,344
  • 15
  • 21
  • Hello, thanks for you answer. I've tried it. But it seems that toJson() method is not available for DesiredCapabilities class. It's only for ChromeOptions class. So I can't return capabilities as string. – tic Aug 02 '16 at 06:28
  • I was able to return desiredCapability as string using: return capabilities.getCapability(ChromeOptions.CAPABILITY).toString();... However, I've encountered: "Invalid desiredCapabilities: desired_capabilities=org.openqa.selenium.chrome.ChromeOptions@13473b89. – tic Aug 02 '16 at 07:24
  • I was able to pass chromeOption as a desiredCapability in JSON format. The value is 'chromeOptions': u'org.openqa.selenium.chrome.ChromeOptions@13473b89'. However, I believe it would still Not be possible to disable extensions in Chrome as of the moment. This is because the current Java port of Selenium2Library does not import ChromeOptions. Any idea guys? – tic Aug 03 '16 at 03:30
  • I was trying not to recommend this approach, but maybe its for the best. See http://stackoverflow.com/a/18261156/2532697. The package names are wrong for newer versions. Basically instead of fighting to programmatically generating the capabilities string, just create your own webdriver instance and inject it into selenium2library. – ombre42 Aug 03 '16 at 13:37
  • Thanks for the answer. Sorry I just got a little confused. You can open a browser using RF with Selenium2Library and get the WebDriver instance and use it with your custom library. Is the opposite possible? Open a browser using your custom library(so i can set chromeOptions), then get the WebDriver instance and use it in RF with Selenium2Libary? – tic Aug 05 '16 at 08:07
  • Sorry about that. The referenced solution has a getWebDriverCache keyword. Once you have a reference to the webDriverCache, you can call the register method with the instance of webDriver you created (see https://github.com/MarkusBernhardt/robotframework-selenium2library-java/blob/master/src/main/java/com/github/markusbernhardt/selenium2library/utils/WebDriverCache.java#L44) – ombre42 Aug 05 '16 at 17:35
  • Thanks again. Using keyword "com.github.markusbernhardt.selenium2library.keywords.BrowserManagement.Get Web Driver Cache" returns the webDriverCache... Your "Misc.Get Web Driver Cache" unfortunately doesn't. It gives the error: "java.lang.ClassCastException: Selenium2Library cannot be cast to com.github.markustbernhardt.selenium2library.keywords.BroswerManagement". Using the first one, I can't get the current webDriver. I'm still investigating where I went wrong with the second one. – tic Aug 09 '16 at 09:37
0

You can add the below two arguments in Chrome options; it won’t ask again to accept.

 Call Method    ${chrome_options}    add_argument    --ignore-certificate-errors

 Call Method    ${chrome_options}    add_argument    --ignore-urlfetcher-cert-requests
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
-3

Instead of using "Open Browser" use the "Create Webdriver" keyword.

Helio
  • 3,322
  • 1
  • 14
  • 23