Skip to content

Instantly share code, notes, and snippets.

@ammarnajjar
Created April 9, 2017 13:04
Show Gist options
  • Save ammarnajjar/9c1ebcb212d287a798a004f054cba0e3 to your computer and use it in GitHub Desktop.
Save ammarnajjar/9c1ebcb212d287a798a004f054cba0e3 to your computer and use it in GitHub Desktop.
Custom selenium webdrivers using decorators (ipython notebook)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## License"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Copyright (c) 2017 Ammar Najjar <[email protected]>\n",
"# Permission is hereby granted, free of charge, to any person obtaining a copy\n",
"# of this software and associated documentation files (the \"Software\"), to deal\n",
"# in the Software without restriction, including without limitation the rights\n",
"# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
"# copies of the Software, and to permit persons to whom the Software is\n",
"# furnished to do so, subject to the following conditions:\n",
"# The above copyright notice and this permission notice shall be included in all\n",
"# copies or substantial portions of the Software.\n",
"# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
"# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
"# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n",
"# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
"# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
"# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n",
"# SOFTWARE.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from selenium import webdriver\n",
"from selenium.webdriver.chrome.options import Options\n",
"from selenium.webdriver.firefox.firefox_binary import FirefoxBinary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Firefox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class CustomFirefox:\n",
" '''\n",
" Decorator to generate Firefox webdriver\n",
" the parameter passed to this decorator is the path to\n",
" the executable firefox browser\n",
" '''\n",
" \n",
" def __init__(self, bin_path):\n",
" self.bin_path = bin_path\n",
" \n",
" def __call__(self, cls):\n",
" if self.bin_path:\n",
" binary = FirefoxBinary(self.bin_path)\n",
" \n",
" def __init__(self, firefox_binary=binary):\n",
" webdriver.Firefox.__init__(self, firefox_binary=firefox_binary)\n",
" \n",
" meta_data = dict(webdriver.Firefox.__dict__)\n",
" meta_data['__init__'] = __init__\n",
" Firefox = type('Firefox', (webdriver.Firefox, ), meta_data)\n",
" return Firefox\n",
" else:\n",
"return webdriver.Firefox"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chrome"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class CustomChrome:\n",
" '''\n",
" Decorator to generate Firefox webdriver\n",
" the parameter passed to this decorator is the path to\n",
" the executable chrome browser\n",
" '''\n",
" \n",
" def __init__(self, bin_path):\n",
" self.bin_path = bin_path\n",
" \n",
" def __call__(self, cls):\n",
" if self.bin_path:\n",
" chrome_options = Options()\n",
" chrome_options.binary_location = self.bin_path\n",
"\n",
" def __init__(self, chrome_options=chrome_options):\n",
" webdriver.Chrome.__init__(self, chrome_options=chrome_options)\n",
"\n",
" meta_data = dict(webdriver.Chrome.__dict__)\n",
" meta_data['__init__'] = __init__\n",
" Chrome = type('Chrome', (webdriver.Chrome, ), meta_data)\n",
" return Chrome\n",
" else:\n",
"return webdriver.Chrome"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Factory Example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def driver_factory(decorator, bin_paths):\n",
" drivers = []\n",
" for bin_path in bin_paths:\n",
" @decorator(bin_path)\n",
" class CustomDriver: pass \n",
" drivers.append(CustomDriver)\n",
"return drivers"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment