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.

source code
 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.py
Author: shmakovpn <shmakovpn@yandex.ru>
Date: 2020-01-13
makedoc.run_sphinx() → None[source]

Executes sphinx-build command

auto_makedoc.sh

This script automatically executes makedoc.py on changes in the documentation source folder.

source code
 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.

source code
 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.py
Author: shmakovpn <shmakovpn@yandex.ru>
Date: 2020-01-28
run_coverage.run_coverage() → None[source]

Executes coverage run command

python_selenium

python_selenium package documentation.

python_selenium/proxy.py

python_selenium/proxy.py module documentation

source code
 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
Author: shmakovpn “shmakovpn<yandex.ru>”
Date: 2012-01-28
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
test_proxy source code
 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

Author: shmakovpn <shmakovpn@yandex.ru>
Date: 2021-01-28
class python_selenium.tests.proxy.test_proxy.TestProxy(methodName='runTest')[source]

Testing the Proxy class

test__auth() → None[source]

Testing getter of an authentication part of the proxy URL

test__str(mock_url: unittest.mock.PropertyMock) → None[source]

Testing __str__ of Proxy

test_defaults() → None[source]

Testing default values

test_url(mock__auth: unittest.mock.PropertyMock) → None[source]

Testign getter of the proxy URL

sphinx

sphinx hints.

Sphinx code-block types

Source materials are collected from here.

List of available languages

abap
abnf
ada, ada95, ada2005
adl
agda
ahk, autohotkey
alloy
ampl
antlr-as, antlr-actionscript
antlr-cpp
antlr-csharp, antlr-c#
antlr-java
antlr-objc
antlr-perl
antlr-python
antlr-ruby, antlr-rb
antlr
apacheconf, aconf, apache
apl
applescript
arduino
as, actionscript
as3, actionscript3
aspectj
aspx-cs
aspx-vb
asy, asymptote
at, ambienttalk, ambienttalk/2
autoit
awk, gawk, mawk, nawk
basemake
bash, sh, ksh, shell
bat, batch, dosbatch, winbatch
bbcode
bc
befunge
blitzbasic, b3d, bplus
blitzmax, bmax
bnf
boo
boogie
brainfuck, bf
bro
bugs, winbugs, openbugs
c-objdump
c
ca65
cadl
camkes, idl4
cbmbas
ceylon
cfc
cfengine3, cf3
cfm
cfs
chai, chaiscript
chapel, chpl
cheetah, spitfire
cirru
clay
clean
clojure, clj
clojurescript, cljs
cmake
cobol
cobolfree
coffee-script, coffeescript, coffee
common-lisp, cl, lisp
componentpascal, cp
console, shell-session
control, debcontrol
coq
cpp, c++
cpp-objdump, c++-objdumb, cxx-objdump
cpsa
crmsh, pcmk
croc
cryptol, cry
csharp, c#
csound, csound-orc
csound-document, csound-csd
csound-score, csound-sco
css+django, css+jinja
css+erb, css+ruby
css+genshitext, css+genshi
css+lasso
css+mako
css+mako
css+mozpreproc
css+myghty
css+php
css+smarty
css
cucumber, gherkin
cuda, cu
cypher
cython, pyx, pyrex
d-objdump
d
dart
delphi, pas, pascal, objectpascal
dg
diff, udiff
django, jinja
docker, dockerfile
doscon
dpatch
dtd
duel, jbst, jsonml+bst
dylan-console, dylan-repl
dylan-lid, lid
dylan
earl-grey, earlgrey, eg
easytrieve
ebnf
ec
ecl
eiffel
elixir, ex, exs
elm
emacs, elisp, emacs-lisp
erb
erl
erlang
evoque
extempore
ezhil
factor
fan
fancy, fy
felix, flx
fish, fishshell
flatline
fortran
fortranfixed
foxpro, vfp, clipper, xbase
fsharp
gap
gas, asm
genshi, kid, xml+genshi, xml+kid
genshitext
glsl
gnuplot
go
golo
gooddata-cl
gosu
groff, nroff, man
groovy
gst
haml
handlebars
haskell, hs
haxeml, hxml
hexdump
hsail, hsa
html+cheetah, html+spitfire, htmlcheetah
html+django, html+jinja, htmldjango
html+evoque
html+genshi, html+kid
html+handlebars
html+lasso
html+mako
html+mako
html+myghty
html+php
html+smarty
html+twig
html+velocity
html
http
hx, haxe, hxsl
hybris, hy
hylang
i6t
idl
idris, idr
iex
igor, igorpro
inform6, i6
inform7, i7
ini, cfg, dosini
io
ioke, ik
irc
isabelle
j
jade
jags
jasmin, jasminxt
java
javascript+mozpreproc
jcl
jlcon
js+cheetah, javascript+cheetah, js+spitfire, javascript+spitfire
js+django, javascript+django, js+jinja, javascript+jinja
js+erb, javascript+erb, js+ruby, javascript+ruby
js+genshitext, js+genshi, javascript+genshitext, javascript+genshi
js+lasso, javascript+lasso
js+mako, javascript+mako
js+mako, javascript+mako
js+myghty, javascript+myghty
js+php, javascript+php
js+smarty, javascript+smarty
js, javascript
jsgf
json
jsonld, json-ld
jsp
julia, jl
kal
kconfig, menuconfig, linux-config, kernel-config
koka
kotlin
lagda, literate-agda
lasso, lassoscript
lcry, literate-cryptol, lcryptol
lean
less
lhs, literate-haskell, lhaskell
lidr, literate-idris, lidris
lighty, lighttpd
limbo
liquid
live-script, livescript
llvm
logos
logtalk
lsl
lua
make, makefile, mf, bsdmake
mako
mako
maql
mask
mason
mathematica, mma, nb
matlab
matlabsession
minid
modelica
modula2, m2
monkey
moocode, moo
moon, moonscript
mozhashpreproc
mozpercentpreproc
mql, mq4, mq5, mql4, mql5
mscgen, msc
mupad
mxml
myghty
mysql
nasm
ncl
nemerle
nesc
newlisp
newspeak
nginx
nimrod, nim
nit
nixos, nix
nsis, nsi, nsh
numpy
objdump-nasm
objdump
objective-c++, objectivec++, obj-c++, objc++
objective-c, objectivec, obj-c, objc
objective-j, objectivej, obj-j, objj
ocaml
octave
odin
ooc
opa
openedge, abl, progress
pacmanconf
pan
parasail
pawn
perl, pl
perl6, pl6
php, php3, php4, php5
pig
pike
pkgconfig
plpgsql
postgresql, postgres
postscript, postscr
pot, po
pov
powershell, posh, ps1, psm1
praat
prolog
properties, jproperties
protobuf, proto
ps1con
psql, postgresql-console, postgres-console
puppet
py3tb
pycon
pypylog, pypy
pytb
python, py, sage
python3, py3
qbasic, basic
qml, qbs
qvto, qvt
racket, rkt
ragel-c
ragel-cpp
ragel-d
ragel-em
ragel-java
ragel-objc
ragel-ruby, ragel-rb
ragel
raw
rb, ruby, duby
rbcon, irb
rconsole, rout
rd
rebol
red, red/system
redcode
registry
resource, resourcebundle
rexx, arexx
rhtml, html+erb, html+ruby
roboconf-graph
roboconf-instances
robotframework
rql
rsl
rst, rest, restructuredtext
rts, trafficscript
rust
sass
sc, supercollider
scala
scaml
scheme, scm
scilab
scss
shen
silver
slim
smali
smalltalk, squeak, st
smarty
sml
snobol
sourceslist, sources.list, debsources
sp
sparql
spec
splus, s, r
sql
sqlite3
squidconf, squid.conf, squid
ssp
stan
swift
swig
systemverilog, sv
tads3
tap
tcl
tcsh, csh
tcshcon
tea
termcap
terminfo
terraform, tf
tex, latex
text
thrift
todotxt
trac-wiki, moin
treetop
ts, typescript
turtle
twig
typoscript
typoscriptcssdata
typoscripthtmldata
urbiscript
vala, vapi
vb.net, vbnet
vcl
vclsnippets, vclsnippet
vctreestatus
velocity
verilog, v
vgl
vhdl
vim
wdiff
x10, xten
xml+cheetah, xml+spitfire
xml+django, xml+jinja
xml+erb, xml+ruby
xml+evoque
xml+lasso
xml+mako
xml+mako
xml+myghty
xml+php
xml+smarty
xml+velocity
xml
xquery, xqy, xq, xql, xqm
xslt
xtend
xul+mozpreproc
yaml+jinja, salt, sls
yaml
zephir

Indices and tables