#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pytest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


'''
Prior to running this module, set the environment variable "URL" first. Set the value to
"https://start.engagespark.com/sign-in/" for the Login test. For the Signup test, set the value to
"https://start.engagespark.com/sign-up/"
'''


class Base:

    @pytest.fixture(autouse=True)
    def browser_setup_and_teardown(self):
        self.browser = webdriver.Firefox()
        self.browser.maximize_window()
        self.wait = WebDriverWait(self.browser, 30)
        yield
        self.browser.close()
        self.browser.quit()

    @pytest.fixture()
    def open_url(self):
        self.browser.get(os.getenv("URL"))
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "form")))

    def enter_credentials(self, input_field, credential):
        self.browser.find_element_by_name(input_field).click()
        self.browser.find_element_by_name(input_field).send_keys(credential)


class TestSignup(Base):

    @pytest.mark.skipif("sign-up" not in os.getenv("URL"), reason="url provided is incorrect")
    @pytest.mark.usefixtures("open_url")
    @pytest.mark.parametrize("email, password", [
        ("sample_email@blah.org", "password1"),
        ("not_another_email@blah.org", "password2"),
    ])
    def test_signup_without_captcha(self, email, password):
        print("Setting email for sign up")
        self.enter_credentials(input_field="email", credential=email)
        print("Setting password for sign up")
        self.enter_credentials(input_field="password1", credential=password)
        self.browser.find_element_by_css_selector("input[type='checkbox']").click()
        assert not self.browser.find_element_by_css_selector("#signupform button").is_enabled()


class TestLogin(Base):

    @pytest.mark.skipif("sign-in" not in os.getenv("URL"), reason="url provided is incorrect")
    @pytest.mark.usefixtures("open_url")
    @pytest.mark.parametrize("email, password", [
        ("ghost_email_woo@blah.org", "password1"),
        ("my_imaginary_email@blah.org", "password2"),
        pytest.mark.xfail(
            (os.getenv("VALID_EMAIL"), os.getenv("VALID_PASSWORD")))
    ])
    def test_login_with_inexistent_accounts(self, email, password):
        print("Setting email for sign up")
        self.enter_credentials(input_field="login", credential=email)
        print("Setting password for sign up")
        self.enter_credentials(input_field="password", credential=password)
        self.browser.find_element_by_css_selector("#loginform button").click()
        print("Error notification should appear")
        self.wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, "notification-message"), "The e-mail address and/or password you specified are not correct."))

@@ -262,20 +262,18 @@
 rWait%0A%0A%0A
-'''%0A
+# 
 Prior to
@@ -335,16 +335,18 @@
 %22 first.
+%0A#
  Set the
@@ -346,33 +346,33 @@
 Set the value to
-%0A
+ 
 %22https://start.e
@@ -415,16 +415,18 @@
 in test.
+%0A#
  For the
@@ -459,10 +459,9 @@
 e to
-%0A%22
+ 
 http
@@ -499,12 +499,9 @@
 up/%22
-%0A'''
+.
 %0A%0A%0Ac
@@ -512,16 +512,89 @@
  Base:%0A%0A
+    # This fixture contains the set up and tear down code for each test.%0A
     @pyt
@@ -867,16 +867,140 @@
 quit()%0A%0A
+    # This fixture opens the URL set in the environment variable and waits%0A    # until a form is visible before proceeding.%0A
     @pyt
@@ -1399,296 +1399,203 @@
     
-@pytest.mark.skipif(%22sign-up%22 not in os.getenv(%22URL%22), reason=%22url provided is incorrect%22)%0A    @pytest.mark.usefixtures(%22open_url%22)%0A    @pytest.mark.parametrize(%22email, password%22, %5B%0A        (%22sample_email@blah.org%22, %22password1%22),%0A        (%22not_another_email@blah.org%22, %22password2%22),%0A    %5D
+# When this test runs, it will use the 'browser_setup_and_teardown' fixture first, followed by%0A    # the 'open_url' fixture before running the test itself.%0A    @pytest.mark.usefixtures(%22open_url%22
 )%0A  
@@ -1632,33 +1632,16 @@
 cha(self
-, email, password
 ):%0A     
@@ -1733,37 +1733,55 @@
 il%22, credential=
-email
+%22sample_email@blah.org%22
 )%0A        print(
@@ -1875,32 +1875,33 @@
  credential=
+%22
 password
 )%0A        se
@@ -1880,32 +1880,34 @@
 ential=%22password
+1%22
 )%0A        self.b
@@ -2106,303 +2106,169 @@
     
-@pytest.mark.skipif(%22sign-in%22 not in os.getenv(%22URL%22), reason=%22url provided is incorrect%22)%0A    @pytest.mark.usefixtures(%22open_url%22)%0A    @pytest.mark.parametrize(%22email, password%22, %5B%0A        (%22ghost_email_woo@blah.org%22, %22password1%22),%0A        (%22my_imaginary_email@blah.org%22, %22password2%22),
+# When this test runs, it will use the 'browser_setup_and_teardown' fixture first, followed by%0A    # the 'open_url' fixture before running the test itself.
 %0A    
-    
+@
 pyte
@@ -2279,89 +2279,30 @@
 ark.
-xfail(%0A            (os.getenv(%22VALID_EMAIL%22), os.getenv(%22VALID_PASSWORD%22)))%0A    %5D
+usefixtures(%22open_url%22
 )%0A  
@@ -2351,25 +2351,8 @@
 self
-, email, password
 ):%0A 
@@ -2456,21 +2456,50 @@
 dential=
-email
+%22another_imaginary_email@blah.org%22
 )%0A      
@@ -2604,24 +2604,25 @@
 dential=
+%22
 password
 )%0A      
@@ -2613,16 +2613,18 @@
 password
+2%22
 )%0A      

add comments to explain tests