added tests, simple login test

This commit is contained in:
marko
2024-11-14 14:20:18 +01:00
parent b9b47c069a
commit 6e52d452eb
10 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
MAKE=make
.PHONY: TESTS
TESTS:
$(MAKE) --print-directory --directory ./test
.PHONY: INSTALL
INSTALL:
$(MAKE) --print-directory --directory ./test INSTALL
.PHONY: CLEAN
CLEAN:
$(MAKE) --print-directory --directory ./test CLEAN

View File

@@ -0,0 +1,3 @@
.venv
__pycache__

View File

@@ -0,0 +1,15 @@
PY=python
.PHONY: RUN
RUN: INSTALL
. ./run-tests
.PHONY: INSTALL
INSTALL: .venv
.PHONY: CLEAN
CLEAN:
rm -rf .venv
.venv: requirements.txt
. ./init-venv

View File

@@ -0,0 +1,3 @@
[credentials]
user = "____"
password = "****"

View File

@@ -0,0 +1,6 @@
[credentials]
username = "marko"
password = "kodokan"
[url]
home = "http://127.0.0.1/participo"

View File

@@ -0,0 +1,56 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
def load_config(config_path: str = "config.toml"):
from tomllib import load
with open(file=config_path, mode="rb") as config_file:
return load(config_file)
def login(driver: WebDriver, username: str, password:str):
driver.find_element(by=By.ID, value="username").send_keys(username)
driver.find_element(by=By.ID, value="password").send_keys(password)
driver.find_element(by=By.TAG_NAME, value="form").submit()
class Participo:
def __init__(self) -> None:
from selenium.webdriver import FirefoxOptions
options = FirefoxOptions()
# options.add_argument("-headless")
self.config = load_config()
self.driver = webdriver.Firefox(options=options)
self.delay = 5
def __enter__(self):
self.get_home()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.driver.quit()
def get_home(self):
self.driver.get(url=self.config["url"]["home"])
def login(self):
credentials = self.config["credentials"]
login(driver=self.driver, username=credentials["username"], password=credentials["password"])
def check_login(self):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
try:
logoutButton = WebDriverWait(driver=self.driver, timeout=self.delay).until(
EC.presence_of_element_located(
locator=(By.LINK_TEXT, "Logout")
)
)
return True
except NoSuchElementException:
return False

View File

@@ -0,0 +1,8 @@
#! /usr/bin/env bash
PY=python
${PY} -m venv .venv
. .venv/bin/activate
${PY} -m pip install --upgrade pip
${PY} -m pip install --requirement requirements.txt

View File

@@ -0,0 +1 @@
selenium

View File

@@ -0,0 +1,7 @@
#! /usr/bin/env bash
PY=python
. .venv/bin/activate
${PY} test_main.py
deactivate

View File

@@ -0,0 +1,15 @@
#! /usr/bin/env python
# testing the participo app
import unittest
from helper import Participo
class TestParticipo(unittest.TestCase):
def test_login(self):
with Participo() as participo:
participo.login()
self.assertTrue(participo.check_login())
if __name__ == "__main__":
unittest.main()