I have a more complicated question, but I am trying to narrow down the issue to avoid any confusion.
While testing a page using Selenium, I encountered an anomaly. The page contains two external JavaScript scripts. When visiting the page manually, everything works fine. However, when using Selenium, one of the JavaScript files fails to load:
The script from “http://localhost:55234/static/js/common.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
2 add-name
Loading failed for the <script> with source “http://localhost:55234/static/js/common.js”.
Upon checking the page source (right-click => view page source), I can see that the template includes the following lines among others:
[...]
<!-- load global javascript -->
<script type='text/javascript' src="/static/js/common.js"></script>
<!-- load app javascript -->
<script type='text/javascript' src="/static/lists/js/lists.js"></script>
[...]
You can click on the `src` links in the source. Clicking the first link removes the last two lines related to `lists.js`, which means it fails to load:
<!-- load app javascript -->
<script type='text/javascript' src="/static/lists/js/lists.js"></script>
Clicking the second link (`lists.js`) reveals an old version of the code. It seems like the browser cache might be causing this discrepancy since you recently disabled caching. However, these settings are different when the page is opened through Selenium. Would clearing the cache resolve the issue?
You also shared a code snippet to disable the cache entirely while setting up the WebDriver. Have you tried implementing this solution already?
Here's an excerpt from your codebase:
My template base.html
:
[...]
<head>
[...]
<!-- load global javascript -->
<script type='text/javascript' src="{% static '/js/common.js' %}"></script>
<!-- load app javascript -->
{% block script %}{% endblock %}
[...]
my template list.html
{% block script %}
<script type='text/javascript' src="{% static 'lists/js/lists.js' %}"></script>
{% endblock %}
Based on the path references provided, everything seems correct in terms of file locations and inclusion within templates.
Within test.py
:
class NameFormSeleniumTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(2)
def tearDown(self):
self.browser.quit()
def test_form_can_save_a_name(self):
# some code
# english = ...
self.browser.get(self.live_server_url + '/lists/add-name/')
Select(self.browser.find_element_by_id('id_namelanguage')).\
select_by_value(str(english.id))
# ...
breakpoint()
form = NameForm({'nametype': nome.id, 'gender': maschio.id,
'name': 'Remo', 'namelanguage': english.id,
'usato': 0})
print(form.errors)
form.save()
The error message thrown during testing indicates a validation failure because the necessary data wasn't present. Furthermore, inspection via `breakpoint()` shows that a specific JavaScript function is missing, leading to incomplete form submission and subsequent errors.
Your insights and assistance are greatly appreciated.