Skip to content

Added custom fixture to fix session merge issue #9

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

Open
wants to merge 3 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
59 changes: 59 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import threading
import urllib
import uuid
#import threading
from multiprocessing import Process, Lock
import pytest
from browserstack.local import Local
import os, json
from jsonmerge import merge
from dotenv import load_dotenv
import time

from playwright.sync_api import Playwright
from playwright.sync_api import Page

lock = Lock()
threaded_count = 0

load_dotenv()

TASK_ID = int(os.environ['TASK_ID']) if 'TASK_ID' in os.environ else 0


BROWSERSTACK_USERNAME = os.environ['BROWSERSTACK_USERNAME']
BROWSERSTACK_ACCESS_KEY = os.environ['BROWSERSTACK_ACCESS_KEY']


def update_session_name(page, session_name=''):
if not (page and session_name):
return

command = {
"action": "setSessionName",
"arguments": {
"name": session_name
}
}

browser_stack_command = f"browserstack_executor: {json.dumps(command)}"
result = page.evaluate("() => {}", browser_stack_command)
return json.loads(result)

@pytest.fixture(scope='function')
def session_capabilities(playwright: Playwright):
global timenow
global lock
test_name = os.environ.get('PYTEST_CURRENT_TEST').split(' ')[0].split('::')[1]
capabilities = {}
stringifiedCaps = urllib.parse.quote(json.dumps(capabilities))
caps = 'wss://cdp.browserstack.com/playwright?caps=' + stringifiedCaps
browser = playwright.chromium.launch()
context = browser.new_context()
page = context.new_page()
update_session_name(page,test_name)

yield page
context.close()
browser.close()

3 changes: 2 additions & 1 deletion tests/sample-local-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest
from playwright.sync_api import expect

def test_bstack_local_sample(page) -> None:
def test_bstack_local_sample(session_capabilities) -> None:
page = session_capabilities
try:
#Navigate to the base url
page.goto("http://bs-local.com:45454", timeout=0)
Expand Down
3 changes: 2 additions & 1 deletion tests/sample-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from playwright.sync_api import expect


def test_bstack_sample(page) -> None:
def test_sample(session_capabilities) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same change for local test as well

page = session_capabilities
try:
# Navigate to the base url
page.goto("https://bstackdemo.com/", timeout=0)
Expand Down