Skip to content

Instantly share code, notes, and snippets.

@artkoshelev
Created March 4, 2014 13:52
Show Gist options
  • Save artkoshelev/9346848 to your computer and use it in GitHub Desktop.
Save artkoshelev/9346848 to your computer and use it in GitHub Desktop.
diff --git a/src/config.cpp b/src/config.cpp
index 16b6d6e..29e39f3 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -73,6 +73,7 @@ static const struct QCommandLineConfigEntry flags[] =
{ QCommandLine::Option, '\0', "webdriver-logfile", "File where to write the WebDriver's Log (default 'none') (NOTE: needs '--webdriver') ", QCommandLine::Optional },
{ QCommandLine::Option, '\0', "webdriver-loglevel", "WebDriver Logging Level: (supported: 'ERROR', 'WARN', 'INFO', 'DEBUG') (default 'INFO') (NOTE: needs '--webdriver') ", QCommandLine::Optional },
{ QCommandLine::Option, '\0', "webdriver-selenium-grid-hub", "URL to the Selenium Grid HUB: 'URL_TO_HUB' (default 'none') (NOTE: needs '--webdriver') ", QCommandLine::Optional },
+ { QCommandLine::Option, '\0', "webdriver-selenium-grid-remote-proxy-class", "Sets the remote proxy class used by the hub for the node, e.g. '--webdriver-selenium-grid-remote-proxy-class=org.openqa.grid.selenium.proxy.DefaultRemoteProxy'", QCommandLine::Optional },
{ QCommandLine::Param, '\0', "script", "Script", QCommandLine::Flags(QCommandLine::Optional|QCommandLine::ParameterFence)},
{ QCommandLine::Param, '\0', "argument", "Script argument", QCommandLine::OptionalMultiple },
{ QCommandLine::Switch, 'w', "wd", "Equivalent to '--webdriver' option above", QCommandLine::Optional },
@@ -123,6 +124,8 @@ void Config::processArgs(const QStringList &args)
argsForGhostDriver << QString("--ip=%1").arg(m_webdriverIp); //< "--ip=IP"
argsForGhostDriver << QString("--port=%1").arg(m_webdriverPort); //< "--port=PORT"
+ argsForGhostDriver << QString("--proxy=%1").arg(m_webdriverSeleniumGridRemoteProxyClass); //< "--proxy=FQCN"
+ argsForGhostDriver << QString("--version=%1").arg(PHANTOMJS_VERSION_STRING);
if (!m_webdriverSeleniumGridHub.isEmpty()) {
argsForGhostDriver << QString("--hub=%1").arg(m_webdriverSeleniumGridHub); //< "--hub=SELENIUM_GRID_HUB_URL"
@@ -562,6 +565,7 @@ void Config::resetToDefaults()
m_webdriverLogFile = QString();
m_webdriverLogLevel = "INFO";
m_webdriverSeleniumGridHub = QString();
+ m_webdriverSeleniumGridRemoteProxyClass = QString();
}
void Config::setProxyAuthPass(const QString &value)
@@ -728,6 +732,9 @@ void Config::handleOption(const QString &option, const QVariant &value)
if (option == "webdriver-selenium-grid-hub") {
setWebdriverSeleniumGridHub(value.toString());
}
+ if (option == "webdriver-selenium-grid-remote-proxy-class") {
+ setWebdriverSeleniumGridRemoteProxyClass(value.toString());
+ }
}
void Config::handleParam(const QString& param, const QVariant &value)
@@ -772,3 +779,13 @@ void Config::setSslCertificatesPath(const QString& sslCertificatesPath)
m_sslCertificatesPath = sslCertificatesPath;
}
}
+
+void Config::setWebdriverSeleniumGridRemoteProxyClass(const QString& webdriverSeleniumGridRemoteProxyClass)
+{
+ m_webdriverSeleniumGridRemoteProxyClass = webdriverSeleniumGridRemoteProxyClass;
+}
+
+QString Config::webdriverSeleniumGridRemoteProxyClass() const
+{
+ return m_webdriverSeleniumGridRemoteProxyClass;
+}
\ No newline at end of file
diff --git a/src/config.h b/src/config.h
index 84c8e09..5cddc8c 100644
--- a/src/config.h
+++ b/src/config.h
@@ -63,6 +63,7 @@ class Config: public QObject
Q_PROPERTY(QString webdriverLogFile READ webdriverLogFile WRITE setWebdriverLogFile)
Q_PROPERTY(QString webdriverLogLevel READ webdriverLogLevel WRITE setWebdriverLogLevel)
Q_PROPERTY(QString webdriverSeleniumGridHub READ webdriverSeleniumGridHub WRITE setWebdriverSeleniumGridHub)
+ Q_PROPERTY(QString webdriverSeleniumGridRemoteProxyClass READ webdriverSeleniumGridRemoteProxyClass WRITE setWebdriverSeleniumGridRemoteProxyClass)
public:
Config(QObject *parent = 0);
@@ -176,6 +177,9 @@ public:
void setWebdriverSeleniumGridHub(const QString& hubUrl);
QString webdriverSeleniumGridHub() const;
+ void setWebdriverSeleniumGridRemoteProxyClass(const QString& webdriverSeleniumGridRemoteProxyClass);
+ QString webdriverSeleniumGridRemoteProxyClass() const;
+
public slots:
void handleSwitch(const QString &sw);
void handleOption(const QString &option, const QVariant &value);
@@ -227,6 +231,7 @@ private:
QString m_webdriverLogFile;
QString m_webdriverLogLevel;
QString m_webdriverSeleniumGridHub;
+ QString m_webdriverSeleniumGridRemoteProxyClass;
};
#endif // CONFIG_H
diff --git a/src/ghostdriver/session.js b/src/ghostdriver/session.js
index 2bf7247..6c021ce 100644
--- a/src/ghostdriver/session.js
+++ b/src/ghostdriver/session.js
@@ -40,6 +40,10 @@ ghostdriver.Session = function(desiredCapabilities) {
LOG_TYPES : {
HAR : "har",
BROWSER : "browser"
+ },
+ PROXY_TYPES : {
+ MANUAL : "manual",
+ DIRECT : "direct"
}
};
@@ -63,8 +67,8 @@ ghostdriver.Session = function(desiredCapabilities) {
"acceptSslCerts" : false, //< TODO
"nativeEvents" : true, //< TODO Only some commands are Native Events currently
"proxy" : { //< TODO Support more proxy options - PhantomJS does allow setting from command line
- "proxyType" : "direct"
- }
+ "proxyType" : _const.PROXY_TYPES.DIRECT
+ },
},
_negotiatedCapabilities = {
"browserName" : _defaultCapabilities.browserName,
@@ -106,10 +110,34 @@ ghostdriver.Session = function(desiredCapabilities) {
_inputs = ghostdriver.Inputs(),
_capsPageSettingsPref = "phantomjs.page.settings.",
_capsPageCustomHeadersPref = "phantomjs.page.customHeaders.",
+ _capsPageSettingsProxyPref = "proxy",
_pageSettings = {},
_pageCustomHeaders = {},
_log = ghostdriver.logger.create("Session [" + _id + "]"),
- k, settingKey, headerKey;
+ k, settingKey, headerKey, proxySettings;
+
+ var
+ /**
+ * Parses proxy JSON object and return proxy settings for phantom
+ *
+ * @param proxyCapability proxy JSON Object: @see https://code.google.com/p/selenium/wiki/DesiredCapabilities
+ */
+ _getProxySettingsFromCapabilities = function(proxyCapability) {
+ var proxySettings = {};
+ if (proxyCapability["proxyType"].toLowerCase() == _const.PROXY_TYPES.MANUAL) { //< TODO: support other options
+ if (proxyCapability["httpProxy"] !== "null") { //< TODO: support other proxy types
+ var urlParts = proxyCapability["httpProxy"].split(':');
+ proxySettings["ip"] = urlParts[0];
+ proxySettings["port"] = urlParts[1];
+ proxySettings["proxyType"] = "http";
+ proxySettings["user"] = "";
+ proxySettings["password"] = "";
+
+ return proxySettings;
+ }
+ }
+ return proxySettings;
+ };
// Searching for `phantomjs.settings.* and phantomjs.customHeaders.*` in the Desired Capabilities and merging with the Negotiated Capabilities
// Possible values for settings: @see https://github.com/ariya/phantomjs/wiki/API-Reference#wiki-webpage-settings.
@@ -129,6 +157,10 @@ ghostdriver.Session = function(desiredCapabilities) {
_pageCustomHeaders[headerKey] = desiredCapabilities[k];
}
}
+ if (k.indexOf(_capsPageSettingsProxyPref) === 0) {
+ proxySettings = _getProxySettingsFromCapabilities(desiredCapabilities[k]);
+ phantom.setProxy(proxySettings["ip"], proxySettings["port"], proxySettings["proxyType"], proxySettings["user"], proxySettings["password"]);
+ }
}
var
@@ -700,4 +732,3 @@ ghostdriver.Session = function(desiredCapabilities) {
getLogTypes: _getLogTypes
};
};
-
diff --git a/src/phantom.cpp b/src/phantom.cpp
index 519e56e..43a1904 100644
--- a/src/phantom.cpp
+++ b/src/phantom.cpp
@@ -105,23 +105,7 @@ void Phantom::init()
QString proxyType = m_config.proxyType();
if (proxyType != "none") {
- if (m_config.proxyHost().isEmpty()) {
- QNetworkProxyFactory::setUseSystemConfiguration(true);
- } else {
- QNetworkProxy::ProxyType networkProxyType = QNetworkProxy::HttpProxy;
-
- if (proxyType == "socks5") {
- networkProxyType = QNetworkProxy::Socks5Proxy;
- }
-
- if(!m_config.proxyAuthUser().isEmpty() && !m_config.proxyAuthPass().isEmpty()) {
- QNetworkProxy proxy(networkProxyType, m_config.proxyHost(), m_config.proxyPort(), m_config.proxyAuthUser(), m_config.proxyAuthPass());
- QNetworkProxy::setApplicationProxy(proxy);
- } else {
- QNetworkProxy proxy(networkProxyType, m_config.proxyHost(), m_config.proxyPort());
- QNetworkProxy::setApplicationProxy(proxy);
- }
- }
+ setProxy(m_config.proxyHost(), m_config.proxyPort(), proxyType, m_config.proxyAuthUser(), m_config.proxyAuthPass());
}
// Set output encoding
@@ -407,6 +391,29 @@ bool Phantom::injectJs(const QString &jsFilePath)
return Utils::injectJsInFrame(pre + jsFilePath, libraryPath(), m_page->mainFrame());
}
+void Phantom::setProxy(const QString &ip, const qint64 &port, const QString &proxyType, const QString &user, const QString &password)
+{
+ qDebug() << "Set " << proxyType << " proxy to: " << ip << ":" << port;
+ if (ip.isEmpty()) {
+ QNetworkProxyFactory::setUseSystemConfiguration(true);
+ }
+ else {
+ QNetworkProxy::ProxyType networkProxyType = QNetworkProxy::HttpProxy;
+
+ if (proxyType == "socks5") {
+ networkProxyType = QNetworkProxy::Socks5Proxy;
+ }
+ // Checking for passed proxy user and password
+ if(!user.isEmpty() && !password.isEmpty()) {
+ QNetworkProxy proxy(networkProxyType, ip, port, user, password);
+ QNetworkProxy::setApplicationProxy(proxy);
+ } else {
+ QNetworkProxy proxy(networkProxyType, ip, port);
+ QNetworkProxy::setApplicationProxy(proxy);
+ }
+ }
+}
+
void Phantom::exit(int code)
{
if (m_config.debug()) {
diff --git a/src/phantom.h b/src/phantom.h
index d515d53..4ff34d7 100644
--- a/src/phantom.h
+++ b/src/phantom.h
@@ -168,6 +168,15 @@ public slots:
*/
void clearCookies();
+ /**
+ * Set the application proxy
+ * @brief setProxy
+ * @param ip The proxy ip
+ * @param port The proxy port
+ * @param proxyType The type of this proxy
+ */
+ void setProxy(const QString &ip, const qint64 &port = 80, const QString &proxyType = "none", const QString &user = NULL, const QString &password = NULL);
+
// exit() will not exit in debug mode. debugExit() will always exit.
void exit(int code = 0);
void debugExit(int code = 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment