def generate_page_from_yml(file) -> Page: with open(file) as f: page_data = yaml.load(f, Loader=yaml.FullLoader)['page'] url = page_data['url'] page = Page(url) elements_data = page_data['elements'] if 'elements' in page_data else [] page.elements = generate_elements(elements_data) return page
def generate_page_from_json(file) -> Page: with open(file) as file: page_data = json.load(file)['page'] url = page_data['url'] page = Page(url) json_elements = page_data['elements'] element_dict = {} for je in json_elements: locator_dict = {je['locator']: je['value']} name = je['name'] element_dict[name] = locator_dict page.elements = generate_elements(element_dict) return page
def test_script_execution(self): page = Page('https://demoqa.com/accordian/') actions = Action() actions.go(page) click_header_script = 'document.getElementById("section2Heading").click()' div_class_name = 'return document.getElementById("section2Content").parentElement.className' self.assertEqual(actions.execute_script(div_class_name), 'collapse') actions.execute_script(click_header_script) self.assertEqual(actions.execute_script(div_class_name), 'collapsing')
def test_dropdown(self): page = Page('http://the-internet.herokuapp.com/dropdown') actions = Action() actions.go(page) dropdown = PageElement(('id', 'dropdown'), 'dropdown') option_2 = PageElement( ('xpath', '/html/body/div[2]/div/div/select/option[3]'), 'option 2') actions.select_drop_down(dropdown, 'Option 2') is_selected = actions.get_attribute(option_2, 'selected') self.assertTrue(is_selected)
def test_saucedemo_refresh_page(self): page = Page('https://www.saucedemo.com/inventory.html') actions = Action() actions.go(page) page.elements = [ PageElement(('id', 'user-name'), 'username field'), PageElement(('id', 'password'), 'password field'), PageElement(('id', 'login-button'), 'login button'), PageElement(("id", "add-to-cart-sauce-labs-backpack"), "button 1"), PageElement(("xpath", '//*[@id="item_4_title_link"]/div'), "inventory item"), PageElement(("class", "shopping_cart_badge"), "shopping cart badge"), PageElement(("class", "inventory_item_name"), "item name") ] login = Steps( actions, [('type', page.get_element('username field'), 'standard_user'), ('type', page.get_element('password field'), 'secret_sauce'), ('click', page.get_element('login button'))]) Do(login) actions.click(page.get_element("button 1")) item_name = actions.get_attribute(page.get_element("inventory item"), "innerHTML") items_in_cart = actions.get_attribute( page.get_element("shopping cart badge"), "innerHTML") self.assertEqual(items_in_cart, "1") actions.refresh() items_in_cart = actions.get_attribute( page.get_element("shopping cart badge"), "innerHTML") self.assertEqual(items_in_cart, "1") actions.go_to('https://www.saucedemo.com/cart.html') inventory_item_name = actions.get_attribute( page.get_element("item name"), "innerHTML") self.assertEqual(item_name, inventory_item_name) actions.close()
def test_javascript_confirm(self): page = Page('https://the-internet.herokuapp.com/javascript_alerts') actions = Action() actions.go(page) to_be_clicked = PageElement(('xpath', "(//button)[2]")) actions.click(to_be_clicked) alert = AlertElement() js_text = actions.get_text(alert) self.assertEqual(js_text, "I am a JS Confirm") actions.dismiss(alert) actions.close()
def test_file_upload(self): import os # Need this to work locally and in remote CI base_path = os.getcwd() if 'tests' in base_path: pass else: base_path += '/tests' page = Page('http://the-internet.herokuapp.com/upload') actions = Action() actions.go(page) page.elements = [ PageElement(('id', 'file-upload'), 'Upload Element'), PageElement(('id', 'file-submit'), 'Submit Button') ] file_path = base_path + '/fixtures/files/upload_text_file.txt' actions.upload_file(page.get_element('Upload Element'), file_path) actions.click(page.get_element('Submit Button')) success_text = 'File Uploaded!' self.assertIn(success_text, actions.get_page_source())
def test_javascript_prompt(self): page = Page('https://the-internet.herokuapp.com/javascript_alerts') actions = Action() actions.go(page) page.elements = [ PageElement(("xpath", "(//button)[3]"), "prompt button"), PageElement(("id", "result"), "result"), AlertElement('alert') ] actions.click(page.get_element("prompt button")) alert_text = actions.get_text(page.get_element('alert')) self.assertEqual(alert_text, "I am a JS prompt") actions.input_text(page.get_element('alert'), 'This is a test') actions.accept(page.get_element('alert')) result = actions.get_attribute(page.get_element('result'), 'innerHTML') self.assertEqual(result, "You entered: This is a test") actions.close()
class TestPage(unittest.TestCase): page = Page('www.example.com') element = PageElement(Locator('id', 'test'), 'test') def test_basic_page(self): self.assertEqual(self.page.url, 'www.example.com') def test_init_with_elements(self): p = Page('www.example.com', [self.element]) self.assertEqual(p.url, 'www.example.com') self.assertEqual(len(p.elements), 1) def test_add_element(self): self.assertEqual(len(self.page.elements), 0) self.page.elements.append(self.element) self.assertEqual(len(self.page.elements), 1) def test_get_element_from_page(self): p = Page('www.example.com', [self.element]) element_by_index = p.elements[0] self.assertEqual(element_by_index, self.element) element_by_name = [elem for elem in p.elements if elem.name == 'test'][0] self.assertEqual(element_by_name, self.element)
class TestBrowsers(unittest.TestCase): page = Page('http://erikwhiting.com/newsOutlet') actions = None import os running_in_travis = os.getenv('RUNNING_IN_CI') def _set_up(self, browser): if self.running_in_travis and browser.lower() == 'edge': import shutil options_hash = BrowserOptions({ 'driver_type': browser, 'headless': True, 'browser_binary_location': shutil.which('microsoft-edge-dev'), 'webdriver_location': shutil.which('msedgedriver'), 'operating_system': 'LINUX' }) else: options_hash = BrowserOptions({'driver_type': browser}) self.actions = Action(options_hash) self.actions.go(self.page) def tearDown(self): self.actions.close() del self.actions def test_page_source_feature_chrome(self): self._set_up('Chrome') source = self.actions.get_page_source() self.assertIn('<body onload="defaultBreaking()">', source) def test_page_refresh_chrome(self): self._set_up('Chrome') self.actions.refresh() current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) def test_page_change_url_chrome(self): self._set_up('Chrome') current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) self.actions.go_to('https://github.com') current_url = self.actions.get_url() self.assertEqual('https://github.com/', current_url) def test_page_source_feature_firefox(self): self._set_up('Firefox') source = self.actions.get_page_source() self.assertIn('<body onload="defaultBreaking()">', source) def test_page_refresh_firefox(self): self._set_up('Firefox') self.actions.refresh() current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) def test_page_change_url_firefox(self): self._set_up('Firefox') current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) self.actions.go_to('https://github.com') current_url = self.actions.get_url() self.assertEqual('https://github.com/', current_url) def test_page_source_feature_edge(self): self._set_up('Edge') source = self.actions.get_page_source() self.assertIn('<body onload="defaultBreaking()">', source) def test_page_refresh_edge(self): self._set_up('Edge') self.actions.refresh() current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) def test_page_change_url_edge(self): self._set_up('Edge') current_url = self.actions.get_url() self.assertEqual('http://erikwhiting.com/newsOutlet/', current_url) self.actions.go_to('https://github.com') current_url = self.actions.get_url() self.assertEqual('https://github.com/', current_url)
def test_get_element_from_page(self): p = Page('www.example.com', [self.element]) element_by_index = p.elements[0] self.assertEqual(element_by_index, self.element) element_by_name = [elem for elem in p.elements if elem.name == 'test'][0] self.assertEqual(element_by_name, self.element)
def test_init_with_elements(self): p = Page('www.example.com', [self.element]) self.assertEqual(p.url, 'www.example.com') self.assertEqual(len(p.elements), 1)