Skip to content

Commit

Permalink
Returning empty string if JS function was unable to find good locator
Browse files Browse the repository at this point in the history
Also, not throwing stacktraces to the frontend
  • Loading branch information
ivnglkv committed Apr 20, 2024
1 parent 38de87a commit 7b6bd0a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/css_locators/auxiliary/generate_css_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const generateSelectorByElement = (element) => {
} else if (!isSelectorByGeneratorString && isSelectorByFinderString) {
selectorGenerationResult = selectorByFinder;
} else {
selectorGenerationResult = 'CSS selector generation was failed';
selectorGenerationResult = "";
}
return selectorGenerationResult;
};
17 changes: 10 additions & 7 deletions app/css_locators/tasks.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import logging
from pathlib import Path

from selenium.common.exceptions import WebDriverException

from app.celery_app import celery_app
from app.selenium_app import get_webdriver


logger = logging.getLogger(__name__)


@celery_app.task(bind=True)
def task_schedule_css_locator_generation(self, element_id: int, document_path: str) -> str:
driver = get_webdriver()

driver.get(f"file:///html/{Path(document_path).name}")
result = None

# noinspection PyBroadException
try:
result = driver.execute_script(
f"""
el = document.querySelector('[jdn-hash="{element_id}"]');
return generateSelectorByElement(el);
"""
)
except WebDriverException:
pass
finally:
return result
except Exception as exc:
logger.exception("Error generating CSS locator")
raise RuntimeError("Error generating CSS locator") from exc

return result

0 comments on commit 7b6bd0a

Please sign in to comment.