test_signup.py 3.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# This test will only pass if the site and secret keys are set to 
# RECAPTCHA_SITE_KEY = 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
# RECAPTCHA_SECRET_KEY = 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
# in agreelance/settings.py. See recaptcha docs for more information:
# https://developers.google.com/recaptcha/docs/faq

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

import time
import os
import random 
import string
from time import sleep

url = "http://127.0.0.1:8000/user/signup"

class AgreelanceSignUp(unittest.TestCase):

    def setUp(self):
        if os.name == "nt":
            self.driver = webdriver.Chrome("./bin/chromedriver.exe")
        else:
            self.driver = webdriver.Chrome("./bin/chromedriver")

    # Input random valid info in form
    def test_fill_form_valid(self):
        driver = self.driver
        driver.get(url)

        username = driver.find_element_by_id("id_username")
        firstname = driver.find_element_by_id("id_first_name")
        lastname = driver.find_element_by_id("id_last_name")
        categories = Select(driver.find_element_by_id("id_categories"))
        company = driver.find_element_by_id("id_company")
        email = driver.find_element_by_id("id_email")
        email_confirmation = driver.find_element_by_id("id_email_confirmation")
        password = driver.find_element_by_id("id_password1")
        password_confirmation = driver.find_element_by_id("id_password2")
        phone_number = driver.find_element_by_id("id_phone_number")
        country = driver.find_element_by_id("id_country")
        state = driver.find_element_by_id("id_state")
        city = driver.find_element_by_id("id_city")
        postal_code = driver.find_element_by_id("id_postal_code")
        street_address = driver.find_element_by_id("id_street_address")
        
        recaptcha = driver.find_element_by_class_name("g-recaptcha")
        submit = driver.find_element_by_xpath("/html/body/div[1]/form/button")

        username.send_keys(self.rString(30))
        firstname.send_keys(self.rString(30))
        lastname.send_keys(self.rString(30))
        categories.select_by_index(0)
        company.send_keys(self.rString(30))

        email_ = self.rString(10) + "@" + self.rString(10) + ".com"
        email.send_keys(email_)
        email_confirmation.send_keys(email_)

        password_ = self.rString(30)
        password.send_keys(password_)
        password_confirmation.send_keys(password_)

        phone_number.send_keys(self.rDigits(8))
        country.send_keys(self.rString(30))
        state.send_keys(self.rString(30))
        city.send_keys(self.rString(30))
        postal_code.send_keys(self.rDigits(4))
        street_address.send_keys(self.rString(30))


        recaptcha.click()

        submit.click()
        
        timeout = 5
        try:
            element_present = EC.presence_of_element_located((By.CLASS_NAME, 'alert'))
            WebDriverWait(driver, timeout).until(element_present)
        except TimeoutException:
            print("Timed out waiting for page to load")
        
        alert = driver.find_element_by_class_name("alert")
        self.assertTrue("Your account has been created and is awaiting verification" in alert.text)


        
    def tearDown(self):
        self.driver.close()

    @staticmethod
    def rString(length):
        return ''.join(random.choices(string.ascii_lowercase, k=length))

    @staticmethod
    def rDigits(length):
        return ''.join(random.choices(string.digits, k=length))

if __name__ == "__main__":
    unittest.main()