Skip to content

Commit

Permalink
update the code
Browse files Browse the repository at this point in the history
  • Loading branch information
atharvar28 committed Dec 22, 2023
1 parent 578762f commit 25b3e1e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 44 deletions.
23 changes: 23 additions & 0 deletions gen3-integration-tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import os
import pytest
from playwright.sync_api import sync_playwright
from playwright.async_api import async_playwright


from pathlib import Path

Expand Down Expand Up @@ -63,3 +66,23 @@ def pytest_configure(config):
def test_data_path():
"""Fixture to be used when a test needs test data"""
return Path(__file__).parent / "test_data"


# Synchronous fixture for Playwright
@pytest.fixture
def browser():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page() # Create a new page
yield page
# browser.close()


# Asynchronous fixture for Playwright
@pytest.fixture
async def async_browser():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page() # Create a new page asynchronously
yield page
await browser.close()
56 changes: 36 additions & 20 deletions gen3-integration-tests/services/hatchery.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,70 @@ def __init__(self, page):
self.BASE_URL = f"{pytest.root_url}"
self.WORKSPACE_TAB = f"{self.BASE_URL}/workspace"
# Locators
self.workspace_page = ".workspace" # Workspace Page
self.workspace_options = ".workspace_options" # Workspace Options
self.generic_card = (
"//div[contains(text(), /^(Generic)/)]" # Juptyer WS Generic Card
self.workspace_page = "//div[@class='workspace ']" # Workspace Page
self.workspace_options = (
"//div[@class='workspace__options']" # Workspace Options
)
self.launch_button = ".workspace-option__button" # Launch Button
self.workspace_iframe_locator = 'iframe[title="Workspace"]' # IFrame workspace
self.generic_card = "//div[@class='workspace-option' and starts-with(h3[@class='workspace-option__title'], '(Generic)')]" # Juptyer WS Generic Card
self.workspace_spinner = "//div[@class='workspace__spinner-container']"
self.workspace_iframe = "//div[@class='workspace__iframe']"
# Locators inside the workspace iframe
self.workspace_launcher_frame = 'iframe[title="Workspace"]' # IFrame workspace
self.python_command_field = "//div[@class='CodeMirror cm-s-jupyter']"

def go_to_workspace_page(self):
"""Goes to workspace page and checks if loaded correctly"""
self.page.goto(self.WORKSPACE_TAB)
expect(self.page.locator(self.workspace_page)).to_be_visible
self.page.wait_for_selector(self.workspace_page, state="visible")
self.page.screenshot(path="output/workspacePage.png", full_page=True)

def open_jupyter_workspace(self):
"""Launch a jupyter workspace"""
expect(self.page.locator(self.workspace_options)).to_be_visible
expect(self.page.locator(self.workspace_options)).to_be_visible()
# find the workspace option for Juptyer WS to launch
generic_card = self.page.locator(self.generic_card)
expect(generic_card).to_be_visible()
# click the launch button from the juptyer workspace card
generic_card.page_locator(self.launch_button).click()
launch_button_xpath = f"{self.generic_card}//button[text()='Launch']"
launch_button = self.page.locator(launch_button_xpath)
launch_button.click()
self.page.screenshot(path="output/jupyterWorkspace.png", full_page=True)
self.page.wait_for_selector(self.workspace_spinner, state="visible")
# after launch, workspace takes around 6 mins to load and launc
self.page.wait_for_selector(
self.workspace_iframe, state="visible", timeout=324000
)

def open_python_kernel(self):
"""perform drs pull in workspace page"""
workspace_iframe = self.page.frame_locator(self.workspace_iframe_locator)
# here the frame is on the page, so page.locator is used
workspace_iframe = self.page.locator(self.workspace_launcher_frame)
expect(workspace_iframe).to_be_visible
# here the element is inside the frame, so page.frame_locator is used
python_kernel_nb = (
self.page.frame_locator(self.workspace_iframe_locator)
self.page.frame_locator(self.workspace_launcher_frame)
.get_by_title("Python 3 (ipykernel)")
.first
)
python_kernel_nb.click()
expect(
self.page.frame_locator(self.workspace_iframe_locator).get_by_label(
"notebook content"
)
).to_be_visible
self.page.wait_for_timeout(3000)
self.page.screenshot(path="output/pythonKernel.png")
commandPrompt = self.page.frame_locator(self.workspace_launcher_frame).locator(
self.python_command_field
)
expect(commandPrompt).to_be_visible

def run_command_notebook(self):
fill_command = (
self.page.frame_locator(self.workspace_iframe_locator)
self.page.frame_locator(self.workspace_launcher_frame)
.get_by_label("notebook content")
.locator("pre")
)
fill_command.click()
self.page.frame_locator(self.workspace_iframe_locator).get_by_label(
self.page.frame_locator(self.workspace_launcher_frame).get_by_label(
"notebook content"
).get_by_role("textbox").fill("!gen3 drs-pull manifest <manifest-name>")
self.page.frame_locator(self.workspace_iframe_locator).get_by_role(
).get_by_role("textbox").fill("!gen3 --help")
self.page.frame_locator(self.workspace_launcher_frame).get_by_role(
"button", name="Run the selected cells and advance"
).click()

Expand Down
44 changes: 29 additions & 15 deletions gen3-integration-tests/services/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def __init__(self, page):
self.BASE_URL = f"{pytest.root_url}"
self.LOGIN_URL = f"{self.BASE_URL}/login"
# Locators
self.nav_bar = ".nav_bar" # homepage navigation bar
self.nav_bar = "//div[@class='nav-bar']" # homepage navigation bar
self.username_locator = "//div[@class='top-bar']//a[3]" # username locator
self.pop_up_box = ".pop_up_box" # pop_up_box
self.pop_up_box = "//div[@id='popup']" # pop_up_box

def go_to_page(self):
"""Goes to the login page"""
self.page.goto(self.LOGIN_URL)
expect(self.page.locator(self.nav_bar)).to_be_visible
self.page.wait_for_selector(self.nav_bar, state="visible")
self.page.screenshot(path="output/LoginPage.png", full_page=True)

def login(self, user="main_account"):
Expand All @@ -44,32 +44,46 @@ def login(self, user="main_account"):
)
expect(login_button).to_be_visible(timeout=5000)
login_button.click()
self.page.screenshot(path="output/Afterlogin.png", full_page=True)
username = self.page.wait_for_selector(self.username_locator)
assert (username.text) == pytest.users[user]
access_token_cookie = self.page.context.cookies(
url=self.BASE_URL, name="access_token"
self.page.wait_for_timeout(3000)
self.page.screenshot(path="output/AfterLogin.png", full_page=True)
self.page.wait_for_selector(self.username_locator, state="attached")

self.handle_popup()
self.page.screenshot(path="output/AfterPopUpAccept.png", full_page=True)
access_token_cookie = next(
(
cookie
for cookie in self.page.context.cookies()
if cookie["name"] == "access_token"
),
None,
)
assert access_token_cookie is not None
# cookies = page.context.cookies()
# for cookie in cookies:
# if cookie["name"] == "access_token":
# break
assert (
access_token_cookie is not None
), "Access token cookie not found after login"

def logout(self):
"""Logs out and wait for Login button on nav bar"""
self.page.get_by_role("button", name="Logout").click()
nav_bar_login_button = self.page.get_by_role("button", name="Login")
self.page.screenshot(path="Afterlogout.png")
self.page.screenshot(path="output/AfterLogout.png")
expect(nav_bar_login_button).to_be_visible

# function to handle pop ups after login
def handle_popup(self):
"""Handling popups after login"""
popup_message = self.page.query_selector(self.pop_up_box)
if popup_message:
self.page.screenshot(path="popupBox.png", full_page=True)
logger.info("Popup message found")
self.page.evaluate(
f"""
(element) => {{
element.scrollTop = element.scrollHeight;
}}
""",
popup_message,
)
self.page.screenshot(path="output/PopupBox.png")
accept_button = self.page.get_by_role("button", name="Accept")
if accept_button:
accept_button.click()
Expand Down
21 changes: 12 additions & 9 deletions gen3-integration-tests/tests/portal/test_hatchery.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""
Hatchery tests
1.
Hatchery Test
"""

import os
import pytest

from cdislogging import get_logger
import utils.gen3_admin_tasks as gat

from playwright.sync_api import sync_playwright, expect, Page
from playwright.sync_api import Page

from services.login import LoginPage
from services.hatchery import Hatchery
Expand All @@ -19,15 +17,20 @@

@pytest.mark.hatchery
class TestHatchery:
def test_workspace_drs_pull(self, page: Page):
hatchery = Hatchery(page)
login_page = LoginPage(page)
def test_workspace_drs_pull(self, browser: Page):
hatchery = Hatchery(browser)
login_page = LoginPage(browser)
logger.info("# Logging in with mainAcct")
login_page.go_to_page()
"""login with mainAcct user"""
login_page.login()
login_page.handle_popup()
"""navigates to workspace page and sees workspace_options"""
hatchery.go_to_workspace_page()
hatchery.open_jupyter_workspace()
"""launches the workspace Generic notebook"""
# hatchery.open_jupyter_workspace()
"""opens python kernel in notebook"""
hatchery.open_python_kernel()
"""executes gen3 --help command"""
hatchery.run_command_notebook()
"""terminates the workspace after executing the command"""
hatchery.terminate_workspace()

0 comments on commit 25b3e1e

Please sign in to comment.