Welcome to python_selenium’s documentation!¶
Notes and examples about Python and Selenium.
code¶
python_selenium code documentation.
makedoc.py¶
This script builds documentation for the project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | """
makedoc.py
++++++++++
Generates the documentation of this project
Usage:
.. code-block:: bash
python makedoc.py
| Author: shmakovpn <shmakovpn@yandex.ru>
| Date: 2020-01-13
"""
import os
# The path to the folder of this script
SCRIPT_DIR: str = os.path.dirname(os.path.abspath(__file__))
def run_sphinx() -> None:
"""Executes *sphinx-build* command"""
docs_dir: str = os.path.join(SCRIPT_DIR, 'docs')
docs_source_dir: str = os.path.join(docs_dir, 'source')
build_dir: str = os.path.join(docs_dir, 'build')
html_dir: str = os.path.join(build_dir, 'html')
cmd: str = f'sphinx-build -b html "{docs_source_dir}" "{html_dir}"'
os.system(cmd)
print('__END__')
if __name__ == '__main__':
run_sphinx()
|
Autodoc documentation¶
makedoc.py¶
Generates the documentation of this project
Usage:
python makedoc.pyAuthor: shmakovpn <shmakovpn@yandex.ru>Date: 2020-01-13
auto_makedoc.sh¶
This script automatically executes makedoc.py on changes in the documentation source folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #!/bin/bash
# Runs `sphinx build` when files in ./doc/source was changed,
# uses inotifywait for watching files changes
# Author: shmakovpn <shmakovpn@yandex.ru>
# Date: 2020-01-07
# Requirements:
# Ubuntu: inotify-tools
# Centos 8: inotify-tools (from epel repository)
SCRIPT_DIR="$(dirname $0)"
DOCS_SOURCE_DIR="${SCRIPT_DIR}/docs/source"
# Checking that a VIRTUALENV is activated, exit otherwise
if ! test "${VIRTUAL_ENV}" ; then
echo "A virtualenv is not activated. \$VIRTUAL_ENV is null"
exit 1
fi
# Checking that *inotifywait* is installed
if ! which inotifywait > /dev/null 2>&1 ; then
echo "*inotifywait* is not installed. Install package *inotify-tools*."
exit 1
fi
# `inotifywait -r -m -e modify -e move -e crate -e delete watching_dir` generates multiple events
# when a file was saved used vim or something else
# but we want to run `sphinx build` only once when a file was changed.
# Thus `while true` is used.
# inotifywait (without *-m* key) generates one event then stops,
# then makedoc.py runs `shpihx build`
# then next iteration of infinitive cicle `while true` starts `inotifywait -m` once again
while true; do
inotifywait -r -e modify -e move -e create -e delete "${DOCS_SOURCE_DIR}" 2>/dev/null \
&& python "${SCRIPT_DIR}"/makedoc.py
done
|
run_coverage.py¶
This script runs tests and coverage on the python_selenium package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | """
run_coverage.py
+++++++++++++++
Runs tests and coverage in *python_selenium/tests*.
Usage:
.. code-block:: bash
python run_coverage.py
| Author: shmakovpn <shmakovpn@yandex.ru>
| Date: 2020-01-28
"""
import os
# The path to the folder of this script
SCRIPT_DIR: str = os.path.dirname(os.path.abspath(__file__))
def run_coverage() -> None:
"""Executes *coverage run* command"""
coverage_cmd: str = f'coverage run --source=python_selenium -m pytest python_selenium/tests'
os.system(coverage_cmd)
report_cmd: str = 'coverage report'
os.system(report_cmd)
print('__END__')
if __name__ == '__main__':
run_coverage()
|
Autodoc documentation¶
run_coverage.py¶
Runs tests and coverage in python_selenium/tests.
Usage:
python run_coverage.pyAuthor: shmakovpn <shmakovpn@yandex.ru>Date: 2020-01-28
python_selenium¶
python_selenium package documentation.
python_selenium/proxy.py¶
python_selenium/proxy.py module documentation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """
python_selenium/proxy.py
++++++++++++++++++++++++
| Author: shmakovpn "shmakovpn<yandex.ru>"
| Date: 2012-01-28
"""
class Proxy:
"""
Proxy configuration
:param str protocol: The protocol of the proxy: *'http'*, *'https'* or *'socks'*.\
Defaults to *'http'*.
:param str ip: The IP address of the proxy.\
Default to the empty string (proxy isn't using).
:param int port: The TCP port of the proxy.\
Default to 80.
:param str login: The username of the proxy user.\
Default to the empty string (authentication isn't using).
:param str password: The password of the proxy user.\
Default to the empty string.
"""
def __init__(self,
protocol: str = 'http',
ip: str = '',
port: int = 80,
login: str = '',
password: str = '') -> None:
self.protocol = protocol
self.ip = ip
self.port = port
self.login = login
self.password = password
@property
def _auth(self) -> str:
"""
Returns an authentication part of the proxy URL
"""
if not self.login:
return ''
if not self.password:
return f'{self.login}@'
return f'{self.login}:{self.password}@'
@property
def url(self) -> str:
"""
Returns the proxy URL
"""
if not self.ip:
return ''
return f'{self.protocol}://{self._auth}{self.ip}:{self.port}'
# @property
# def to_dict(self) -> Dict[str, str]:
# return {
# 'http': f'{self.ip}:{self.port}',
# 'https': f'{self.ip}:{self.port}',
# 'socks': f'{self.ip}:{self.port}',
# }
def __str__(self) -> str:
return self.url
|
Autodoc documentation¶
python_selenium/proxy.py¶
-
class
python_selenium.proxy.
Proxy
(protocol: str = 'http', ip: str = '', port: int = 80, login: str = '', password: str = '')[source]¶ Proxy configuration
- Parameters
protocol (str) – The protocol of the proxy: ‘http’, ‘https’ or ‘socks’. Defaults to ‘http’.
ip (str) – The IP address of the proxy. Default to the empty string (proxy isn’t using).
port (int) – The TCP port of the proxy. Default to 80.
login (str) – The username of the proxy user. Default to the empty string (authentication isn’t using).
password (str) – The password of the proxy user. Default to the empty string.
-
property
url
¶ Returns the proxy URL
Tests¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """
./python_selenium/tests/proxy/test_proxy.py
+++++++++++++++++++++++++++++++++++++++++++
Tests for proxy.py
| Author: shmakovpn <shmakovpn@yandex.ru>
| Date: 2021-01-28
"""
from unittest import TestCase
from unittest import mock
from unittest.mock import PropertyMock, patch
from python_selenium.proxy import Proxy
class TestProxy(TestCase):
"""Testing the Proxy class"""
def test_defaults(self) -> None:
"""Testing default values"""
proxy: Proxy = Proxy()
self.assertEqual(proxy.protocol, 'http')
self.assertEqual(proxy.ip, '')
self.assertEqual(proxy.port, 80)
self.assertEqual(proxy.login, '')
self.assertEqual(proxy.password, '')
def test__auth(self) -> None:
"""Testing getter of an authentication part of the proxy URL"""
proxy: Proxy = Proxy()
proxy.login = ''
proxy.password = ''
self.assertEqual(proxy._auth, '') # type: ignore
proxy.login = 'python'
self.assertEqual(proxy._auth, 'python@') # type: ignore
proxy.password = 'the_best'
self.assertEqual(proxy._auth, 'python:the_best@') # type: ignore
@patch.object(Proxy,
'_auth',
new_callable=PropertyMock)
def test_url(self, mock__auth: PropertyMock) -> None:
"""Testign getter of the proxy URL"""
mock__auth.side_effect = lambda: 'L:P@'
proxy: Proxy = Proxy()
proxy.ip = ''
proxy.port = 81
proxy.protocol = 'HTTP'
self.assertEqual(proxy.url, '')
mock__auth.assert_not_called()
proxy.ip = 'IP'
self.assertEqual(proxy.url, 'HTTP://L:P@IP:81')
mock__auth.assert_called_once_with()
@patch.object(Proxy, 'url', new_callable=PropertyMock)
def test__str(self, mock_url: PropertyMock) -> None:
"""Testing __str__ of Proxy"""
mock_url.side_effect = lambda: 'URL'
proxy: Proxy = Proxy()
self.assertEqual(str(proxy), 'URL')
mock_url.assert_called_once_with()
|
Tests autodoc documentation¶
./python_selenium/tests/proxy/test_proxy.py¶
Tests for proxy.py
sphinx¶
sphinx hints.
Sphinx code-block types¶
Source materials are collected from here.
List of available languages