import unittest from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By class TestScenarioA(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.base_url = "http://localhost:3000" self.title_xpath = "//li[last()][contains(concat(' ',normalize-space(@class),' '),' media ')]/div/h5" self.form_xpath = "//form" self.workshop_url = self.base_url + "/workshop" def add_workshop(self, driver, title, description): driver.get(self.workshop_url) # get form elements ws = driver.find_element(by=By.ID, value="name") desc = driver.find_element(by=By.ID, value="description") button = driver.find_element(by=By.CSS_SELECTOR, value="button[type='submit']") ws.send_keys(title) desc.send_keys(description) button.click() def is_element_exist(self, selector, element): try: self.driver.find_element(by=selector, value=element) except NoSuchElementException as e: return False return True def test_A_1(self): driver = self.driver driver.get(self.base_url) button = driver.find_element(by=By.CLASS_NAME, value="btn") button.click() self.assertNotIn(driver.title, "Error") self.assertTrue(self.is_element_exist(By.XPATH, self.form_xpath)) self.assertTrue(self.is_element_exist(By.ID, "name")) self.assertTrue(self.is_element_exist(By.ID, "description")) def test_A_2(self): driver = self.driver driver.get(self.workshop_url) button = driver.find_element(by=By.LINK_TEXT, value="Cancel") button.click() self.assertEqual(driver.current_url, self.base_url + '/') def test_A_3(self): self.add_workshop(self.driver, "My workshop", "") self.assertNotEqual(self.driver.current_url, self.base_url) def test_A_4(self): self.add_workshop(self.driver, "My workshop", "") self.assertNotEqual(self.driver.current_url, self.base_url) def test_A_5(self): self.add_workshop(self.driver, "My workshop", "This is a test") ws_title = self.driver.find_elements(by=By.XPATH, value=self.title_xpath) self.assertEqual(ws_title[0].text, "My workshop") self.add_workshop(self.driver, "second workshop", "This is a test") ws_title = self.driver.find_elements(by=By.XPATH, value=self.title_xpath) self.assertEqual(ws_title[0].text, "second workshop") def tearDown(self): self.driver.close() if __name__ == '__main__': unittest.main()