behave / behave-django

Behave BDD integration for Django
https://behave-django.readthedocs.io/
MIT License
205 stars 46 forks source link

Add support for customized LiveServerTestCase with selenium-hub support #124

Open joepreludian opened 3 years ago

joepreludian commented 3 years ago

Hello guys, and thanks for the great work.

I'm using your library in order to add Behavior Driven Development capabilities into my application and I'm having a good experience with that. I'm not sure if this feature belongs to some Milestone, or it can be considered out of scope of this project, but I would like to give a try anyway.

The problem

One thing that I noticed, and I was struggling to get working into my instance was: I would like to have a browser ready for my selenium based testing using Splinter with a difference: I would like to connect to a browser available through an external tool like selenium-hub pointing to my django test server.

Actually behave-django already create a Test Server, but only listening localhost.

Suppose I'm using the following docker-compose structure, with my django app running under myapp service; I would like to connect to a remote webdriver into http://selenium-hub:4444/wd/hub and connect, during the behave run, to http://myapp:<a_test_port>.

Something like this:

from splinter.browser import Browser

# (...)
context.selenium_browser = Browser(driver_name='remote', browser='chrome',
                                                         command_executor='http://selenium-hub:4444/wd/hub')
# (...)

context.selenium_browser.visit(context.get_url())  # But this url will point to the right place since the browser belongs the same network as django app
version: "3.9"

services:
  database:
    image: docker.io/postgis/postgis:13-3.1-alpine
    env_file:
      - deploy/development.env
    volumes:
      - database_files:/var/lib/postgresql/data

  myapp:
    user: "1000:1001"
    image: mydjango_app:latest
    env_file:
      - deploy/development.env
    command: python manage.py runserver 0.0.0.0:8100
    volumes:
      - ./app:/app
      - ./deploy:/deploy
    ports:
      - "8100:8100"
    depends_on:
      - database

  gulp-watcher:
    user: "1000:1001"
    image: node:lts-alpine
    command: 'sh /app/deploy/node_yarn_n_run.sh npx gulp watch'
    working_dir: /app
    volumes:
      - ./:/app

  selenium-hub:
    image: selenium/hub:3.141.59
    depends_on:
      - myapp

  chrome:
    image: selenium/node-chrome:3.141.59
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
    user: "1000:1001"

volumes:
  database_files:

Remark about selenium-hub and chrome container

There is brilliant project called docker-selenium, maintained by the SeleniumHQ itself that create docker images ready for being used with a remote Webdriver. In a nutshell you can delegate the selenium calls to this API called selenium-hub that will work as a load balancer of browers, adding the ability to select different types of browsers (e.g. Chrome, Firefox, Edge and so on) using the same interface.

What is going to be the outcomes from this feature

By adding a way to perform such kind of tests, or a configuration param that allow us to listen a Live server in a local docker network, I would be able to point the selenium browser to http://myapp:port through a real context.get_url(), having a pretty smooth development and also this feature could be easily integrated to a CI/CD pipeline as well.

Some good finding about how to integrate it

I noticed that we already have a SeleniumTestCaseBase object on the stable branch of Django, interestingly configurable, as you can see on this link:

If you guys find this feature useful I can also create a PR for it with a suggestion. What it sounds like? Thank you!

bittner commented 3 years ago

I'm not sure what your change is going to do or look like. Is it overriding base_url in the environment module?