Skip to content

Commit

Permalink
Merge pull request #25 from mreiche/feature/webdriver-detection
Browse files Browse the repository at this point in the history
Feature/webdriver detection
  • Loading branch information
mreiche authored Jul 19, 2024
2 parents fd166de + 74509cc commit 65d85f2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ jobs:

runs-on: ubuntu-latest
container:
image: ghcr.io/mreiche/paf-test-base:chrome-116
image: ghcr.io/mreiche/paf-test-base:latest
credentials:
username: mreiche
password: ${{ secrets.DOCKER_CONTAINER_REGISTRY_TOKEN }}

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
# - name: Set up Python 3.10
# uses: actions/setup-python@v3
# with:
# python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install -r requirements.txt
# - name: List chromedriver
# run: |
# ls -lahF
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pytest --cov=paf -n=4 test
podman build -f ubuntu-base.Dockerfile --arch=amd64 -t paf-test-base:latest
source build.env
echo $DOCKER_CONTAINER_REGISTRY_TOKEN | podman login -u mreiche --password-stdin ghcr.io
podman push paf-test-base:latest docker://ghcr.io/mreiche/paf-test-base:chrome-116
podman push paf-test-base:latest docker://ghcr.io/mreiche/paf-test-base:latest
```
#### Build test runner container (for testing)
Expand Down Expand Up @@ -168,3 +168,4 @@ snapshot.snapshotItem(0).textContent
- https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/
- https://stackoverflow.com/questions/46052736/python-proxy-class
- https://chromedriver.chromium.org/
- Make stealth WebDriver: https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver
33 changes: 33 additions & 0 deletions examples/test_monkeytype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import inject
import pytest

from paf.control import change
from paf.manager import WebDriverManager
from paf.page import PageFactory, FinderPage
from test import create_webdriver


@pytest.fixture
def finder():
page_factory = inject.instance(PageFactory)
finder = page_factory.create_page(FinderPage, create_webdriver())
yield finder


def test_typespeed(finder: FinderPage):
finder.open("https://monkeytype.com")

input_container = finder.find("#wordsInput")
words_container = finder.find("#words")

active_word = words_container.find(".active")

with change(wait_after_fail=3):
while active_word.wait_for.displayed(True):
word = active_word.expect.text.actual
input_container.send_keys(word)
input_container.send_keys(" ")


def teardown_module():
inject.instance(WebDriverManager).shutdown_all()
31 changes: 31 additions & 0 deletions test/test_detect_webdriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import inject
import pytest
from selenium.webdriver import ChromeOptions

from paf.manager import WebDriverManager
from paf.request import WebDriverRequest
from test import create_webdriver


def test_detect_webdriver():
request = WebDriverRequest("automated")
request.browser = "chrome"
webdriver = create_webdriver(request)
result = webdriver.execute_script("return navigator.webdriver")
assert result is True


@pytest.mark.skip(reason="Does not work in test image")
def test_hide_webdriver():
request = WebDriverRequest("non-automated")
request.browser = "chrome"
options = ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
request.options = options
webdriver = create_webdriver(request)
result = webdriver.execute_script("return navigator.webdriver")
assert result is False


def teardown_module():
inject.instance(WebDriverManager).shutdown_all()
4 changes: 2 additions & 2 deletions test/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ def test_given_chrome_options(manager: WebDriverManager):
request.browser = "chrome"
request.options = ChromeOptions()
webdriver = create_webdriver(request)
assert webdriver.name == request.browser
assert request.browser in webdriver.name


def test_not_given_chrome_options(manager: WebDriverManager):
request = WebDriverRequest("chrome-no-options")
request.browser = "chrome"
webdriver = create_webdriver(request)
assert webdriver.name == request.browser
assert request.browser in webdriver.name


@pytest.mark.skipif(
Expand Down
24 changes: 13 additions & 11 deletions ubuntu-base.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ FROM ubuntu:22.04
WORKDIR /home

RUN apt -y update \
&& apt -y install unzip wget default-jre-headless \
&& wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& apt -y install ./google-chrome-stable_current_amd64.deb \
&& rm google-chrome-stable_current_amd64.deb \
&& apt -y install unzip curl default-jre-headless python3 python3-pip python3-venv \
&& curl -fLo chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& apt -y install ./chrome.deb \
&& rm chrome.deb \
&& ln -s /opt/google/chrome/chrome /usr/local/bin \
&& apt clean

RUN wget -O selenium-server.jar https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.9.0/selenium-server-4.9.0.jar \
&& wget https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE \
&& wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/`cat LATEST_RELEASE_STABLE`/linux64/chromedriver-linux64.zip \
&& unzip "chromedriver-linux64.zip" \
RUN curl -fLo LATEST_RELEASE_STABLE https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE \
&& curl -fLo chromedriver.zip https://storage.googleapis.com/chrome-for-testing-public/`cat LATEST_RELEASE_STABLE`/linux64/chromedriver-linux64.zip \
&& unzip "chromedriver.zip" \
&& ln -s "/home/chromedriver-linux64/chromedriver" /usr/local/bin \
&& rm "chromedriver-linux64.zip" \
&& rm "chromedriver.zip" \
&& rm LATEST_RELEASE_STABLE

RUN java -jar selenium-server.jar standalone --version \
RUN python3 -m venv venv \
&& curl -fLo selenium-server.jar https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.9.0/selenium-server-4.9.0.jar \
&& java -jar selenium-server.jar standalone --version \
&& chrome --version \
&& chromedriver --version \
&& python3 --version
&& python3 --version \
&& pip --version

0 comments on commit 65d85f2

Please sign in to comment.