【发布时间】:2018-03-09 09:01:16
【问题描述】:
尝试将我们的代码更新到 Selenium 3.x,但在尝试运行我们的测试时不断遇到错误:
error: cannot access MutableCapabilities
就运行测试而言,同样的代码过去一直有效,但我不确定它在哪里或为什么会出现如此奇怪的错误。我似乎找不到任何人以前写过的东西,所以我希望 stackoverflow 社区可以帮助我。
这是产生此错误的代码:
package com.iacapps.ste.ta.helpers;
import com.google.common.base.Strings;
import com.paypal.selion.platform.grid.browsercapabilities.DefaultCapabilitiesBuilder;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
public class CustomCapabilities extends DefaultCapabilitiesBuilder
{
private static final Logger logger = LoggerFactory.getLogger(DefaultCapabilitiesBuilder.class);
private static final String SAUCE_TUNNEL_PROPERTY = "sauceTunnel";
private static final String SAUCE_ENABLED_PROPERTY = "enableSauceConnect";
private static final String TUNNEL_CAPABILITY = "tunnelIdentifier";
private static final String ACCEPT_ALL_SSL_CAPABILITY = "acceptSslCerts";
private static final String CHROME_SWITCHES = "chrome.switches";
private static final String CHROME_IGNORE_SSL = "--ignore-certificate-errors";
private static final String FIREFOX_ACCEPT_BAD_CERTS_CAPABILITY = "acceptInsecureCerts";
@Override
public DesiredCapabilities getCapabilities(DesiredCapabilities capabilities)
{
String sauceEnabledValue = System.getProperty(SAUCE_ENABLED_PROPERTY);
String tunnelIdValue = System.getProperty(SAUCE_TUNNEL_PROPERTY);
//This will just prevent the warning being printed when sauceconnect isn't enabled.
if (!Strings.isNullOrEmpty(sauceEnabledValue) && Boolean.valueOf(sauceEnabledValue))
{
if (Strings.isNullOrEmpty(tunnelIdValue))
{
logger.warn("{} not set", SAUCE_TUNNEL_PROPERTY);
}
else
{
capabilities.setCapability(TUNNEL_CAPABILITY, tunnelIdValue);
}
}
//There's a reason for this charlie foxtrot. I don't always get to know what browser driver I'm
//talking to.
//Per selenium docs: "Whether the session should accept all SSL certs by default."
//The DOWNSIDE: this seems to work with newer browser drivers, but it may not work with old ones.
capabilities.setCapability(ACCEPT_ALL_SSL_CAPABILITY, true);
//This *supposedly* works with some versions of IE.
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
//This *supposedly* works with some chrome versions.
capabilities.setCapability(CHROME_SWITCHES, Collections.singletonList(CHROME_IGNORE_SSL));
//Oh my god please work you STUPID FIREFOX
//http://stackoverflow.com/a/40788935
//https://bugzilla.mozilla.org/show_bug.cgi?id=1103196
//Should work with firefox > v51
capabilities.setCapability(FIREFOX_ACCEPT_BAD_CERTS_CAPABILITY,true);
//When in doubt SET EVERYTHING!
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
capabilities.setCapability(FirefoxDriver.PROFILE,profile);
capabilities.setCapability(FirefoxDriver.MARIONETTE,false);
return capabilities;
}
}
【问题讨论】:
-
这个类的目的到底是什么?你如何使用它?您真的需要扩展
DefaultCapabilitiesBuilder来创建一组自定义功能吗? -
我需要将浏览器 mob 代理代理信息附加到测试中。以及如果我使用saucelabs进行测试,我需要输入sauce connect代理信息。
-
没关系。但我看不出有任何理由在这里使用
DefaultCapabilitiesBuilder。您可以使用一个常见的 java 方法执行相同的操作,该方法返回DesiredCapabilities。这就是为什么我要你展示一个代码,在哪里使用这个类。 -
@SergeyKorol - 此构建器通过 JVM 参数或服务加载器注入。
标签: java selenium selenium-grid