Skip to content

Commit

Permalink
Option to create a unique desktop name added.
Browse files Browse the repository at this point in the history
  • Loading branch information
kybu committed Sep 6, 2015
1 parent c665aba commit 59d47f1
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 25 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Any command line parameters are passed on to the IE driver.

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

New virtual desktop name is `HeadlessDesktop`. If you want to create a unique desktop name each time
the `headless_ie_selenium.exe` executable is run, variable `HEADLESS_UNIQUE` has to exist in the environment.
Its value does not matter, unique desktop name will be generated as long as this variable is present.

## Basic Ruby example

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

Dir.chdir(File.dirname $0)

# Generate unique desktop name each run
ENV['HEADLESS_UNIQUE'] = 1

# Find the Headless IE binary.
headlessBinary =
if File.exists?('../Debug/headless_ie_selenium.exe')
Expand Down Expand Up @@ -146,9 +153,9 @@ Tested on Win 7.

Supported browsers at the moment: IE.

Developed using VS 2013 Express, Boost 1.56, Mercurial with the GIT bridge.
Developed using VS 2013 Express, Boost 1.59, Mercurial with the GIT bridge.

# License

GPLv3, Copyright 2014 Peter Vrabel
GPLv3, Copyright 2014,2015 Peter Vrabel

7 changes: 2 additions & 5 deletions common.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(BOOST_ROOT)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(BOOST_ROOT)\include\boost-1_59</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<Optimization>Disabled</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<StringPooling>true</StringPooling>
<MinimalRebuild>false</MinimalRebuild>
<AdditionalOptions>/Zm300 %(AdditionalOptions)</AdditionalOptions>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<CompileAsManaged>false</CompileAsManaged>
<WholeProgramOptimization>true</WholeProgramOptimization>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(BOOST_ROOT)/stage/lib</AdditionalLibraryDirectories>
<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>
<AdditionalLibraryDirectories>$(BOOST_ROOT)\lib</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
Expand Down
6 changes: 1 addition & 5 deletions desktop_utils/desktop_utils.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
Expand All @@ -68,23 +67,20 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>e:\_projects\_3rd_party\boost_1_56_0</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>e:\_projects\_3rd_party\boost_1_56_0\stage\lib</AdditionalLibraryDirectories>
<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>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="desktop.h" />
<ClInclude Include="environment.h" />
<ClInclude Include="process.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
Expand Down
41 changes: 41 additions & 0 deletions desktop_utils/environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

/* # Copyright 2014 Peter Vrabel([email protected])
#
# This file is part of 'Headless Selenium for Win'.
#
# 'Headless Selenium for Win' is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 'Headless Selenium for Win' is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 'Headless Selenium for Win'.If not, see <http://www.gnu.org/licenses/>. */

#include "stdafx.h"

class Environment {
public:
static bool variableExists(const std::string &);
};

// inline

inline
bool Environment::variableExists(const std::string &var) {
wchar_t buf[104];

auto ret = GetEnvironmentVariable(
boost::from_utf8(var).c_str(),
buf, 104);

if (!ret && GetLastError()==ERROR_ENVVAR_NOT_FOUND)
return false;

return true;
}
1 change: 1 addition & 0 deletions desktop_utils/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/log/utility/setup/console.hpp>

#include "desktop.h"
#include "environment.h"
#include "process.h"


Expand Down
23 changes: 20 additions & 3 deletions headless_ie_selenium/headless_ie_selenium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using namespace std;
namespace bo = boost;

string desktopName = "HeadlessDesktop";
string desktopName;

template <class SepT, class IterT>
SepT join(IterT begin, const IterT end, const SepT &sep)
Expand Down Expand Up @@ -58,9 +58,24 @@ string getAppCmdArgs() {
return cmdLine;
}

void prepareDesktopName() {
if (Environment::variableExists("HEADLESS_UNIQUE")) {
bo::random_device random;

string name;
do {
name = (bo::format("HeadlessDesktop_%1%") % random()).str();
} while (Desktop::exists(name));

desktopName = name;
}
else
desktopName = "HeadlessDesktop";
}

// TODO: Implement safer SearchPath.
string findIEDriver() {
bo::shared_ptr<wchar_t[]> buffer(new wchar_t[32*1024]);
bo::movelib::unique_ptr<wchar_t[]> buffer(new wchar_t[32*1024]);
wchar_t *bufferEnd = 0;

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

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

PostMessage(w, WM_ENDSESSION, NULL, ENDSESSION_CLOSEAPP);
PostMessage(w, WM_QUIT, 0, 0);
Expand Down Expand Up @@ -138,6 +153,8 @@ int _tmain(int argc, _TCHAR* argv[])
try {
setupLogger();

prepareDesktopName();

string cmdLine = getAppCmdArgs();
string ieDriverPath = findIEDriver();

Expand Down
9 changes: 3 additions & 6 deletions headless_ie_selenium/headless_ie_selenium.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,29 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<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>
<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>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<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>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\desktop_utils\desktop.h" />
<ClInclude Include="..\desktop_utils\environment.h" />
<ClInclude Include="..\desktop_utils\process.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
Expand Down
4 changes: 4 additions & 0 deletions headless_ie_selenium/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
#include <RestartManager.h>

#include <boost/smart_ptr.hpp>
#include <boost/move/unique_ptr.hpp>
#include <boost/thread.hpp>

#include <boost/random/random_device.hpp>

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/console.hpp>


#include "../desktop_utils/desktop.h"
#include "../desktop_utils/environment.h"

#define LOGT BOOST_LOG_TRIVIAL(trace)
#define LOGD BOOST_LOG_TRIVIAL(debug)
Expand Down
8 changes: 4 additions & 4 deletions version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define FILEVER 1,1,0,0
#define PRODUCTVER 1,1,0,0
#define STRFILEVER "1, 1\0"
#define STRPRODUCTVER "1, 1\0"
#define FILEVER 1,2,0,0
#define PRODUCTVER 1,2,0,0
#define STRFILEVER "1, 2\0"
#define STRPRODUCTVER "1, 2\0"

0 comments on commit 59d47f1

Please sign in to comment.