This commit is contained in:
Chris Talib 2024-06-25 16:51:25 +02:00
commit b3158687ac
5 changed files with 80 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.env/

9
Dockerfile Normal file
View file

@ -0,0 +1,9 @@
FROM selenium/standalone-chrome:4.0.0
ENV SELENIUM_HOST=localhost \
SELENIUM_PORT=4444
EXPOSE 4444
# docker run -d -p 4444:4444 --shm-size="4g" -t selenium
# docker logs -f hash

29
prepare_script.py Normal file
View file

@ -0,0 +1,29 @@
import re
import sys
def add_console_log_to_js(js_file_path):
with open(js_file_path, 'r') as file:
lines = file.readlines()
return_pattern = re.compile(r'\s*return\s+(.*);')
modified_lines = []
for i, line in enumerate(lines):
match = return_pattern.search(line)
if match:
return_expression = match.group(1)
indentation = len(line) - len(line.lstrip())
log_statement = f'{" " * indentation}console.log("Returning from function at line {i + 1}: ", {return_expression});\n'
modified_lines.append(log_statement)
modified_lines.append(line)
with open(js_file_path, 'w') as file:
file.writelines(modified_lines)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python prepare_script.py <path_to_js_file>")
sys.exit(1)
js_file_path = sys.argv[1]
add_console_log_to_js(js_file_path)

31
run_js.py Normal file
View file

@ -0,0 +1,31 @@
import sys
from selenium import webdriver
def run_js_in_browser(js_file_path):
with open(js_file_path, 'r') as file:
js_code = file.read()
options = webdriver.ChromeOptions()
options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options,
)
driver.get("about:blank")
res = driver.execute_script(js_code)
print(res)
for entry in driver.get_log('browser'):
print(entry)
driver.quit()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python run_js.py <path_to_js_file>")
sys.exit(1)
js_file_path = sys.argv[1]
run_js_in_browser(js_file_path)

10
test2.js Normal file
View file

@ -0,0 +1,10 @@
var a = "Hello from the other side";
console.log(a);
function greet() {
var b = "Hello darkness my old friend";
console.log("Returning from function at line 6: ", b);
return b;
}
greet();