Issue
I have a Vue.js
app which I want to write selenium tests for. When I run a test against the deployed production version of the app the test passes but it fails when running it against a local version.
Of course when running locally the Vue.js
app isn't running from a build but is instead running using npm run serve
.
I have the following docker compose
setup locally:
app:
build:
context: .
dockerfile: ./app.dockerfile
image: app
command: npm run serve
ports:
- 8080:8080
volumes:
- ./app:/app
selenium:
image: selenium/standalone-chrome:91.0
depends_on:
- app
e2e-tests:
build:
context: .
dockerfile: ./e2e_tests.dockerfile
image: e2e-tests
command: poetry run python ./foo.py
depends_on:
- app
- selenium
volumes:
- ./e2e_tests:/app
Here's a basic test that works correctly using the e2e-tests
and selenium
container to test the deployed production version of the app out on the internet:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1420,1080")
chrome_options.add_argument("--headless")
driver = webdriver.Remote("http://selenium:4444/wd/hub", options=chrome_options)
driver.get('https://www.example.com')
try:
assert 'My app title' in driver.title
except AssertionError:
print('invalid title')
driver.quit()
If I now change the above test to use the app
container which is simply a locally running version of the Vue app it fails because the title is empty:
driver.get('http://app:8080')
I have tried adding a 10 second wait
to the test after the driver.get
but the title is still empty.
Solution
It turns out the app was returning Invalid Host Header
when selenium tried to call it from within docker even though I can access the app through a browser. Must be some docker related idiosyncrasy.
The solution was to update my vue.config.js
as follows:
module.exports = {
devServer: {
disableHostCheck: true
}
};
The disableHostCheck
solved the issue. Apparently this should only be used while in dev as it would be a security risk if enabled in production.
Answered By - darkpool
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.