WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Python and selenium driver to test Harvard NPC

Submitted by barnettech on Mon, 08/06/2018 - 13:53

http://selenium-python.readthedocs.io/locating-elements.html

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

class PythonOrgSearch(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox()

def test_search_in_python_org(self):
driver = self.driver
driver.get("https://qa.college.harvard.edu/financial-aid/net-price-calculator")
self.assertIn("Calculator", driver.title)
elem = driver.find_element_by_class_name("modal-close")
elem.click()
elem = driver.find_element_by_class_name("output")
self.assertEqual(str(elem.text), "$0")
elem = driver.find_element_by_name('citizenship')
for option in elem.find_elements_by_tag_name('option'):
if option.text == 'USA':
option.click() # select() in earlier versions of webdriver
break
elem = driver.find_element_by_class_name("output")
self.assertEqual(str(elem.text), "$67,050")
elem = driver.find_element_by_name('residence')
for option in elem.find_elements_by_tag_name('option'):
if option.text == 'MA':
option.click() # select() in earlier versions of webdriver
break
time.sleep(1)
elem = driver.find_element_by_name('numbercollege')
for option in elem.find_elements_by_tag_name('option'):
if option.text == '2':
option.click() # select() in earlier versions of webdriver
break
elem = driver.find_element_by_name('numberfamily')
for option in elem.find_elements_by_tag_name('option'):
if option.text == '4':
option.click() # select() in earlier versions of webdriver
break
elem = driver.find_element_by_name('salary')
for option in elem.find_elements_by_tag_name('option'):
if option.text == '$100,000':
option.click() # select() in earlier versions of webdriver
break
elem = driver.find_element_by_class_name("output")
time.sleep(1)
self.assertEqual(str(elem.text), "$63,250")

#elem = driver.find_element_by_name("q")
#elem.send_keys("pycon")
#elem.send_keys(Keys.RETURN)
#assert "No results found." not in driver.page_source

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

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