Skip to content

Commit

Permalink
Merge pull request #197 from jdi-testing/issue_1598-Pass-screen-resol…
Browse files Browse the repository at this point in the history
…ution-to-selenoid

Viewport dimensions passed to selenoid
  • Loading branch information
Bojana33 authored Aug 16, 2024
2 parents db3e79e + 787bfbb commit 389cf15
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PredictionRequestElement(BaseModel):
class PredictionInputModel(BaseModel):
document: str
elements: str
viewport: Dict


class PredictedElement(BaseModel):
Expand Down
9 changes: 6 additions & 3 deletions app/selenium_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ def get_page_elements(driver: webdriver.Remote, page_content: str) -> List[WebEl
return driver.find_elements(by=By.XPATH, value="//*")


def get_elements_visibility(page_content: str, starting_element_idx: int, ending_element_idx: int) -> Dict[str, bool]:
def get_elements_visibility(page_content: str, starting_element_idx: int, ending_element_idx: int,
viewport: Dict) -> Dict[str, bool]:
"""Returns a visibility of portion of elements contained in page_content
starting_element_idx and ending_element_idx are referring to the starting
and ending indexes for slice of page_content elements returned by
get_page_elements() function.
"""
driver = get_webdriver()
driver.set_window_size(viewport['width'], viewport['height'])
all_elements = get_page_elements(driver, page_content)

result = {}
Expand Down Expand Up @@ -92,14 +94,15 @@ def get_chunks_boundaries(data: Sized, desired_chunks_amount: int) -> Iterable[T
yield i * chunk_size, data_size


def get_element_id_to_is_displayed_mapping(page_content: str) -> Dict[str, bool]:
def get_element_id_to_is_displayed_mapping(page_content: str, viewport: Dict) -> Dict[str, bool]:
"""Returns visibility status of all elements in the page
Returned dictionary uses elements' jdn-hash property value as keys
"""
escaped_page_content = str(page_content).encode('utf-8').decode('unicode_escape')

driver = get_webdriver()
driver.set_window_size(viewport['width'], viewport['height'])
all_elements = get_page_elements(driver, escaped_page_content)
driver.quit()

Expand All @@ -109,7 +112,7 @@ def get_element_id_to_is_displayed_mapping(page_content: str) -> Dict[str, bool]

with concurrent.futures.ProcessPoolExecutor(max_workers=num_of_workers) as executor:
futures = [
executor.submit(get_elements_visibility, escaped_page_content, s, e)
executor.submit(get_elements_visibility, escaped_page_content, s, e, viewport)
for s, e in jobs_chunks
]
for future in concurrent.futures.as_completed(futures):
Expand Down
3 changes: 2 additions & 1 deletion ds_methods/angular_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async def angular_predict_elements(body):
body_json = json.loads(body_str)
elements_json = body_json.get("elements", [])
document_json = body_json.get("document", "")
viewport_json = body_json.get("viewport", {})

# create softmax layser function to get probabilities from logits
softmax = torch.nn.Softmax(dim=1)
Expand Down Expand Up @@ -111,7 +112,7 @@ async def angular_predict_elements(body):
del model
gc.collect()
result = results_df[columns_to_publish].to_dict(orient="records")
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json)
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json, viewport_json)
for element in result:
element["is_shown"] = element_id_to_is_displayed_map.get(element["element_id"], None)
return result
3 changes: 2 additions & 1 deletion ds_methods/html5_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async def html5_predict_elements(body):
body_json = json.loads(body_str)
elements_json = body_json.get("elements", [])
document_json = body_json.get("document", "")
viewport_json = body_json.get("viewport", {})

# generate temporary filename
filename = dt.datetime.now().strftime("%Y%m%d%H%M%S%f.json")
Expand Down Expand Up @@ -95,7 +96,7 @@ async def html5_predict_elements(body):
result = results_df[columns_to_publish].to_dict(orient="records")

logger.info("Determining visibility locators")
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json)
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json, viewport_json)
for element in result:
element["is_shown"] = element_id_to_is_displayed_map.get(element["element_id"], None)
return result
3 changes: 2 additions & 1 deletion ds_methods/mui_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async def mui_predict_elements(body):
body_json = json.loads(body_str)
elements_json = body_json.get("elements", [])
document_json = body_json.get("document", "")
viewport_json = body_json.get("viewport", {})

# create softmax layser function to get probabilities from logits
softmax = torch.nn.Softmax(dim=1)
Expand Down Expand Up @@ -113,7 +114,7 @@ async def mui_predict_elements(body):
del model
gc.collect()
result = results_df[columns_to_publish].to_dict(orient="records")
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json)
element_id_to_is_displayed_map = get_element_id_to_is_displayed_mapping(document_json, viewport_json)
for element in result:
element["is_shown"] = element_id_to_is_displayed_map.get(element["element_id"], None)
return result

0 comments on commit 389cf15

Please sign in to comment.