Skip to content

Commit

Permalink
[yashaka#552] DOCS: how to add chrome extension
Browse files Browse the repository at this point in the history
Style update
  • Loading branch information
roman-isakov committed Aug 2, 2024
1 parent 57da7ca commit 9d50330
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions docs/faq/adding-chrome-extension-howto.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# How to add Chrome extension in Selene?


### An example how to remove ads in Chrome with [uBlock Origin extension](https://chromewebstore.google.com/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm) by installing `.crx` file
## An example how to remove ads in Chrome with [uBlock Origin extension](https://chromewebstore.google.com/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm) by installing `.crx` file

**Preliminary steps**
### Preliminary steps
1. Download and store extension. (Example: by using [crxextractor](https://crxextractor.com/))

**Example project structure**
### Example project structure
```
📦my-project
┣ 📂my_project
Expand All @@ -24,17 +24,17 @@
┗ 📜pyproject.toml
```

**Code description**
### Code description

`my_project / resources / __init__.py`: help function
#### `my_project / resources / __init__.py`: help function
```python
from pathlib import Path

path = Path(__file__).resolve().parent
```


`conftest.py`: install extension and navigate into it's settings.
#### `tests / conftest.py`: install extension and navigate into it's settings
```python
import pytest
from selene import browser, have, Element, be
Expand All @@ -44,53 +44,50 @@ from selenium import webdriver
from my_project import resources



@pytest.fixture(autouse=True)
def browser_with_ublock():
ublock_path = resources.path / 'chrome_extensions/uBlock-Origin.crx'
ublock_id = 'cjpalhdlnbpafiamejdnhcphjbkeiagm' # it's a unique constant for uBlock Origin
ublock_id = (
'cjpalhdlnbpafiamejdnhcphjbkeiagm' # it's a unique constant for uBlock Origin
)
options = webdriver.ChromeOptions()
options.add_extension(ublock_path)
browser.config.driver_options = options

browser.open('chrome://extensions/')
js = f"return document.querySelector('body > extensions-manager')\
.shadowRoot.querySelector('#items-list')\
.shadowRoot.querySelector('#{ublock_id}')\
.shadowRoot.querySelector('#card')"
js = f'''return document.querySelector('body > extensions-manager')
.shadowRoot.querySelector('#items-list')
.shadowRoot.querySelector('#{ublock_id}')
.shadowRoot.querySelector('#card')'''
card = Element(
Locator(
'ublock extension card',
lambda: browser.execute_script(js)
),
browser.config)
# Specific behaviour for uBlock extension. Initially it is enabled, then disabled, then enabled again.
# You might want to increase timeout.
card.should(have.css_class('disabled')).should(have.css_class('enabled'))

browser.open(
f"chrome-extension://{ublock_id}/dashboard.html#about.html"
Locator('ublock extension card', lambda: browser.execute_script(js)),
browser.config,
)
# Specific behaviour for uBlock extension.
# Initially it is enabled, then disabled, then enabled again.
card.should(have.css_class('disabled')).should(have.css_class('enabled'))
# You might want to increase timeout.
# card.with_(timeout=browser.config.timeout*1.5).should(...).should(...)
browser.open(f'chrome-extension://{ublock_id}/dashboard.html#about.html')
browser.switch_to.frame(browser.element('iframe')())
browser.element('#aboutNameVer').should(be.visible).should(
have.text("uBlock Origin")
have.text('uBlock Origin')
)

return browser

```

`tests / test_ublock.py`
#### `tests / test_ublock.py`: verify that uBlock extension rules are applied to site with ads

```python
from selene import browser, have


def test_verify_noads_rules_are_applied():
def test_verify_applied_rules():
# Act
browser.open('https://d3ward.github.io/toolz/adblock')
browser.element('#close-icon').with_(click_by_js=True).click() # close modal
browser.element('#dlg_changelog').element('button').with_(
click_by_js=True).click()
browser.element('#dlg_changelog button').with_(click_by_js=True).click()

# Assert
browser.element('#adb_test_r').should(have.text('0 not blocked'))
Expand Down

0 comments on commit 9d50330

Please sign in to comment.