Skip to content

Commit

Permalink
Merge pull request #140 from aodn/testing/138-add-map-tests
Browse files Browse the repository at this point in the history
Playwright: Add map tests
  • Loading branch information
utas-raymondng authored Sep 2, 2024
2 parents 8e780cf + 574f4f2 commit 891de8e
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dist-ssr
.vscode/*
!.vscode/extensions.json
!.vscode/launch.json
!.vscode/settings.json
playwright-report/*
.idea
.DS_Store
Expand Down
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.envFile": "${workspaceFolder}/playwright/.env.debug",
"python.testing.pytestArgs": [
"playwright/tests",
"--rootdir=playwright",
"-c /dev/null" /* ignore configfile: pytest.ini */
]
}
Empty file added playwright/core/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions playwright/core/enums/layer_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from enum import Enum, auto


class LayerType(Enum):
MARINE_PARKS = auto()
WORLD_BOUNDARIES = auto()
SPIDER = auto()
15 changes: 15 additions & 0 deletions playwright/core/factories/layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from core.enums.layer_type import LayerType
from pages.components.map import Map


class LayerFactory:
def __init__(self, map: Map):
self.map = map

def get_layer_id(self, layer_type: LayerType) -> str:
if layer_type == LayerType.MARINE_PARKS:
return self.map.get_AU_Marine_Parks_Layer_id()
elif layer_type == LayerType.WORLD_BOUNDARIES:
return self.map.get_World_Boundaries_Layer_id()
elif layer_type == LayerType.SPIDER:
return self.map.get_Spider_Layer_id()
4 changes: 3 additions & 1 deletion playwright/mocks/mock_data/collections_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -2312,5 +2312,7 @@
],
"itemType": "Collection"
}
]
],
"total": 17,
"search_after": ["c13451a9-7cfe-091c-e044-00144f7bc0f4"]
}
4 changes: 3 additions & 1 deletion playwright/mocks/mock_data/collections_bbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,7 @@
},
"itemType": "Collection"
}
]
],
"total": 17,
"search_after": ["c13451a9-7cfe-091c-e044-00144f7bc0f4"]
}
4 changes: 3 additions & 1 deletion playwright/mocks/mock_data/collections_update_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
],
"itemType": "Collection"
}
]
],
"total": 1,
"search_after": ["8112a083-019e-4b70-9404-431648892026"]
}
4 changes: 3 additions & 1 deletion playwright/mocks/mock_data/collections_update_bbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
},
"itemType": "Collection"
}
]
],
"total": 1,
"search_after": ["8112a083-019e-4b70-9404-431648892026"]
}
77 changes: 77 additions & 0 deletions playwright/pages/components/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from playwright.sync_api import Page

from pages.base_page import BasePage
from pages.js_scripts.js_utils import execute_js, load_js_functions


class Map(BasePage):
def __init__(self, page: Page):
self.page = page
load_js_functions(page)

# Page locators
self.basemap_show_hide_menu = page.get_by_label(
'basemap-show-hide-menu'
)

def hover_map(self) -> None:
"""Move the mouse cursor to the center of the map"""
self.page.get_by_label('Map', exact=True).hover()

def drag_map(self) -> None:
"""Drag the map to a fixed distance using the mouse"""
self.hover_map()
# Calculate the center coordinates
bounding_box = self.page.get_by_label('Map', exact=True).bounding_box()
x, y = 0.0, 0.0
if bounding_box is not None:
x = bounding_box['x'] + bounding_box['width'] / 2
y = bounding_box['y'] + bounding_box['height'] / 2

self.page.mouse.down()
self.page.mouse.move(x + 150, y + 50) # fixed mouse move
self.page.mouse.up()

def center_map(self, lng: str, lat: str) -> None:
"""Center the map to a given longitude and latitude coordinates"""
execute_js(self.page, 'centerMap', lng, lat, '12')
self.wait_for_map_loading()

def wait_for_map_loading(self) -> None:
"""Wait until the map is fully loaded"""
self.page.wait_for_function('() => {return isMapLoaded();}')

def click_map(self) -> None:
"""Click the map at the current position of the mouse"""
self.page.mouse.down()
self.page.mouse.up()

def get_map_layers(self) -> str:
"""Get the total number of heatmap layers"""
self.wait_for_map_loading()
layers = execute_js(self.page, 'getMapLayers')
return str(layers)

def get_Layer_id_from_test_props(self, layer_function_name: str) -> str:
"""Get specific layer id"""
self.wait_for_map_loading()
layer_id = execute_js(self.page, layer_function_name)
return str(layer_id)

def get_AU_Marine_Parks_Layer_id(self) -> str:
"""Get the Australian Marine Parks layer id"""
return self.get_Layer_id_from_test_props('getAUMarineParksLayer')

def get_World_Boundaries_Layer_id(self) -> str:
"""Get the World Boundaries layer id"""
return self.get_Layer_id_from_test_props('getWorldBoundariesLayer')

def get_Spider_Layer_id(self) -> str:
"""Get the Spider layer id"""
return self.get_Layer_id_from_test_props('getSpiderLayer')

def is_map_layer_visible(self, layer_id: str) -> bool:
"""Check whether a given map layer is visible"""
self.wait_for_map_loading()
is_visible = execute_js(self.page, 'isMapLayerVisible', layer_id)
return is_visible
2 changes: 2 additions & 0 deletions playwright/pages/components/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class SearchComponent(BasePage):
def __init__(self, page: Page):
self.page = page

# Page locators
self.search_field = page.get_by_test_id('input-with-suggester')
self.search_button = self.get_button(text='Search')

Expand Down
18 changes: 18 additions & 0 deletions playwright/pages/js_scripts/js_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from typing import Any

from playwright.sync_api import Page


def load_js_functions(page: Page) -> None:
current_dir = os.path.dirname(os.path.abspath(__file__))
js_file_path = os.path.join(current_dir, 'map_functions.js')
with open(js_file_path, 'r') as file:
js_code = file.read()
page.add_init_script(js_code)


def execute_js(page: Page, func_name: str, *args: Any) -> Any:
args_str = ', '.join(map(repr, args))
script = f'{func_name}({args_str});'
return page.evaluate(script)
49 changes: 49 additions & 0 deletions playwright/pages/js_scripts/map_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function getTestProps() {
return window.testProps;
}

function getMap() {
return getTestProps().getMap();
}

function getHeatmapLayer() {
return getTestProps().getHeatmapLayer();
}

function centerMap(lng, lat, zoom) {
const map = getMap();
map.setZoom(zoom);
map.setCenter([lng, lat]);
}

function isMapLoaded() {
const map = getMap();
return map.loaded();
}

function getMapLayers() {
const map = getMap();
const layer = getHeatmapLayer();
return map.queryRenderedFeatures({ layers: [layer] }).length;
}

function getAUMarineParksLayer() {
return getTestProps().getAUMarineParksLayer
? getTestProps().getAUMarineParksLayer()
: "";
}

function getWorldBoundariesLayer() {
return getTestProps().getWorldBoundariesLayer
? getTestProps().getWorldBoundariesLayer()
: "";
}

function getSpiderLayer() {
return getTestProps().getSpiderLayer ? getTestProps().getSpiderLayer() : "";
}

function isMapLayerVisible(layerId) {
const map = getMap();
return undefined != map.getLayer(layerId);
}
6 changes: 5 additions & 1 deletion playwright/pages/search_page.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from playwright.sync_api import Page

from pages.base_page import BasePage
from pages.components.map import Map
from pages.components.search import SearchComponent


class SearchPage(BasePage):
def __init__(self, page: Page):
self.page = page
self.search = SearchComponent(page)
self.first_result_title = page.get_by_test_id("result-card-title").first
self.map = Map(page)

# Page locators
self.first_result_title = page.get_by_test_id('result-card-title').first

def wait_for_updated_search_result(self) -> None:
"""Wait until the second search result is detached"""
Expand Down
Loading

0 comments on commit 891de8e

Please sign in to comment.