i’m working on a scenario with Robot Framework where I need to modify three values in an HTML file and then execute it using Chrome. However, I’m struggling to find information on how to achieve this, especially since I'm familiar with loading JSON and XML files but can't find a way to load HTML files.
Does anyone have any suggestions?
Here’s the content of the HTML file:
<html lang="en">
<head>
<meta charset="utf-8">
<title>The Title</title>
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="css/styles.css?v=1.0">
</head>
<body>
<form id="formulaire" method="post" action="Variable1">
<input type="hidden" id="PaReq" name="PaReq" value="Variable2"/>
<input type="hidden" id="TermUrl" name="TermUrl" value="Variable3"/>
<input type="hidden" id="MD" name="MD" value=""/>
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
Well i was looking for library or a pre-defined method to do it but nothing was found, so i made a simple python script that i use later in a robotframework keyword, here is the code
# update_html.py
import os
import sys
from bs4 import BeautifulSoup
def update_html(file_path, new_action, new_pareq_value, new_termurl_value):
file_path = os.path.join(os.path.dirname(__file__), 'templates', 'acs.html')
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
form = soup.find('form')
form['action'] = new_action
pareq_input = soup.find('input', {'id': 'PaReq'})
pareq_input['value'] = new_pareq_value
termurl_input = soup.find('input', {'id': 'TermUrl'})
termurl_input['value'] = new_termurl_value
with open(file_path, 'w', encoding='utf-8') as file:
file.write(str(soup))
if __name__ == "__main__":
# Vérifier si le bon nombre d'arguments a été fourni
if len(sys.argv) != 4:
print("Usage: python update_html.py <new_action> <new_pareq_value> <new_termurl_value>")
sys.exit(1)
new_action = sys.argv[1]
new_pareq_value = sys.argv[2]
new_termurl_value = sys.argv[3]
update_html('acs.html', new_action, new_pareq_value, new_termurl_value)
And for execute it, i added a library Process then i used Run Process python3 ${script_path} ${paReq} ${termUrl} ${redirectionUrl}