From ddabfc00c681614eb61878804964ae57b2782cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez=20Alonso?= Date: Tue, 14 May 2024 06:49:30 +0200 Subject: [PATCH 1/2] feat: add initial integration of playwright --- VERSION | 2 +- requirements.txt | 1 + toolium/behave/environment.py | 31 ++++++++++++++++-- toolium/pageobjects/playwright_page_object.py | 32 +++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 toolium/pageobjects/playwright_page_object.py diff --git a/VERSION b/VERSION index e94017a5..745e2a57 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.5.dev0 +3.2.0.dev0 diff --git a/requirements.txt b/requirements.txt index 281ce90d..32034f00 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ requests~=2.27 # api tests selenium~=4.0 # web tests +playwright~=1.43 # web tests Appium-Python-Client~=2.3 # mobile tests Pillow~=10.1 # visual testing screeninfo~=0.8 diff --git a/toolium/behave/environment.py b/toolium/behave/environment.py index b6f66f09..3752a05e 100644 --- a/toolium/behave/environment.py +++ b/toolium/behave/environment.py @@ -20,6 +20,9 @@ import os import re +from behave.api.async_step import use_or_create_async_context +from playwright.async_api import async_playwright + from toolium.utils import dataset from toolium.config_files import ConfigFiles from toolium.driver_wrapper import DriverWrappersPool @@ -225,6 +228,12 @@ def after_scenario(context, scenario): DriverWrappersPool.close_drivers(scope='function', test_name=scenario.name, test_passed=scenario.status in ['passed', 'skipped'], context=context) + # Stop playwright + if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright': + # TODO: reuse driver like in close_drivers + loop = context.async_context.loop + loop.run_until_complete(context.playwright.stop()) + # Save test status to be updated later if jira_test_status: add_jira_status(get_jira_key_from_scenario(scenario), jira_test_status, jira_test_comment) @@ -281,6 +290,22 @@ def start_driver(context, no_driver): :param context: behave context :param no_driver: True if this is an api test and driver should not be started """ - create_and_configure_wrapper(context) - if not no_driver: - connect_wrapper(context) + if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright': + start_playwright(context) + else: + create_and_configure_wrapper(context) + if not no_driver: + connect_wrapper(context) + + +def start_playwright(context): + """Start playwright with configured values + + :param context: behave context + """ + use_or_create_async_context(context) + loop = context.async_context.loop + context.playwright = loop.run_until_complete(async_playwright().start()) + # TODO: select browser from config + context.browser = loop.run_until_complete(context.playwright.chromium.launch(headless=False)) + context.page = loop.run_until_complete(context.browser.new_page()) diff --git a/toolium/pageobjects/playwright_page_object.py b/toolium/pageobjects/playwright_page_object.py new file mode 100644 index 00000000..d4e33ea6 --- /dev/null +++ b/toolium/pageobjects/playwright_page_object.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +""" +Copyright 2024 Telefónica Innovación Digital, S.L. +This file is part of Toolium. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +from playwright.async_api import Page +from toolium.pageobjects.page_object import PageObject + + +class PlaywrightPageObject(PageObject): + """Class to represent a playwright web page""" + + def __init__(self, page: Page): + """Initialize page object properties + + :param page: playwright page instance + """ + self.page = page + super(PlaywrightPageObject, self).__init__() From e7f6636251cfe454d8e98a45ce8246a459901d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez=20Alonso?= Date: Thu, 16 May 2024 11:54:21 +0200 Subject: [PATCH 2/2] avoid error in after_scenario --- toolium/behave/environment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolium/behave/environment.py b/toolium/behave/environment.py index 3752a05e..8236aa3f 100644 --- a/toolium/behave/environment.py +++ b/toolium/behave/environment.py @@ -229,7 +229,7 @@ def after_scenario(context, scenario): test_passed=scenario.status in ['passed', 'skipped'], context=context) # Stop playwright - if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright': + if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright' and hasattr(context, 'playwright'): # TODO: reuse driver like in close_drivers loop = context.async_context.loop loop.run_until_complete(context.playwright.stop())