Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo initialized with test cases #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .codegenignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.gitignore
.github/**
tests/e2etests/**
tests/unitTests/**
tests/__init__.py
tests/http_response_catcher.py
e2e-requirements.txt
34 changes: 34 additions & 0 deletions .github/workflows/run-e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Playwright Tests
on:
workflow_dispatch:
jobs:
flows-tests:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r test-requirements.txt
pip install -r e2e-requirements.txt
- name : Create env
run: |
echo "EMAIL=${{ secrets.EMAIL }}" >> .env
echo "PASSWORD=${{ secrets.PASSWORD }}" >> .env
echo "CLIENT_ID=${{ secrets.CLIENT_ID }}" >> .env
echo "CLIENT_SECRET=${{ secrets.CLIENT_SECRET }}" >> .env
- name: Ensure browsers are installed
run: python -m playwright install --with-deps
- name: Running E2E tests
run: pytest --tracing=retain-on-failure
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-traces
path: test-results/
2 changes: 2 additions & 0 deletions e2e-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-dotenv
pytest-playwright
Empty file added tests/__init__.py
Empty file.
Empty file added tests/e2etests/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions tests/e2etests/e2e_test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import os
import unittest
from dotenv import load_dotenv
from paypalserversdk.configuration import Configuration
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient
from tests.http_response_catcher import HttpResponseCatcher


class E2ETestBase(unittest.TestCase):

"""E2E Tests Class inherits from this class. It abstracts out
common functionality and configuration variables set up."""

client = None
config = None

@classmethod
def setUpClass(cls):
"""Class method called once before running tests in a test class."""
cls.request_timeout = 30
cls.assert_precision = 0.01
cls.config = E2ETestBase.create_configuration()
cls.client = PaypalserversdkClient(config=cls.config)

@staticmethod
def create_configuration():
## Load the environment variables
load_dotenv()
o_auth_client_id = os.getenv("CLIENT_ID")
o_auth_client_secret = os.getenv("CLIENT_SECRET")
client_credentials_auth_credentials = ClientCredentialsAuthCredentials(
o_auth_client_id= o_auth_client_id,
o_auth_client_secret= o_auth_client_secret
)
return Configuration(http_call_back=HttpResponseCatcher() , client_credentials_auth_credentials=client_credentials_auth_credentials)
127 changes: 127 additions & 0 deletions tests/e2etests/playwrightflows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import os
import random
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright

# Load environment variables from .env file
load_dotenv()
email = os.getenv('EMAIL')
password = os.getenv('PASSWORD')

class PlaywrightFlows:
"""A utility class for handling Playwright flows within E2E tests."""
def __init__(self):
# Attributes
self.p = sync_playwright().start()
self.browser = self.p.firefox.launch(headless=True)
self.page = None
self.isSuccessful = False

# Destructor
def destruct(self):
self.browser.close()
self.p.stop()

# Method to generate a new page
def generate_new_page(self):
self.isSuccessful = False
self.context = self.browser.new_context(ignore_https_errors=True, viewport={'width': 1920, 'height': 1080})
self.context.set_default_timeout(8000)
self.context.tracing.start(screenshots=True, snapshots=True)
self.page = self.context.new_page()

# Method to teardown the page
def teardown(self):
self.page.close()
if not self.isSuccessful:
random_name = str(random.randint(1000, 9999))
self.context.tracing.stop(path=f'test-results/{random_name}.zip')
self.context.close()

# Method for logging in used in save_payment and complete_payment methods
def login(self , timeout: int = 10000 ):
if email and password:
self.page.fill('#email', email)
if self.page.is_visible('#btnNext'):
self.page.wait_for_selector('#btnNext', timeout=timeout)
self.page.click('#btnNext')
self.page.fill('#password', password)
self.page.wait_for_selector('#btnLogin', timeout=timeout)
self.page.click('#btnLogin')
else:
raise ValueError('EMAIL or PASSWORD environment variable is not set')

# Method for complete payment flow
def complete_payment(self, checkout_url: str, timeout: int = 10000) -> bool:
try:
self.page.goto(checkout_url)
self.login(timeout=timeout)
try:
self.page.wait_for_selector('#payment-submit-btn', timeout=timeout)
except Exception:
self.page.go_back(timeout=timeout)
self.page.wait_for_selector('#payment-submit-btn', timeout=timeout)
self.page.click('#payment-submit-btn')
try:
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
except Exception:
self.page.reload()
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
self.isSuccessful = True
return True
except Exception as error:
print(f'Error completing payment: {error}')
return False

# Method for saving payment method
def save_payment(self, url: str , timeout: int = 10000) -> bool:
try:
self.page.goto(url)
self.login(timeout=timeout)
try:
self.page.wait_for_selector('#consentButton', timeout=timeout)
except Exception :
self.page.go_back(timeout=timeout)
self.page.wait_for_selector('#consentButton', timeout=timeout)
self.page.click('#consentButton')
try:
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
except Exception:
self.page.reload()
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
self.isSuccessful = True
return True
except Exception as error:
print(f'Error saving payment method: {error}')
self.page.screenshot(path='error_screenshot.png')
return False

# Method for completing Helios verification
def complete_helios_verification(self, checkout_url: str , timeout: int = 10000) -> bool:
try:
self.page.goto(checkout_url)
try:
self.page.wait_for_url('https://www.sandbox.paypal.com/webapps/helios?action=verify&flow=3ds**', timeout=timeout)
except Exception:
self.page.reload(timeout=timeout)
self.page.wait_for_url('https://www.sandbox.paypal.com/webapps/helios?action=verify&flow=3ds**', timeout=timeout)
self.page.wait_for_load_state('networkidle', timeout=timeout)
frame = self.page.frame_locator('iframe[name="threedsIframeV2"]').frame_locator('iframe')
input_field = frame.get_by_placeholder(' Enter Code Here' )
submit_button = frame.get_by_role('button', name='SUBMIT' )
if input_field.is_visible():
input_field.click()
input_field.fill('1234')
if submit_button.is_visible():
submit_button.click()
try:
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
except Exception:
self.page.reload()
self.page.wait_for_url('https://example.com/returnUrl**', timeout=timeout)
self.isSuccessful = True
return True
except Exception as error:
self.page.screenshot(path='error_screenshot.png')
print(f'Error completing payment: {error}')
return False
Loading