2011-08-26 22:02:35 +04:00
/*
This file is part of the PhantomJS project from Ofi Labs .
2012-09-03 14:30:56 +04:00
Copyright ( C ) 2012 Ariya Hidayat < ariya . hidayat @ gmail . com >
2011-08-26 22:02:35 +04:00
Copyright ( C ) 2011 Ariya Hidayat < ariya . hidayat @ gmail . com >
Copyright ( C ) 2011 execjosh , http : //execjosh.blogspot.com
2013-01-14 22:55:20 +04:00
Copyright ( C ) 2013 James M . Greene < james . m . greene @ gmail . com >
2011-08-26 22:02:35 +04:00
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
* Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
* Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
* Neither the name of the < organization > nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED . IN NO EVENT SHALL < COPYRIGHT HOLDER > BE LIABLE FOR ANY
DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2011-08-28 16:05:34 +04:00
# include "config.h"
2011-08-26 22:02:35 +04:00
2011-08-26 22:03:06 +04:00
# include <QDir>
2013-02-28 02:50:51 +04:00
# include <QFileInfo>
2014-07-24 21:25:46 +04:00
# include <QtWebKitWidgets/QWebPage>
# include <QtWebKitWidgets/QWebFrame>
2011-09-26 08:51:44 +04:00
# include <QNetworkProxy>
2011-08-29 19:43:41 +04:00
# include "terminal.h"
2012-09-03 14:30:56 +04:00
# include "qcommandline.h"
2011-10-16 23:35:21 +04:00
# include "utils.h"
2013-03-18 05:30:24 +04:00
# include "consts.h"
2011-08-26 22:03:06 +04:00
2012-09-03 14:30:56 +04:00
# include <iostream>
2013-03-18 05:30:24 +04:00
2012-09-03 14:30:56 +04:00
static const struct QCommandLineConfigEntry flags [ ] =
{
{ QCommandLine : : Option , ' \0 ' , " cookies-file " , " Sets the file name to store the persistent cookies " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " config " , " Specifies JSON-formatted configuration file " , QCommandLine : : Optional } ,
2013-01-29 09:49:30 +04:00
{ QCommandLine : : Option , ' \0 ' , " debug " , " Prints additional warning and debug message: 'true' or 'false' (default) " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " disk-cache " , " Enables disk cache: 'true' or 'false' (default) " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " ignore-ssl-errors " , " Ignores SSL errors (expired/self-signed certificate errors): 'true' or 'false' (default) " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " load-images " , " Loads all inlined images: 'true' (default) or 'false' " , QCommandLine : : Optional } ,
2012-09-03 14:30:56 +04:00
{ QCommandLine : : Option , ' \0 ' , " local-storage-path " , " Specifies the location for offline local storage " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " local-storage-quota " , " Sets the maximum size of the offline local storage (in KB) " , QCommandLine : : Optional } ,
2013-01-29 09:49:30 +04:00
{ QCommandLine : : Option , ' \0 ' , " local-to-remote-url-access " , " Allows local content to access remote URL: 'true' or 'false' (default) " , QCommandLine : : Optional } ,
2012-09-03 14:30:56 +04:00
{ QCommandLine : : Option , ' \0 ' , " max-disk-cache-size " , " Limits the size of the disk cache (in KB) " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " output-encoding " , " Sets the encoding for the terminal output, default is 'utf8' " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " remote-debugger-port " , " Starts the script in a debug harness and listens on the specified port " , QCommandLine : : Optional } ,
2013-01-29 09:49:30 +04:00
{ QCommandLine : : Option , ' \0 ' , " remote-debugger-autorun " , " Runs the script in the debugger immediately: 'true' or 'false' (default) " , QCommandLine : : Optional } ,
2012-09-03 14:30:56 +04:00
{ QCommandLine : : Option , ' \0 ' , " proxy " , " Sets the proxy server, e.g. '--proxy=http://proxy.company.com:8080' " , QCommandLine : : Optional } ,
{ QCommandLine : : Option , ' \0 ' , " proxy-auth " , " Provides authentication information for the proxy, e.g. ''-proxy-auth=username:password' " , QCommandLine : : Optional } ,
2012-09-04 05:20:02 +04:00
{ QCommandLine : : Option , ' \0 ' , " proxy-type " , " Specifies the proxy type, 'http' (default), 'none' (disable completely), or 'socks5' " , QCommandLine : : Optional } ,
2012-09-03 14:30:56 +04:00
{ QCommandLine : : Option , ' \0 ' , " script-encoding " , " Sets the encoding used for the starting script, default is 'utf8' " , QCommandLine : : Optional } ,
2014-07-30 10:50:04 +04:00
{ QCommandLine : : Option , ' \0 ' , " script-language " , " Sets the script language instead of detecting it: 'javascript' " , QCommandLine : : Optional } ,
2013-01-29 09:49:30 +04:00
{ QCommandLine : : Option , ' \0 ' , " web-security " , " Enables web security, 'true' (default) or 'false' " , QCommandLine : : Optional } ,
2012-10-27 23:10:33 +04:00
{ QCommandLine : : Option , ' \0 ' , " ssl-protocol " , " Sets the SSL protocol (supported protocols: 'SSLv3' (default), 'SSLv2', 'TLSv1', 'any') " , QCommandLine : : Optional } ,
2013-02-28 02:50:51 +04:00
{ QCommandLine : : Option , ' \0 ' , " ssl-certificates-path " , " Sets the location for custom CA certificates (if none set, uses system default) " , QCommandLine : : Optional } ,
2012-11-19 03:58:21 +04:00
{ QCommandLine : : Option , ' \0 ' , " webdriver " , " Starts in 'Remote WebDriver mode' (embedded GhostDriver): '[[<IP>:]<PORT>]' (default '127.0.0.1:8910') " , QCommandLine : : Optional } ,
2013-03-18 05:30:24 +04:00
{ 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 } ,
2012-09-14 19:04:06 +04:00
{ QCommandLine : : Param , ' \0 ' , " script " , " Script " , QCommandLine : : Flags ( QCommandLine : : Optional | QCommandLine : : ParameterFence ) } ,
2012-09-14 14:05:52 +04:00
{ QCommandLine : : Param , ' \0 ' , " argument " , " Script argument " , QCommandLine : : OptionalMultiple } ,
2013-03-18 05:30:24 +04:00
{ QCommandLine : : Switch , ' w ' , " wd " , " Equivalent to '--webdriver' option above " , QCommandLine : : Optional } ,
2012-09-03 14:30:56 +04:00
{ QCommandLine : : Switch , ' h ' , " help " , " Shows this message and quits " , QCommandLine : : Optional } ,
{ QCommandLine : : Switch , ' v ' , " version " , " Prints out PhantomJS version " , QCommandLine : : Optional } ,
QCOMMANDLINE_CONFIG_ENTRY_END
} ;
2011-08-29 18:23:01 +04:00
Config : : Config ( QObject * parent )
: QObject ( parent )
2011-08-26 22:02:35 +04:00
{
2014-07-24 21:25:46 +04:00
m_cmdLine = new QCommandLine ( this ) ;
2012-09-20 00:56:56 +04:00
// We will handle --help and --version ourselves in phantom.cpp
m_cmdLine - > enableHelp ( false ) ;
m_cmdLine - > enableVersion ( false ) ;
2011-08-26 22:02:35 +04:00
resetToDefaults ( ) ;
}
2011-08-28 16:40:00 +04:00
void Config : : init ( const QStringList * const args )
2011-08-26 22:02:35 +04:00
{
resetToDefaults ( ) ;
2011-08-26 22:03:06 +04:00
2013-02-28 02:50:51 +04:00
QByteArray envSslCertDir = qgetenv ( " SSL_CERT_DIR " ) ;
if ( ! envSslCertDir . isEmpty ( ) )
setSslCertificatesPath ( envSslCertDir ) ;
2011-08-28 16:40:00 +04:00
processArgs ( * args ) ;
}
void Config : : processArgs ( const QStringList & args )
{
2012-09-03 14:30:56 +04:00
connect ( m_cmdLine , SIGNAL ( switchFound ( const QString & ) ) , this , SLOT ( handleSwitch ( const QString & ) ) ) ;
connect ( m_cmdLine , SIGNAL ( optionFound ( const QString & , const QVariant & ) ) , this , SLOT ( handleOption ( const QString & , const QVariant & ) ) ) ;
connect ( m_cmdLine , SIGNAL ( paramFound ( const QString & , const QVariant & ) ) , this , SLOT ( handleParam ( const QString & , const QVariant & ) ) ) ;
connect ( m_cmdLine , SIGNAL ( parseError ( const QString & ) ) , this , SLOT ( handleError ( const QString & ) ) ) ;
2011-08-28 16:40:00 +04:00
2012-09-03 14:30:56 +04:00
m_cmdLine - > setArguments ( args ) ;
m_cmdLine - > setConfig ( flags ) ;
m_cmdLine - > parse ( ) ;
2012-11-19 03:58:21 +04:00
// Inject command line parameters to be picked up by GhostDriver
if ( isWebdriverMode ( ) ) {
QStringList argsForGhostDriver ;
2013-03-18 05:30:24 +04:00
m_scriptFile = " main.js " ; //< launch script
argsForGhostDriver < < QString ( " --ip=%1 " ) . arg ( m_webdriverIp ) ; //< "--ip=IP"
argsForGhostDriver < < QString ( " --port=%1 " ) . arg ( m_webdriverPort ) ; //< "--port=PORT"
2012-12-13 03:19:42 +04:00
if ( ! m_webdriverSeleniumGridHub . isEmpty ( ) ) {
2013-03-18 05:30:24 +04:00
argsForGhostDriver < < QString ( " --hub=%1 " ) . arg ( m_webdriverSeleniumGridHub ) ; //< "--hub=SELENIUM_GRID_HUB_URL"
2012-11-19 03:58:21 +04:00
}
2013-03-18 05:30:24 +04:00
if ( ! m_webdriverLogFile . isEmpty ( ) ) {
argsForGhostDriver < < QString ( " --logFile=%1 " ) . arg ( m_webdriverLogFile ) ; //< "--logFile=LOG_FILE"
argsForGhostDriver < < " --logColor=false " ; //< Force no-color-output in Log File
}
argsForGhostDriver < < QString ( " --logLevel=%1 " ) . arg ( m_webdriverLogLevel ) ; //< "--logLevel=LOG_LEVEL"
2012-11-19 03:58:21 +04:00
// Clear current args and override with those
setScriptArgs ( argsForGhostDriver ) ;
}
2011-08-26 22:03:06 +04:00
}
2011-08-29 19:43:41 +04:00
void Config : : loadJsonFile ( const QString & filePath )
{
QString jsonConfig ;
2011-10-16 23:35:21 +04:00
QFile f ( filePath ) ;
// Check file exists and is readable
if ( ! f . exists ( ) | | ! f . open ( QFile : : ReadOnly | QFile : : Text ) ) {
2011-08-29 19:43:41 +04:00
Terminal : : instance ( ) - > cerr ( " Unable to open config: \" " + filePath + " \" " ) ;
return ;
2011-10-16 23:35:21 +04:00
}
// Read content
jsonConfig = QString : : fromUtf8 ( f . readAll ( ) . trimmed ( ) ) ;
f . close ( ) ;
// Check it's a valid JSON format
if ( jsonConfig . isEmpty ( ) | | ! jsonConfig . startsWith ( ' { ' ) | | ! jsonConfig . endsWith ( ' } ' ) ) {
2011-08-29 19:43:41 +04:00
Terminal : : instance ( ) - > cerr ( " Config file MUST be in JSON format! " ) ;
return ;
}
// Load configurator
2011-10-16 23:35:21 +04:00
QString configurator = Utils : : readResourceFileUtf8 ( " :/configurator.js " ) ;
2011-08-29 19:43:41 +04:00
2011-10-16 23:35:21 +04:00
// Use a temporary QWebPage to load the JSON configuration in this Object using the 'configurator' above
2011-08-29 19:43:41 +04:00
QWebPage webPage ;
2011-10-16 23:35:21 +04:00
// Add this object to the global scope
2011-08-29 19:43:41 +04:00
webPage . mainFrame ( ) - > addToJavaScriptWindowObject ( " config " , this ) ;
2011-10-16 23:35:21 +04:00
// Apply the JSON config settings to this very object
2012-03-17 21:27:13 +04:00
webPage . mainFrame ( ) - > evaluateJavaScript ( configurator . arg ( jsonConfig ) , QString ( ) ) ;
2011-08-29 19:43:41 +04:00
}
2012-09-03 14:30:56 +04:00
QString Config : : helpText ( ) const
{
return m_cmdLine - > help ( ) ;
}
2011-08-28 16:05:34 +04:00
bool Config : : autoLoadImages ( ) const
2011-08-26 22:02:59 +04:00
{
return m_autoLoadImages ;
}
2011-08-28 16:05:34 +04:00
void Config : : setAutoLoadImages ( const bool value )
2011-08-26 22:02:59 +04:00
{
m_autoLoadImages = value ;
}
2011-09-15 20:40:22 +04:00
QString Config : : cookiesFile ( ) const
2011-08-26 22:02:51 +04:00
{
2011-09-15 20:40:22 +04:00
return m_cookiesFile ;
2011-08-26 22:02:51 +04:00
}
2011-09-15 20:40:22 +04:00
void Config : : setCookiesFile ( const QString & value )
2011-08-26 22:02:51 +04:00
{
2011-09-15 20:40:22 +04:00
m_cookiesFile = value ;
2011-08-26 22:02:51 +04:00
}
2012-05-19 17:04:01 +04:00
QString Config : : offlineStoragePath ( ) const
{
return m_offlineStoragePath ;
}
void Config : : setOfflineStoragePath ( const QString & value )
{
QDir dir ( value ) ;
m_offlineStoragePath = dir . absolutePath ( ) ;
}
int Config : : offlineStorageDefaultQuota ( ) const
{
return m_offlineStorageDefaultQuota ;
}
void Config : : setOfflineStorageDefaultQuota ( int offlineStorageDefaultQuota )
{
m_offlineStorageDefaultQuota = offlineStorageDefaultQuota * 1024 ;
}
2011-08-28 16:05:34 +04:00
bool Config : : diskCacheEnabled ( ) const
2011-08-26 22:02:51 +04:00
{
return m_diskCacheEnabled ;
}
2011-08-28 16:05:34 +04:00
void Config : : setDiskCacheEnabled ( const bool value )
2011-08-26 22:02:51 +04:00
{
m_diskCacheEnabled = value ;
}
2011-09-07 03:33:11 +04:00
int Config : : maxDiskCacheSize ( ) const
{
return m_maxDiskCacheSize ;
}
void Config : : setMaxDiskCacheSize ( int maxDiskCacheSize )
{
m_maxDiskCacheSize = maxDiskCacheSize ;
}
2011-08-28 16:05:34 +04:00
bool Config : : ignoreSslErrors ( ) const
2011-08-26 22:02:51 +04:00
{
return m_ignoreSslErrors ;
}
2011-08-28 16:05:34 +04:00
void Config : : setIgnoreSslErrors ( const bool value )
2011-08-26 22:02:51 +04:00
{
m_ignoreSslErrors = value ;
}
2011-09-07 20:27:20 +04:00
bool Config : : localToRemoteUrlAccessEnabled ( ) const
2011-08-26 22:02:59 +04:00
{
2011-09-07 20:27:20 +04:00
return m_localToRemoteUrlAccessEnabled ;
2011-08-26 22:02:59 +04:00
}
2011-09-07 20:27:20 +04:00
void Config : : setLocalToRemoteUrlAccessEnabled ( const bool value )
2011-08-26 22:02:59 +04:00
{
2011-09-07 20:27:20 +04:00
m_localToRemoteUrlAccessEnabled = value ;
2011-08-26 22:02:59 +04:00
}
2011-08-28 16:05:34 +04:00
QString Config : : outputEncoding ( ) const
2011-08-26 22:02:35 +04:00
{
return m_outputEncoding ;
}
2011-08-28 16:05:34 +04:00
void Config : : setOutputEncoding ( const QString & value )
2011-08-26 22:02:35 +04:00
{
if ( value . isEmpty ( ) ) {
return ;
}
m_outputEncoding = value ;
}
2011-09-26 08:51:44 +04:00
QString Config : : proxyType ( ) const
{
return m_proxyType ;
}
void Config : : setProxyType ( const QString value )
{
m_proxyType = value ;
}
2011-08-29 18:23:01 +04:00
QString Config : : proxy ( ) const
{
2013-05-27 23:59:42 +04:00
return m_proxyHost + " : " + QString : : number ( m_proxyPort ) ;
2011-08-29 18:23:01 +04:00
}
2011-08-28 16:05:34 +04:00
void Config : : setProxy ( const QString & value )
2011-08-26 22:02:43 +04:00
{
2013-05-27 23:59:42 +04:00
QUrl proxyUrl = QUrl : : fromUserInput ( value ) ;
2011-08-26 22:02:43 +04:00
2013-05-27 23:59:42 +04:00
if ( proxyUrl . isValid ( ) ) {
setProxyHost ( proxyUrl . host ( ) ) ;
setProxyPort ( proxyUrl . port ( 1080 ) ) ;
2011-08-26 22:02:43 +04:00
}
}
2012-03-06 20:01:48 +04:00
void Config : : setProxyAuth ( const QString & value )
{
QString proxyUser = value ;
QString proxyPass = " " ;
if ( proxyUser . lastIndexOf ( ' : ' ) > 0 ) {
proxyPass = proxyUser . mid ( proxyUser . lastIndexOf ( ' : ' ) + 1 ) . trimmed ( ) ;
proxyUser = proxyUser . left ( proxyUser . lastIndexOf ( ' : ' ) ) . trimmed ( ) ;
setProxyAuthUser ( proxyUser ) ;
setProxyAuthPass ( proxyPass ) ;
}
}
QString Config : : proxyAuth ( ) const
{
return proxyAuthUser ( ) + " : " + proxyAuthPass ( ) ;
}
QString Config : : proxyAuthUser ( ) const
{
return m_proxyAuthUser ;
}
QString Config : : proxyAuthPass ( ) const
{
return m_proxyAuthPass ;
}
2011-08-28 16:05:34 +04:00
QString Config : : proxyHost ( ) const
2011-08-26 22:02:43 +04:00
{
return m_proxyHost ;
}
2011-08-28 16:05:34 +04:00
int Config : : proxyPort ( ) const
2011-08-26 22:02:43 +04:00
{
return m_proxyPort ;
}
2011-08-28 16:40:00 +04:00
QStringList Config : : scriptArgs ( ) const
{
return m_scriptArgs ;
}
void Config : : setScriptArgs ( const QStringList & value )
{
m_scriptArgs . clear ( ) ;
QStringListIterator it ( value ) ;
while ( it . hasNext ( ) ) {
m_scriptArgs . append ( it . next ( ) ) ;
}
}
2011-08-28 16:05:34 +04:00
QString Config : : scriptEncoding ( ) const
2011-08-26 22:02:35 +04:00
{
return m_scriptEncoding ;
}
2011-08-28 16:05:34 +04:00
void Config : : setScriptEncoding ( const QString & value )
2011-08-26 22:02:35 +04:00
{
if ( value . isEmpty ( ) ) {
return ;
}
m_scriptEncoding = value ;
}
2013-11-13 18:52:57 +04:00
QString Config : : scriptLanguage ( ) const
{
return m_scriptLanguage ;
}
void Config : : setScriptLanguage ( const QString & value )
{
if ( value . isEmpty ( ) ) {
return ;
}
m_scriptLanguage = value ;
}
2011-08-28 16:40:00 +04:00
QString Config : : scriptFile ( ) const
{
return m_scriptFile ;
}
void Config : : setScriptFile ( const QString & value )
{
m_scriptFile = value ;
}
QString Config : : unknownOption ( ) const
{
return m_unknownOption ;
}
void Config : : setUnknownOption ( const QString & value )
{
m_unknownOption = value ;
}
bool Config : : versionFlag ( ) const
{
return m_versionFlag ;
}
void Config : : setVersionFlag ( const bool value )
{
m_versionFlag = value ;
}
2011-10-25 23:16:25 +04:00
bool Config : : debug ( ) const
{
return m_debug ;
}
void Config : : setDebug ( const bool value )
{
m_debug = value ;
}
int Config : : remoteDebugPort ( ) const
{
return m_remoteDebugPort ;
}
void Config : : setRemoteDebugPort ( const int port )
{
m_remoteDebugPort = port ;
}
2012-02-27 01:15:44 +04:00
bool Config : : remoteDebugAutorun ( ) const
{
return m_remoteDebugAutorun ;
}
void Config : : setRemoteDebugAutorun ( const bool value )
{
m_remoteDebugAutorun = value ;
}
2012-03-13 09:47:58 +04:00
bool Config : : webSecurityEnabled ( ) const
{
return m_webSecurityEnabled ;
}
void Config : : setWebSecurityEnabled ( const bool value )
{
m_webSecurityEnabled = value ;
}
2012-07-05 04:21:15 +04:00
void Config : : setJavascriptCanOpenWindows ( const bool value )
{
m_javascriptCanOpenWindows = value ;
}
bool Config : : javascriptCanOpenWindows ( ) const
{
return m_javascriptCanOpenWindows ;
}
void Config : : setJavascriptCanCloseWindows ( const bool value )
{
m_javascriptCanCloseWindows = value ;
}
bool Config : : javascriptCanCloseWindows ( ) const
{
return m_javascriptCanCloseWindows ;
}
2012-11-19 03:58:21 +04:00
void Config : : setWebdriver ( const QString & webdriverConfig )
{
// Parse and validate the configuration
bool isValidPort ;
QStringList wdCfg = webdriverConfig . split ( ' : ' ) ;
if ( wdCfg . length ( ) = = 1 & & wdCfg [ 0 ] . toInt ( & isValidPort ) & & isValidPort ) {
// Only a PORT was provided
2013-03-18 05:30:24 +04:00
m_webdriverPort = wdCfg [ 0 ] ;
2012-11-19 03:58:21 +04:00
} else if ( wdCfg . length ( ) = = 2 & & ! wdCfg [ 0 ] . isEmpty ( ) & & wdCfg [ 1 ] . toInt ( & isValidPort ) & & isValidPort ) {
// Both IP and PORT provided
2013-03-18 05:30:24 +04:00
m_webdriverIp = wdCfg [ 0 ] ;
m_webdriverPort = wdCfg [ 1 ] ;
2012-11-19 03:58:21 +04:00
}
}
QString Config : : webdriver ( ) const
{
2013-03-18 05:30:24 +04:00
return QString ( " %1:%2 " ) . arg ( m_webdriverIp ) . arg ( m_webdriverPort ) ;
2012-11-19 03:58:21 +04:00
}
bool Config : : isWebdriverMode ( ) const
{
2013-03-18 05:30:24 +04:00
return ! m_webdriverPort . isEmpty ( ) ;
}
void Config : : setWebdriverLogFile ( const QString & webdriverLogFile )
{
m_webdriverLogFile = webdriverLogFile ;
}
QString Config : : webdriverLogFile ( ) const
{
return m_webdriverLogFile ;
}
void Config : : setWebdriverLogLevel ( const QString & webdriverLogLevel )
{
m_webdriverLogLevel = webdriverLogLevel ;
}
QString Config : : webdriverLogLevel ( ) const
{
return m_webdriverLogLevel ;
2012-11-19 03:58:21 +04:00
}
2012-12-13 03:19:42 +04:00
void Config : : setWebdriverSeleniumGridHub ( const QString & hubUrl )
2012-11-19 03:58:21 +04:00
{
2012-12-13 03:19:42 +04:00
m_webdriverSeleniumGridHub = hubUrl ;
2012-11-19 03:58:21 +04:00
}
2012-12-13 03:19:42 +04:00
QString Config : : webdriverSeleniumGridHub ( ) const
2012-11-19 03:58:21 +04:00
{
2012-12-13 03:19:42 +04:00
return m_webdriverSeleniumGridHub ;
2012-11-19 03:58:21 +04:00
}
2011-08-26 22:02:35 +04:00
// private:
2011-08-28 16:05:34 +04:00
void Config : : resetToDefaults ( )
2011-08-26 22:02:35 +04:00
{
2011-08-26 22:02:59 +04:00
m_autoLoadImages = true ;
2011-09-15 20:40:22 +04:00
m_cookiesFile = QString ( ) ;
2012-05-19 17:04:01 +04:00
m_offlineStoragePath = QString ( ) ;
m_offlineStorageDefaultQuota = - 1 ;
2011-08-26 22:02:51 +04:00
m_diskCacheEnabled = false ;
2011-09-07 03:33:11 +04:00
m_maxDiskCacheSize = - 1 ;
2011-08-26 22:02:51 +04:00
m_ignoreSslErrors = false ;
2011-09-07 20:27:20 +04:00
m_localToRemoteUrlAccessEnabled = false ;
2011-08-26 22:02:35 +04:00
m_outputEncoding = " UTF-8 " ;
2011-11-09 09:22:30 +04:00
m_proxyType = " http " ;
2011-08-28 16:40:00 +04:00
m_proxyHost . clear ( ) ;
2011-08-26 22:02:43 +04:00
m_proxyPort = 1080 ;
2012-03-06 20:01:48 +04:00
m_proxyAuthUser . clear ( ) ;
m_proxyAuthPass . clear ( ) ;
2011-08-28 16:40:00 +04:00
m_scriptArgs . clear ( ) ;
2011-08-26 22:02:35 +04:00
m_scriptEncoding = " UTF-8 " ;
2013-11-13 18:52:57 +04:00
m_scriptLanguage . clear ( ) ;
2011-08-28 16:40:00 +04:00
m_scriptFile . clear ( ) ;
m_unknownOption . clear ( ) ;
m_versionFlag = false ;
2011-10-25 23:16:25 +04:00
m_debug = false ;
m_remoteDebugPort = - 1 ;
2012-02-27 01:15:44 +04:00
m_remoteDebugAutorun = false ;
2012-03-13 09:47:58 +04:00
m_webSecurityEnabled = true ;
2012-07-05 04:21:15 +04:00
m_javascriptCanOpenWindows = true ;
m_javascriptCanCloseWindows = true ;
2012-01-09 20:31:49 +04:00
m_helpFlag = false ;
2012-06-05 15:43:23 +04:00
m_printDebugMessages = false ;
2012-10-27 23:10:33 +04:00
m_sslProtocol = " sslv3 " ;
2013-02-28 02:50:51 +04:00
m_sslCertificatesPath . clear ( ) ;
2013-03-18 05:30:24 +04:00
m_webdriverIp = QString ( ) ;
m_webdriverPort = QString ( ) ;
m_webdriverLogFile = QString ( ) ;
m_webdriverLogLevel = " INFO " ;
2012-12-13 03:19:42 +04:00
m_webdriverSeleniumGridHub = QString ( ) ;
2011-08-26 22:02:35 +04:00
}
2011-08-26 22:02:43 +04:00
2012-03-06 20:01:48 +04:00
void Config : : setProxyAuthPass ( const QString & value )
{
m_proxyAuthPass = value ;
}
void Config : : setProxyAuthUser ( const QString & value )
{
m_proxyAuthUser = value ;
}
2011-08-28 16:05:34 +04:00
void Config : : setProxyHost ( const QString & value )
2011-08-26 22:02:43 +04:00
{
m_proxyHost = value ;
}
2011-08-28 16:05:34 +04:00
void Config : : setProxyPort ( const int value )
2011-08-26 22:02:43 +04:00
{
m_proxyPort = value ;
}
2012-01-09 20:31:49 +04:00
bool Config : : helpFlag ( ) const
{
return m_helpFlag ;
}
void Config : : setHelpFlag ( const bool value )
{
m_helpFlag = value ;
}
2012-06-05 15:43:23 +04:00
bool Config : : printDebugMessages ( ) const
{
return m_printDebugMessages ;
}
void Config : : setPrintDebugMessages ( const bool value )
{
m_printDebugMessages = value ;
}
2012-09-03 14:30:56 +04:00
void Config : : handleSwitch ( const QString & sw )
{
setHelpFlag ( sw = = " help " ) ;
setVersionFlag ( sw = = " version " ) ;
2013-03-18 05:30:24 +04:00
if ( sw = = " wd " ) {
setWebdriver ( DEFAULT_WEBDRIVER_CONFIG ) ;
}
2012-09-03 14:30:56 +04:00
}
void Config : : handleOption ( const QString & option , const QVariant & value )
{
bool boolValue = false ;
QStringList booleanFlags ;
booleanFlags < < " debug " ;
booleanFlags < < " disk-cache " ;
booleanFlags < < " ignore-ssl-errors " ;
booleanFlags < < " load-images " ;
booleanFlags < < " local-to-remote-url-access " ;
booleanFlags < < " remote-debugger-autorun " ;
booleanFlags < < " web-security " ;
2012-09-04 00:30:38 +04:00
if ( booleanFlags . contains ( option ) ) {
2012-09-03 14:30:56 +04:00
if ( ( value ! = " true " ) & & ( value ! = " yes " ) & & ( value ! = " false " ) & & ( value ! = " no " ) ) {
setUnknownOption ( QString ( " Invalid values for '%1' option. " ) . arg ( option ) ) ;
return ;
}
boolValue = ( value = = " true " ) | | ( value = = " yes " ) ;
}
if ( option = = " cookies-file " ) {
setCookiesFile ( value . toString ( ) ) ;
}
if ( option = = " config " ) {
loadJsonFile ( value . toString ( ) ) ;
}
if ( option = = " debug " ) {
setPrintDebugMessages ( boolValue ) ;
}
if ( option = = " disk-cache " ) {
setDiskCacheEnabled ( boolValue ) ;
}
if ( option = = " ignore-ssl-errors " ) {
setIgnoreSslErrors ( boolValue ) ;
}
if ( option = = " load-images " ) {
setAutoLoadImages ( boolValue ) ;
}
if ( option = = " local-storage-path " ) {
setOfflineStoragePath ( value . toString ( ) ) ;
}
if ( option = = " local-storage-quota " ) {
setOfflineStorageDefaultQuota ( value . toInt ( ) ) ;
}
if ( option = = " local-to-remote-url-access " ) {
setLocalToRemoteUrlAccessEnabled ( boolValue ) ;
}
2013-04-10 02:52:45 +04:00
if ( option = = " max-disk-cache-size " ) {
2012-09-03 14:30:56 +04:00
setMaxDiskCacheSize ( value . toInt ( ) ) ;
}
if ( option = = " output-encoding " ) {
setOutputEncoding ( value . toString ( ) ) ;
}
if ( option = = " remote-debugger-autorun " ) {
setRemoteDebugAutorun ( boolValue ) ;
}
if ( option = = " remote-debugger-port " ) {
setDebug ( true ) ;
setRemoteDebugPort ( value . toInt ( ) ) ;
}
if ( option = = " proxy " ) {
setProxy ( value . toString ( ) ) ;
}
if ( option = = " proxy-type " ) {
setProxyType ( value . toString ( ) ) ;
}
if ( option = = " proxy-auth " ) {
setProxyAuth ( value . toString ( ) ) ;
}
if ( option = = " script-encoding " ) {
setScriptEncoding ( value . toString ( ) ) ;
}
2013-11-13 18:52:57 +04:00
if ( option = = " script-language " ) {
setScriptLanguage ( value . toString ( ) ) ;
}
2012-09-03 14:30:56 +04:00
if ( option = = " web-security " ) {
setWebSecurityEnabled ( boolValue ) ;
}
2012-10-17 19:03:22 +04:00
if ( option = = " ssl-protocol " ) {
2012-10-27 23:10:33 +04:00
setSslProtocol ( value . toString ( ) ) ;
2012-10-17 19:03:22 +04:00
}
2013-02-28 02:50:51 +04:00
if ( option = = " ssl-certificates-path " ) {
setSslCertificatesPath ( value . toString ( ) ) ;
}
2012-11-19 03:58:21 +04:00
if ( option = = " webdriver " ) {
2013-03-18 05:30:24 +04:00
setWebdriver ( value . toString ( ) . length ( ) > 0 ? value . toString ( ) : DEFAULT_WEBDRIVER_CONFIG ) ;
}
if ( option = = " webdriver-logfile " ) {
setWebdriverLogFile ( value . toString ( ) ) ;
}
if ( option = = " webdriver-loglevel " ) {
setWebdriverLogLevel ( value . toString ( ) ) ;
2012-11-19 03:58:21 +04:00
}
2012-12-13 03:19:42 +04:00
if ( option = = " webdriver-selenium-grid-hub " ) {
setWebdriverSeleniumGridHub ( value . toString ( ) ) ;
2012-11-19 03:58:21 +04:00
}
2012-09-03 14:30:56 +04:00
}
void Config : : handleParam ( const QString & param , const QVariant & value )
{
2012-09-04 00:30:38 +04:00
Q_UNUSED ( param ) ;
2012-09-03 14:30:56 +04:00
if ( m_scriptFile . isEmpty ( ) )
m_scriptFile = value . toString ( ) ;
else
m_scriptArgs + = value . toString ( ) ;
}
void Config : : handleError ( const QString & error )
{
setUnknownOption ( QString ( " Error: %1 " ) . arg ( error ) ) ;
}
2012-10-17 19:03:22 +04:00
QString Config : : sslProtocol ( ) const
{
return m_sslProtocol ;
}
void Config : : setSslProtocol ( const QString & sslProtocolName )
{
2012-10-27 23:10:33 +04:00
m_sslProtocol = sslProtocolName . toLower ( ) ;
2012-11-19 03:58:21 +04:00
}
2013-02-28 02:50:51 +04:00
QString Config : : sslCertificatesPath ( ) const
{
return m_sslCertificatesPath ;
}
void Config : : setSslCertificatesPath ( const QString & sslCertificatesPath )
{
QFileInfo sslPathInfo = QFileInfo ( sslCertificatesPath ) ;
if ( sslPathInfo . isDir ( ) ) {
if ( sslCertificatesPath . endsWith ( ' / ' ) )
m_sslCertificatesPath = sslCertificatesPath + " * " ;
else
m_sslCertificatesPath = sslCertificatesPath + " /* " ;
} else {
m_sslCertificatesPath = sslCertificatesPath ;
}
}