import os
import time

import pytest
from selenium import webdriver


BASE_URL = 'http://localhost:5000'


@pytest.fixture(scope='function')
def driver(request):
    profile = webdriver.FirefoxProfile()
    profile.add_extension(
        os.path.join(os.path.dirname(__file__), 'selenium/JSErrorCollector.xpi'),
    )

    driver = webdriver.Firefox(firefox_profile=profile)
    driver.implicitly_wait(5)

    def close_driver():
        # Let's check for errors
        errors = driver.execute_script('return window.JSErrorCollector_errors.pump()')
        driver.close()

        if errors:
            raise Exception(errors)

    request.addfinalizer(close_driver)

    return driver


class TestDrippies(object):
    def test_index(self, driver):
        """The index page should return a response."""
        driver.get(BASE_URL + '/')

        assert 'Drippies' in driver.title

    def test_shows_weather_for_location(self, driver):
        """Entering a location and submitting should show the weather."""
        # Greg visits the index page
        driver.get(BASE_URL + '/')

        # And sees a big ol' text input
        field = driver.find_element_by_id('location')

        # He types in the name of his town
        field.send_keys('NYC')

        # Submits the form
        field.submit()

        # Waits patiently
        time.sleep(3)

        # The text field is still there, prefilled with the name of his
        # town
        field = driver.find_element_by_id('location')
        assert field.get_attribute('value') == 'NYC'

        # There's also a nice blurb about the weather
        weather = driver.find_element_by_id('weather')
        assert len(weather.text) > 0

@@ -707,24 +707,459 @@
 es(object):%0A
+    def submit_location(self, driver, location):%0A        %22%22%22Submits a location to the location form.%22%22%22%0A        field = driver.find_element_by_id('location')%0A%0A        field.send_keys(location)%0A        field.submit()%0A%0A        time.sleep(3)%0A%0A    def assert_shows_the_weather(self, driver):%0A        # There's also a nice blurb about the weather%0A        weather = driver.find_element_by_id('weather')%0A        assert len(weather.text) %3E 0%0A%0A
     def test
@@ -1254,38 +1254,32 @@
 ver.get(BASE_URL
- + '/'
 )%0A%0A        asser
@@ -1505,14 +1505,8 @@
 _URL
- + '/'
 )%0A%0A 
@@ -1518,114 +1518,18 @@
   # 
-And sees a big ol' text input%0A        field = driver.find_element_by_id('location')%0A%0A        # He types in
+He submits
  the
@@ -1558,129 +1558,42 @@
     
-field.send_keys('NYC')%0A%0A        # Submits the form%0A        field.submit()%0A%0A        # Waits patiently%0A        time.sleep(3
+self.submit_location(driver, 'NYC'
 )%0A%0A 
@@ -1798,142 +1798,42 @@
     
-# There's also a nice blurb about the weather%0A        
+self.assert_shows_the_
 weather
- = 
+(
 driver
-.find_element_by_id('weather')%0A        assert len(weather.text) %3E 0
+)
 %0A

Refactor functional guys