Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
transfered meetup tests from automated-testing repo
Browse files Browse the repository at this point in the history
Signed-off-by: Fenn-25 <fenn25.fn@gmail.com>
  • Loading branch information
nfebe authored and prachi1210 committed May 25, 2018
1 parent 74595b0 commit 8454cf5
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ local/

# Documentation build
_build

# pytest

.pytest_cache
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
sudo: false
language: python
env:
- MOZ_HEADLESS=1

addons:
firefox: "55.0"

python:
- "3.6"
before_install:
- wget https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-linux64.tar.gz
- tar -xzvf geckodriver-v0.20.1-linux64.tar.gz
- sudo mv geckodriver /usr/local/bin
install:
- "pip install -r requirements/dev.txt"
- pip install coveralls==0.4.4
Expand Down
37 changes: 37 additions & 0 deletions AUTOMATED_TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Automated Testing
=================

Documentation on running automated tests with selenium for systers/portal.

Setup Selenium (Unix)
---------------------

To run selenium test you would need to install (download) the standalone servers which implements WebDrivers' wire protocols :
* geckodriver and
* chromedriver

is used here not withstanding, other WebDriver servers may be used with a little tweaking (which entails adding the desired webdriver in the browsers dictionary in the conftest.py)

### Installation

1. Download chromedriver [here](https://sites.google.com/a/chromium.org/chromedriver/home)
1. Download geckodriver [here](https://github.com/mozilla/geckodriver/releases)

Get latest versions, and make sure the version downloaded is meant for your OS and is compatible (32/64 bits)

After downloading, extract both drivers and save in a directory (e.g webdrivers) in a location of choice, finally let selenium know where it can find the webdrivers by exporting the paths to the various executables, you would have to do this every time your (re)start you venv (or any enviroment for that matter)

```bash
export PATH=$PATH:/path/to/directory/of/executable/downloaded/
```

That is quite boring you can just make each of them available globally once and for all

```bash
sudo mv geckodriver /usr/local/bin
sudo mv chromedriver /usr/local/bin
```

You can also write a .bashrc script and export, to avoid the repetition this method however has thesame effect as simply moving the files to ```/usr/local/bin``` above.

Note : Be sure to export only the directory(ies) containing the executables without including the executables to avoid the "NotADirectoryError: [Errno 20] Not a directory"
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
DJANGO_SETTINGS_MODULE = systers_portal.settings.testing
6 changes: 6 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ coverage==4.4.1
django-nose==1.4.5
flake8==2.4.1
sphinx-rtd-theme==0.2.4
pylint==1.9.0
pytest==3.5.1
pytest-cov==2.5.1
pytest-django==3.2.1
pytest-pylint==0.9.0
selenium==3.12.0
87 changes: 87 additions & 0 deletions systers_portal/common/tests/selenium/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from selenium import webdriver
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from meetup.models import (Meetup, MeetupLocation, RequestMeetupLocation)
from django.contrib.auth.models import User
from django.utils import timezone
from cities_light.models import City, Country
from users.models import SystersUser
from django.conf import settings
from django.contrib.auth import (SESSION_KEY, BACKEND_SESSION_KEY,
HASH_SESSION_KEY)
from django.contrib.sessions.backends.db import SessionStore
import json
import os

browsers = {
'firefox': webdriver.Firefox,
'chrome': webdriver.Chrome,
}


class SeleniumTestCase(StaticLiveServerTestCase):
"""
A base test case for selenium, providing different helper methods.
"""

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.browser = browsers['firefox']()
cls.browser.implicitly_wait(0)

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

def setUp(self):
country = Country.objects.create(name='Bar', continent='AS')
self.location = City.objects.create(
name='Foo', display_name='Foo', country=country)
with open('{0}{1}'.format(
os.getcwd(),
'/systers_portal/common/tests/selenium/credentials.json')
) as json_data:
credentials = json.load(json_data)
self.user = User.objects.create(
username=credentials['username'], password=credentials['password'])
self.systers_user = SystersUser.objects.get(user=self.user)
self.meetup_location = MeetupLocation.objects.create(
name="Foo Systers",
slug="foo",
location=self.location,
description="It's a test location",
sponsors="BarBaz",
leader=self.systers_user)
self.meetup_location_request = RequestMeetupLocation.objects.create(
name="Bar Systers",
slug="bar",
location=self.location,
description="This is a test meetup location request",
user=self.systers_user)
self.meetup = Meetup.objects.create(
title="Test Meetup",
slug="baz",
date=timezone.now().date(),
time=timezone.now().time(),
venue="FooBar colony",
description="This is a testing meetup.",
meetup_location=self.meetup_location,
created_by=self.systers_user)

def create_session_cookie(self):
# Then create the authenticated session using the new user credentials
session = SessionStore()
session[SESSION_KEY] = self.user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = self.user.get_session_auth_hash()
session.save()

# Finally, create the cookie dictionary
cookie = {
'name': settings.SESSION_COOKIE_NAME,
'value': session.session_key,
'secure': False,
'path': '/',
}
return cookie
4 changes: 4 additions & 0 deletions systers_portal/common/tests/selenium/credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username":"foo",
"password":"foobar"
}
31 changes: 31 additions & 0 deletions systers_portal/meetup/tests/selenium/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import pytest
from selenium import webdriver

browsers = {
'firefox': webdriver.Firefox,
'chrome': webdriver.Chrome,
}


@pytest.fixture(scope="class")
def browser(request):
if 'DISPLAY' not in os.environ:
pytest.skip('Test requires display server (export DISPLAY)')

b = browsers[request.config.getoption("--browser-config")]()

request.addfinalizer(lambda *args: b.quit())

# inject class variables
request.cls.browser = b

return b


def pytest_addoption(parser):
parser.addoption(
"--browser-config",
action="store",
default="chrome",
help="firefox, chrome are allowed parameters")
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from common.tests.selenium.base import SeleniumTestCase


class TestMeetupLocationAdminPage(SeleniumTestCase):

def test_can_add_new_meetup(self):
self.browser.get(self.live_server_url)
self.browser.add_cookie(self.create_session_cookie())
self.browser.refresh()
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/add'))
self.assertTrue(True)

def test_can_delete_meetup(self):
self.browser.get(self.live_server_url)
self.browser.add_cookie(self.create_session_cookie())
self.browser.refresh()
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/foo/about'))
self.browser.find_element_by_xpath("//a[contains(text(),'Delete Meetup Location')]").click()
self.browser.find_element_by_xpath('//input[@value="Confirm"]').click()
self.assertTrue('Meetup Locations' in self.browser.title)
15 changes: 15 additions & 0 deletions systers_portal/meetup/tests/selenium/test_meetup_location_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from common.tests.selenium.base import SeleniumTestCase


class TestMeetupLocationPage(SeleniumTestCase):

def test_can_choose_location(self):
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/locations/'))
self.browser.find_element_by_xpath("//a[contains(text(),'Foo Systers')]").click()
self.assertTrue('Foo Systers' in self.browser.title)

def test_can_get_members(self):
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/foo/about'))
self.browser.find_element_by_xpath("//a[contains(text(),'Members')]").click()
text = self.browser.find_element_by_xpath("//h2[contains(text(),'Moderators')]").text
self.assertTrue(text in self.browser.page_source)
23 changes: 23 additions & 0 deletions systers_portal/meetup/tests/selenium/test_meetup_locations_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from common.tests.selenium.base import SeleniumTestCase


class TestMeetupLocationsPage(SeleniumTestCase):

def test_can_goto_meetuplocations_page(self):
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/locations/'))
self.assertTrue('Meetup Locations' in self.browser.title)

def test_can_choose_meetup_locations(self):
self.browser.get(self.live_server_url)
dropdown = self.browser.find_element_by_id('meetup-dropdown')
dropdown.click()
menu = dropdown.find_element_by_class_name('dropdown-menu')
for menu_item in menu.find_elements_by_tag_name('li'):
if menu_item.text == 'MEETUP LOCATIONS':
menu_item.click()
self.assertTrue('Meetup Locations' in self.browser.title)

def test_can_choose_location(self):
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/locations/'))
self.browser.find_element_by_xpath("//a[contains(text(),'Foo Systers')]").click()
self.assertTrue('Foo Systers' in self.browser.title)
9 changes: 9 additions & 0 deletions systers_portal/meetup/tests/selenium/test_meetup_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from common.tests.selenium.base import SeleniumTestCase


class TestMeetupPage(SeleniumTestCase):

def test_can_goto_meetup_page(self):
self.browser.get('{0}{1}'.format(self.live_server_url, '/meetup/foo/baz/'))
title = self.browser.find_element_by_id('meetup-title')
self.assertTrue(title.text == 'Test Meetup')
2 changes: 1 addition & 1 deletion systers_portal/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{% endif %}
</ul>
</li>
<li {% if '/meetup/' in request.path %}class="active"{% endif %} class="dropdown">
<li {% if '/meetup/' in request.path %}class="active"{% endif %} class="dropdown" id="meetup-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Meetups <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
{% url 'list_meetup_location' as url %}
Expand Down
2 changes: 1 addition & 1 deletion systers_portal/templates/meetup/meetup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "meetup/base.html" %}

{% block meetup_location_page_content %}
<h2>{{ meetup.title }}</h2>
<h2 id="meetup-title">{{ meetup.title }}</h2>
<div class="box-container box-body pt25">
<p>
<b> Date: </b> {{ meetup.date }}
Expand Down

0 comments on commit 8454cf5

Please sign in to comment.