Skip to content

Commit 59d47f1

Browse files
committed
Option to create a unique desktop name added.
1 parent c665aba commit 59d47f1

9 files changed

+85
-25
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Any command line parameters are passed on to the IE driver.
3838

3939
`IEDriverServer.exe` has to be stored in the searchable path.
4040

41+
New virtual desktop name is `HeadlessDesktop`. If you want to create a unique desktop name each time
42+
the `headless_ie_selenium.exe` executable is run, variable `HEADLESS_UNIQUE` has to exist in the environment.
43+
Its value does not matter, unique desktop name will be generated as long as this variable is present.
44+
4145
## Basic Ruby example
4246

4347
Following example uses www.google.com search to retrieve weather in London.
@@ -47,6 +51,9 @@ require 'selenium-webdriver'
4751

4852
Dir.chdir(File.dirname $0)
4953

54+
# Generate unique desktop name each run
55+
ENV['HEADLESS_UNIQUE'] = 1
56+
5057
# Find the Headless IE binary.
5158
headlessBinary =
5259
if File.exists?('../Debug/headless_ie_selenium.exe')
@@ -146,9 +153,9 @@ Tested on Win 7.
146153

147154
Supported browsers at the moment: IE.
148155

149-
Developed using VS 2013 Express, Boost 1.56, Mercurial with the GIT bridge.
156+
Developed using VS 2013 Express, Boost 1.59, Mercurial with the GIT bridge.
150157

151158
# License
152159

153-
GPLv3, Copyright 2014 Peter Vrabel
160+
GPLv3, Copyright 2014,2015 Peter Vrabel
154161

common.props

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55
<PropertyGroup />
66
<ItemDefinitionGroup>
77
<ClCompile>
8-
<AdditionalIncludeDirectories>$(BOOST_ROOT)</AdditionalIncludeDirectories>
8+
<AdditionalIncludeDirectories>$(BOOST_ROOT)\include\boost-1_59</AdditionalIncludeDirectories>
99
<MultiProcessorCompilation>true</MultiProcessorCompilation>
10-
<Optimization>Disabled</Optimization>
1110
<IntrinsicFunctions>true</IntrinsicFunctions>
1211
<StringPooling>true</StringPooling>
13-
<MinimalRebuild>false</MinimalRebuild>
1412
<AdditionalOptions>/Zm300 %(AdditionalOptions)</AdditionalOptions>
1513
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
1614
<CompileAsManaged>false</CompileAsManaged>
1715
<WholeProgramOptimization>true</WholeProgramOptimization>
1816
</ClCompile>
1917
<Link>
20-
<AdditionalLibraryDirectories>$(BOOST_ROOT)/stage/lib</AdditionalLibraryDirectories>
21-
<AdditionalDependencies>libboost_program_options-vc120-mt-gd-1_56.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
18+
<AdditionalLibraryDirectories>$(BOOST_ROOT)\lib</AdditionalLibraryDirectories>
2219
</Link>
2320
</ItemDefinitionGroup>
2421
<ItemGroup />

desktop_utils/desktop_utils.vcxproj

+1-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<ClCompile>
5353
<PrecompiledHeader>Use</PrecompiledHeader>
5454
<WarningLevel>Level3</WarningLevel>
55-
<Optimization>Disabled</Optimization>
5655
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5756
</ClCompile>
5857
<Link>
@@ -68,23 +67,20 @@
6867
<FunctionLevelLinking>true</FunctionLevelLinking>
6968
<IntrinsicFunctions>true</IntrinsicFunctions>
7069
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
71-
<AdditionalIncludeDirectories>e:\_projects\_3rd_party\boost_1_56_0</AdditionalIncludeDirectories>
7270
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
7371
</ClCompile>
7472
<Link>
7573
<SubSystem>Console</SubSystem>
76-
<GenerateDebugInformation>true</GenerateDebugInformation>
7774
<EnableCOMDATFolding>true</EnableCOMDATFolding>
7875
<OptimizeReferences>true</OptimizeReferences>
79-
<AdditionalLibraryDirectories>e:\_projects\_3rd_party\boost_1_56_0\stage\lib</AdditionalLibraryDirectories>
80-
<AdditionalDependencies>libboost_program_options-vc120-mt-s-1_56.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
8176
</Link>
8277
</ItemDefinitionGroup>
8378
<ItemGroup>
8479
<Text Include="ReadMe.txt" />
8580
</ItemGroup>
8681
<ItemGroup>
8782
<ClInclude Include="desktop.h" />
83+
<ClInclude Include="environment.h" />
8884
<ClInclude Include="process.h" />
8985
<ClInclude Include="resource.h" />
9086
<ClInclude Include="stdafx.h" />

desktop_utils/environment.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
/* # Copyright 2014 Peter Vrabel([email protected])
4+
#
5+
# This file is part of 'Headless Selenium for Win'.
6+
#
7+
# 'Headless Selenium for Win' is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# 'Headless Selenium for Win' is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with 'Headless Selenium for Win'.If not, see <http://www.gnu.org/licenses/>. */
19+
20+
#include "stdafx.h"
21+
22+
class Environment {
23+
public:
24+
static bool variableExists(const std::string &);
25+
};
26+
27+
// inline
28+
29+
inline
30+
bool Environment::variableExists(const std::string &var) {
31+
wchar_t buf[104];
32+
33+
auto ret = GetEnvironmentVariable(
34+
boost::from_utf8(var).c_str(),
35+
buf, 104);
36+
37+
if (!ret && GetLastError()==ERROR_ENVVAR_NOT_FOUND)
38+
return false;
39+
40+
return true;
41+
}

desktop_utils/stdafx.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <boost/log/utility/setup/console.hpp>
2424

2525
#include "desktop.h"
26+
#include "environment.h"
2627
#include "process.h"
2728

2829

headless_ie_selenium/headless_ie_selenium.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
using namespace std;
2222
namespace bo = boost;
2323

24-
string desktopName = "HeadlessDesktop";
24+
string desktopName;
2525

2626
template <class SepT, class IterT>
2727
SepT join(IterT begin, const IterT end, const SepT &sep)
@@ -58,9 +58,24 @@ string getAppCmdArgs() {
5858
return cmdLine;
5959
}
6060

61+
void prepareDesktopName() {
62+
if (Environment::variableExists("HEADLESS_UNIQUE")) {
63+
bo::random_device random;
64+
65+
string name;
66+
do {
67+
name = (bo::format("HeadlessDesktop_%1%") % random()).str();
68+
} while (Desktop::exists(name));
69+
70+
desktopName = name;
71+
}
72+
else
73+
desktopName = "HeadlessDesktop";
74+
}
75+
6176
// TODO: Implement safer SearchPath.
6277
string findIEDriver() {
63-
bo::shared_ptr<wchar_t[]> buffer(new wchar_t[32*1024]);
78+
bo::movelib::unique_ptr<wchar_t[]> buffer(new wchar_t[32*1024]);
6479
wchar_t *bufferEnd = 0;
6580

6681
DWORD length = SearchPath(
@@ -90,7 +105,7 @@ void cleanUp() {
90105
LOGD << "Top level windows in the headless desktop: " << topWindows.size();
91106

92107
for (HWND w : topWindows) {
93-
LOGT << bo::format("Quitting the '%1%' top level window.") % w;
108+
LOGT << (bo::format("Quitting the '%1%' top level window.") % w).str();
94109

95110
PostMessage(w, WM_ENDSESSION, NULL, ENDSESSION_CLOSEAPP);
96111
PostMessage(w, WM_QUIT, 0, 0);
@@ -138,6 +153,8 @@ int _tmain(int argc, _TCHAR* argv[])
138153
try {
139154
setupLogger();
140155

156+
prepareDesktopName();
157+
141158
string cmdLine = getAppCmdArgs();
142159
string ieDriverPath = findIEDriver();
143160

headless_ie_selenium/headless_ie_selenium.vcxproj

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,29 @@
5757
<Link>
5858
<SubSystem>Console</SubSystem>
5959
<GenerateDebugInformation>true</GenerateDebugInformation>
60-
<AdditionalDependencies>libboost_program_options-vc120-mt-gd-1_56.lib;Rstrtmgr.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
60+
<AdditionalDependencies>Rstrtmgr.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
6161
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
6262
</Link>
6363
</ItemDefinitionGroup>
6464
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
6565
<ClCompile>
6666
<WarningLevel>Level3</WarningLevel>
67-
<PrecompiledHeader>Use</PrecompiledHeader>
68-
<FunctionLevelLinking>true</FunctionLevelLinking>
69-
<IntrinsicFunctions>true</IntrinsicFunctions>
7067
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
7168
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
69+
<PrecompiledHeader>Use</PrecompiledHeader>
7270
</ClCompile>
7371
<Link>
7472
<SubSystem>Console</SubSystem>
75-
<GenerateDebugInformation>true</GenerateDebugInformation>
7673
<EnableCOMDATFolding>true</EnableCOMDATFolding>
7774
<OptimizeReferences>true</OptimizeReferences>
78-
<AdditionalDependencies>libboost_program_options-vc120-mt-s-1_56.lib;Rstrtmgr.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
7975
</Link>
8076
</ItemDefinitionGroup>
8177
<ItemGroup>
8278
<Text Include="ReadMe.txt" />
8379
</ItemGroup>
8480
<ItemGroup>
8581
<ClInclude Include="..\desktop_utils\desktop.h" />
82+
<ClInclude Include="..\desktop_utils\environment.h" />
8683
<ClInclude Include="..\desktop_utils\process.h" />
8784
<ClInclude Include="resource.h" />
8885
<ClInclude Include="stdafx.h" />

headless_ie_selenium/stdafx.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
#include <RestartManager.h>
1515

1616
#include <boost/smart_ptr.hpp>
17+
#include <boost/move/unique_ptr.hpp>
1718
#include <boost/thread.hpp>
1819

20+
#include <boost/random/random_device.hpp>
21+
1922
#include <boost/log/trivial.hpp>
2023
#include <boost/log/utility/setup/console.hpp>
2124

2225

2326
#include "../desktop_utils/desktop.h"
27+
#include "../desktop_utils/environment.h"
2428

2529
#define LOGT BOOST_LOG_TRIVIAL(trace)
2630
#define LOGD BOOST_LOG_TRIVIAL(debug)

version.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define FILEVER 1,1,0,0
2-
#define PRODUCTVER 1,1,0,0
3-
#define STRFILEVER "1, 1\0"
4-
#define STRPRODUCTVER "1, 1\0"
1+
#define FILEVER 1,2,0,0
2+
#define PRODUCTVER 1,2,0,0
3+
#define STRFILEVER "1, 2\0"
4+
#define STRPRODUCTVER "1, 2\0"

0 commit comments

Comments
 (0)