简体   繁体   中英

How to use django selenium testing in GitHub actions?

I am creating a Django project and I have created a test case that inherits from django.test.LiveSeverTestCase (so it uses selenium):

from django.test import LiveServerTestCase
from selenium.webdriver.chrome.webdriver import WebDriver

class FrontEndTestCase(LiveServerTestCase):
  @classmethod
  def setUpClass(cls):
    super().setUpClass()
    cls.selenium = WebDriver()
    cls.selenium.implicitly_wait(10)

    # Get the URL
    cls.selenium.get('http://127.0.0.1:8000/vota/')

    # Add CSRFToken 
    cls.selenium.add_cookie({'name': 'csrftoken', 'value': '1cY4Yb3SljOqj9tUndW1YlIokNOD8tNc2MSU5iKNvsZW8co9WoOOCVGd5RFzxD8P'})

    # Define page sections
    cls.choose_comps = cls.selenium.find_element_by_id('choose-comps')
    cls.poll = cls.selenium.find_element_by_id('poll-container')
    cls.end_table = cls.selenium.find_element_by_id('table-container')
    cls.navs = cls.selenium.find_elements_by_class_name('nav-link')


  @classmethod
  def tearDownClass(cls):
    cls.selenium.quit()
    super().tearDownClass()

The tests are irrelevant to my problem, so I won't include them for the sake of longness. As you probably realize, for this test case to work, I have to start the server with python manage.py runserver .

The problem here is that I want to use all these tests in GitHub Actions, and so far, I have, until creating this testcase. Since I have to start the server first, I made a new step in the job. But this step never ends since the server will always be listening for further requests. I also have to install somehow a chromedriver, a step I don't know how to do.

Here is my ci.yml file:

name: Testing
on: push

jobs:
  test_vote:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: juradofms
        ports:
          - 5432:5432
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run migrations
      run: python manage.py migrate
      env:
        # Random key
        SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'
    - name: Runserver
      run: python manage.py runserver
      env:
        # Random key
        SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'
    - name: Test
      run: python manage.py test
      env:
        # Random key
        SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'

My solution for the runserver problem would be somehow running this command on another thread, how can I do this?

In this case you need to move the start server process in the background.

You can modify the command to include an "&"

python manage.py runserver &

See How to run Linux commands in background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM