Skip to content

Commit

Permalink
replacing seleniumbase with playwright (#216)
Browse files Browse the repository at this point in the history
* replacing seleniumbase with playwright

* get playwright to install browsers in GH workflow

* fix workflow

* force english version in integration tests
  • Loading branch information
bbonf authored Jun 18, 2024
1 parent 624a75d commit 5bda904
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 496 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/django-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ jobs:
- name: Integration tests
run: |
cd ${{ matrix.app }}
playwright install
export DJANGO_ALLOW_ASYNC_UNSAFE=1
make testint
- uses: actions/upload-artifact@v1
if: failure()
Expand Down Expand Up @@ -165,7 +167,9 @@ jobs:
- name: Integration tests
run: |
cd integration_tests
pytest --headless2
playwright install
export DJANGO_ALLOW_ASYNC_UNSAFE=1
pytest
- uses: actions/upload-artifact@v1
if: failure()
with:
Expand Down
44 changes: 22 additions & 22 deletions integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

import django
import pytest
from seleniumbase.common.exceptions import NoSuchElementException

from lab_settings import EMAIL_FILE_PATH

from playwright.sync_api import Page, Browser

LAB_PORT = 18000
PARENT_PORT = 19000

Expand Down Expand Up @@ -138,23 +139,23 @@ def apps(lab_app, parent_app):
return namedtuple("Apps", "lab,parent")(lab_app, parent_app)


def set_language_english(page):
loc = page.locator("button").get_by_text("English")
if loc.count():
loc.click()


@pytest.fixture
def as_admin(sb, lab_app):
def as_admin(browser: Browser, lab_app):
lab_app.load("admin")
driver = sb.get_new_driver()
sb.switch_to_driver(driver)
sb.open(lab_app.url + '/login')
sb.type("#id_username", "admin")
sb.type("#id_password", "admin")
sb.click('button:contains("Log in")')
try:
sb.click('button:contains("English")')
except NoSuchElementException:
# ugly workaround for language setting
pass

sb.switch_to_default_driver()
return driver
context = browser.new_context()
page_admin = context.new_page()
page_admin.goto(lab_app.url + '/login')
set_language_english(page_admin)
page_admin.fill("#id_username", "admin")
page_admin.fill("#id_password", "admin")
page_admin.locator('button').get_by_text("Log in").click()
return page_admin


def read_mail(address):
Expand Down Expand Up @@ -197,17 +198,16 @@ def _delegate(email, subject=None):


@pytest.fixture
def login_as(sb, apps, link_from_mail, mailbox):
def login_as(page: Page, apps, link_from_mail, mailbox):

def _delegate(email):
sb.switch_to_default_driver()
sb.open(apps.parent.url + 'auth/')
sb.type('input[name="email"]', email)
sb.click('button:contains("Send")')
page.goto(apps.parent.url + 'auth/')
page.fill('input[name="email"]', email)
page.locator('button').get_by_text('Send').click()

# use login link from (second) email
if link := link_from_mail(email, 'Link'):
sb.open(link)
page.goto(link)
return True
return False

Expand Down
26 changes: 14 additions & 12 deletions integration_tests/test_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import pytest

from playwright.sync_api import expect


@pytest.fixture
def participant(apps):
Expand All @@ -25,7 +27,7 @@ def participant(apps):
participant.delete()


def test_cancel_appointment_from_email(apps, participant, mailbox, link_from_mail, sb):
def test_cancel_appointment_from_email(apps, participant, mailbox, link_from_mail, page):
apps.lab.load('admin') # generate admin user
Experiment = apps.lab.get_model('experiments', 'Experiment')
DefaultCriteria = apps.lab.get_model('experiments', 'DefaultCriteria')
Expand All @@ -46,7 +48,7 @@ def test_cancel_appointment_from_email(apps, participant, mailbox, link_from_mai

send_appointment_mail(appointment, prepare_appointment_mail(appointment))

sb.open(link_from_mail(participant.email))
page.goto(link_from_mail(participant.email))

# check that the appointment was canceled
appointment.refresh_from_db()
Expand All @@ -64,13 +66,13 @@ def test_cancel_appointment_from_email(apps, participant, mailbox, link_from_mai
appointment.delete()


def test_appointment_in_parent_overview(apps, participant, mailbox, sb, login_as):
def test_appointment_in_parent_overview(apps, participant, mailbox, page, login_as):
apps.lab.load('admin') # generate admin user
Experiment = apps.lab.get_model('experiments', 'Experiment')
DefaultCriteria = apps.lab.get_model('experiments', 'DefaultCriteria')
User = apps.lab.get_model('main', 'User')
experiment = Experiment.objects.create(defaultcriteria=DefaultCriteria.objects.create(),
name='Text Experiment')
name='Test Experiment')

# somewhat abusing the get_model() calls above to setup django for the following to work
from experiments.models import make_appointment
Expand All @@ -84,21 +86,21 @@ def test_appointment_in_parent_overview(apps, participant, mailbox, sb, login_as

login_as(participant.email)
try:
sb.assert_text_visible('Appointments')
sb.assert_text_visible(experiment.name)
sb.assert_text_visible(leader.name)
expect(page.get_by_text('Appointments')).to_be_visible()
expect(page.get_by_text(experiment.name)).to_be_visible()
expect(page.get_by_text(leader.name)).to_be_visible()
finally:
# delete appointment so that the participant can be deleted as well
appointment.delete()


def test_past_appointment_not_in_parent_overview(apps, participant, mailbox, sb, login_as):
def test_past_appointment_not_in_parent_overview(apps, participant, mailbox, page, login_as):
apps.lab.load('admin') # generate admin user
Experiment = apps.lab.get_model('experiments', 'Experiment')
DefaultCriteria = apps.lab.get_model('experiments', 'DefaultCriteria')
User = apps.lab.get_model('main', 'User')
experiment = Experiment.objects.create(defaultcriteria=DefaultCriteria.objects.create(),
name='Text Experiment')
name='Test Experiment')

# somewhat abusing the get_model() calls above to setup django for the following to work
from experiments.models import make_appointment
Expand All @@ -112,9 +114,9 @@ def test_past_appointment_not_in_parent_overview(apps, participant, mailbox, sb,

login_as(participant.email)
try:
sb.assert_text_visible('Appointments')
sb.assert_text_not_visible(experiment.name)
sb.assert_text_not_visible(leader.name)
expect(page.get_by_text('Appointments')).to_be_visible()
expect(page.get_by_text(experiment.name)).not_to_be_visible()
expect(page.get_by_text(leader.name)).not_to_be_visible()
finally:
# delete appointment so that the participant can be deleted as well
appointment.delete()
Loading

0 comments on commit 5bda904

Please sign in to comment.