Skip to content

Commit

Permalink
Port to Python 3.10, bug fixes and code quality imporvements
Browse files Browse the repository at this point in the history
  • Loading branch information
inquilabee committed Sep 1, 2023
1 parent 3f9beac commit e46c846
Show file tree
Hide file tree
Showing 26 changed files with 948 additions and 544 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Cache
**_cache**
69 changes: 49 additions & 20 deletions .pre-commit-config.yaml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
exclude: '.git|.tox'
exclude: '^docs/|/migrations/'
default_stages: [ commit ]
fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-json
- id: check-toml
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: detect-private-key

- repo: https://github.com/psf/black
rev: 20.8b1
- repo: https://github.com/asottile/pyupgrade
rev: v3.7.0
hooks:
- id: black
- id: pyupgrade
args: [ --py311-plus ]

- repo: https://github.com/timothycrosley/isort
rev: 5.6.4
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.280
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
types_or: [ python, pyi, jupyter ]

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: flake8
additional_dependencies: [ flake8-isort ]
- id: bandit
args: [ "-c", "bandit.yaml" ]

- repo: local
- repo: https://github.com/sourcery-ai/sourcery
# Source: https://docs.sourcery.ai/Guides/Getting-Started/Pre-Commit/
# https://sourcery.ai/blog/python-best-practices/
rev: v1.6.0
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
- id: sourcery
# only check the files which have changed: does not work on first commit
args: [ --diff=git diff HEAD, --no-summary ]
# args: [ --diff=git diff HEAD, --fix, --no-summary ]
# args: [ --fix, --no-summary ]


# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: [ ]
submodules: false
41 changes: 41 additions & 0 deletions .sourcery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#https://docs.sourcery.ai/Reference/Configuration/sourcery-yaml/

ignore:
- .git
- venv
- .venv
- env
- .env
- .tox
- node_modules
- vendor
- repository_directory

rule_settings:
enable: [default]
disable: []
rule_types:
- refactoring
- suggestion
- comment
python_version: '3.11'

rules: []

metrics:
quality_threshold: 25.0

github:
labels: []
ignore_labels:
- sourcery-ignore
request_review: author
sourcery_branch: sourcery/{base_branch}

clone_detection:
min_lines: 3
min_duplicates: 2
identical_clones_only: false

proxy:
no_ssl_verify: false
99 changes: 60 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### Simple Selenium

The aim of this package is to quickly get started with working with selenium for simple browser automation tasks.
Selenium with Tab Management

<small> With all the already available flexibility and features of Selenium </small>

### Installation

Expand All @@ -10,13 +12,36 @@ Install from PyPI
pip install simpleselenium
```

### Core Idea

A `browser` has many `tabs`.

Action/activity on `Tab` object
- Check if the tab is alive (i.e. it has not been closed)
- Switch to a tab
- See/obtain page source, title and headings
- work on a specific tab (click elements, scroll and so on.)
- ... many more

Action/activity on `Browser` object
- Open a new tab with url
- Get a list of open tabs
- Get active tab
- Switch to a tab of the browser (first, last or any one).
- close a tab
- Close the browser

### Working with driver objects

`driver` object available on any `Tab` object.

### Features

Some basic feature is being listed below.
Some basic features are being listed below (also see the `Usage` section below):

- easy management of different tabs
- switching to a tab is super easy
- know if a tab is active or alive
- know if a tab is active (current tab) or alive
- closing a tab is easy as `browser.close_tab(tab_object)`
- Several (built-in) functions
- `tab.infinite_scroll()`
Expand All @@ -35,58 +60,54 @@ method off it which returns a Tab object.
#### Browser

```python
import time # just to slow down stuffs and see things for testing
from simpleselenium import Browser

chrome_driver = r"/path/to/chromedriver"
from simpleselenium import Browser, Tab

with Browser(name="Chrome", driver_path=chrome_driver, implicit_wait=10) as browser:
google = browser.open("https://google.com")
yahoo = browser.open("https://yahoo.com")
bing = browser.open("https://bing.com")
duck_duck = browser.open("https://duckduckgo.com/")
# name is one of "Chrome" or "FireFox"
# driver path is not required in most cases

print(yahoo) # A Tab Object
print(yahoo.is_alive)
print(yahoo.is_active)
print(dir(yahoo)) # All methods and attributes of Tab Objects
with Browser(name="Chrome", driver_path=None, implicit_wait=10) as browser:
google: Tab = browser.open("https://google.com") # a `Tab` object
yahoo = browser.open("https://yahoo.com")
bing = browser.open("https://bing.com")
duck_duck = browser.open("https://duckduckgo.com/")

print(browser.get_all_tabs()) # List of tab objects
yahoo.scroll_up(times=5)
yahoo.scroll_down(times=10)

print(browser.tabs.all())
print(browser.tabs) # TabManager object
print(dir(browser.tabs)) # All methods and attributes of TabManager Objects
print(browser.tabs)
print(browser.current_tab)
print(browser.first_tab)
print(browser.last_tab)

browser.close_tab(bing) # close a browser tab
print(browser.tabs.all())
print(browser.last_tab.switch())

print(browser.get_current_tab()) # current tab
time.sleep(5)
print(google.page_source)
print(google.title)
print(google.url)
print(google.is_active)
print(google.is_alive)

yahoo.switch() # switch/focus/tap to/on `yahoo` tab
print(browser.get_current_tab())
time.sleep(5)
browser.close_tab(bing)
print(browser.tabs)

google.switch()
print(browser.get_current_tab())
time.sleep(5)
print(browser.current_tab)

browser.close_tab(yahoo)
time.sleep(5)
yahoo.switch()
print(browser.current_tab)
google.switch()

print(google.driver) # Usual selenium driver object which can be worked upon
print(browser.current_tab)

print(google.driver.title, google.title)
browser.close_tab(yahoo)

print(google.scroll_to_bottom())
print(google.is_active)
print(google.is_alive)
print(bing.is_alive) # False, it has been deleted.
print(yahoo.is_alive)
print(yahoo.is_active)

print(browser.get_all_tabs())
print(google.driver.title, google.title)
print(google.driver.title == google.title)
```

### TODO

- Complete documentation
- Test Code
10 changes: 10 additions & 0 deletions bandit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# also see pyproject.toml for [tool.bandit]
# https://bandit.readthedocs.io/en/1.7.5/config.html
# exclude = tests,path/to/file
# tests = B201,B301

exclude_dirs: [ 'tests/', 'repository_directory/', "tox" ]
tests: [ ]
skips: [
'B113' # request.get without a timeout
]
Empty file removed example.env
Empty file.
30 changes: 30 additions & 0 deletions log_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# YAML copied from: https://realpython.com/python-logging/#other-configuration-methods
version: 1
formatters:
extended:
format: '%(asctime)-20s :: %(levelname)-8s :: [%(process)d]%(processName)s :: %(threadName)s[%(thread)d] :: %(pathname)s :: %(lineno)d :: %(message)s'
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: extended
filename: info.log
encoding: utf8
maxBytes: 10000
backupCount: 10
mode: a
loggers:
sampleLogger:
level: DEBUG
handlers: [console, file_handler]
propagate: no
root:
level: DEBUG
handlers: [console, file_handler]
Loading

0 comments on commit e46c846

Please sign in to comment.