Best Python code snippet using toolium_python
test_derived_page_element.py
Source:test_derived_page_element.py
...22from toolium.pageelements import select_page_element23from toolium.pageobjects.page_object import PageObject24child_element = 'child_element'25mock_element = None26def get_mock_select():27 # Mock text property28 mock_prop_text = mock.PropertyMock(return_value='option value')29 mock_text = mock.MagicMock()30 type(mock_text).text = mock_prop_text31 # Mock first_selected_option property32 mock_prop_option = mock.PropertyMock(return_value=mock_text)33 mock_option = mock.MagicMock()34 type(mock_option).first_selected_option = mock_prop_option35 # Mock select36 mock_select = mock.MagicMock(return_value=mock_option)37 return mock_select38class Menu(Group):39 logo = PageElement(By.ID, 'image')40class LoginPageObject(PageObject):41 title = Text(By.ID, 'title')42 username = InputText(By.XPATH, '//input[0]')43 password = InputText(By.ID, 'password')44 language = Select(By.ID, 'language')45 login = Button(By.ID, 'login')46 menu = Menu(By.ID, 'menu')47 username_shadowroot = InputText(By.XPATH, '//input[1]', shadowroot='shadowroot_css')48@pytest.fixture49def driver_wrapper():50 # Create a mock element51 global mock_element52 mock_element = mock.MagicMock(spec=WebElement)53 mock_element.find_element.return_value = child_element54 mock_element.text = 'text value'55 mock_element.get_attribute.return_value = 'input text value'56 # Reset wrappers pool values57 DriverWrappersPool._empty_pool()58 DriverWrapper.config_properties_filenames = None59 # Create a new wrapper60 driver_wrapper = DriverWrappersPool.get_default_wrapper()61 driver_wrapper.driver = mock.MagicMock()62 driver_wrapper.is_mobile_test = mock.MagicMock(return_value=False)63 return driver_wrapper64def test_locator(driver_wrapper):65 page_object = LoginPageObject(driver_wrapper)66 assert page_object.title.locator == (By.ID, 'title')67 assert page_object.username.locator == (By.XPATH, '//input[0]')68 assert page_object.password.locator == (By.ID, 'password')69 assert page_object.language.locator == (By.ID, 'language')70 assert page_object.login.locator == (By.ID, 'login')71 assert page_object.menu.locator == (By.ID, 'menu')72 assert page_object.menu.logo.locator == (By.ID, 'image')73 # Check that elements inside a group have the group as parent74 assert page_object.menu.logo.parent == page_object.menu75def test_get_text(driver_wrapper):76 driver_wrapper.driver.find_element.return_value = mock_element77 title_value = LoginPageObject().title.text78 assert title_value == 'text value'79def test_get_input_text(driver_wrapper):80 driver_wrapper.driver.find_element.return_value = mock_element81 username_value = LoginPageObject().username.text82 assert username_value == 'input text value'83def test_set_input_text(driver_wrapper):84 # Configure driver mock85 driver_wrapper.driver.find_element.return_value = mock_element86 driver_wrapper.is_ios_test = mock.MagicMock(return_value=False)87 LoginPageObject().username.text = 'new input value'88 mock_element.send_keys.assert_called_once_with('new input value')89def test_set_input_text_shadowroot(driver_wrapper):90 # Configure driver mock91 driver_wrapper.driver.find_element.return_value = mock_element92 driver_wrapper.is_ios_test = mock.MagicMock(return_value=False)93 text_value = 'new input value'94 expected_script = 'return document.querySelector("shadowroot_css").shadowRoot.querySelector("//input[1]").value ' \95 '= "new input value"'96 LoginPageObject().username_shadowroot.text = text_value97 mock_element.send_keys.assert_not_called()98 driver_wrapper.driver.execute_script.assert_called_once_with(expected_script)99def test_set_input_text_shadowroot_quotation_marks(driver_wrapper):100 # Configure driver mock101 driver_wrapper.driver.find_element.return_value = mock_element102 driver_wrapper.is_ios_test = mock.MagicMock(return_value=False)103 text_value = 'new "input" value'104 expected_script = 'return document.querySelector("shadowroot_css").shadowRoot.querySelector("//input[1]").value ' \105 '= "new \\"input\\" value"'106 LoginPageObject().username_shadowroot.text = text_value107 mock_element.send_keys.assert_not_called()108 driver_wrapper.driver.execute_script.assert_called_once_with(expected_script)109def test_get_selected_option(driver_wrapper):110 select_page_element.SeleniumSelect = get_mock_select()111 option = LoginPageObject().language.option112 assert option == 'option value'113def test_set_option(driver_wrapper):114 driver_wrapper.driver.find_element.return_value = mock_element115 select_page_element.SeleniumSelect = get_mock_select()116 LoginPageObject().language.option = 'new option value'117 select_page_element.SeleniumSelect.assert_called_once_with(mock_element)118 select_page_element.SeleniumSelect().select_by_visible_text.assert_called_once_with('new option value')119def test_click_button(driver_wrapper):120 driver_wrapper.driver.find_element.return_value = mock_element121 LoginPageObject().login.click()122 mock_element.click.assert_called_once_with()123def test_group_reset_object(driver_wrapper):124 login_page = LoginPageObject()125 # Check that web elements are empty126 assert login_page.menu._web_element is None127 assert login_page.menu.logo._web_element is None128 assert login_page.menu.logo.parent == login_page.menu129 login_page.menu.logo.web_element...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!