from selenium.webdriver.common.by import By from nicegui import ui from .screen import Screen def test_classes(screen: Screen): label = ui.label('Some label') def assert_classes(classes: str) -> None: assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@class)="{classes}" and text()="Some label"]') screen.open('/') screen.wait(0.5) assert_classes('') label.classes('one') assert_classes('one') label.classes('one') assert_classes('one') label.classes('two three') assert_classes('one two three') label.classes(remove='two') assert_classes('one three') label.classes(replace='four') assert_classes('four') def test_style_parsing(): # pylint: disable=protected-access assert ui.element._parse_style(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison assert ui.element._parse_style('color: red; background-color: blue') == {'color': 'red', 'background-color': 'blue'} assert ui.element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'} assert ui.element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'} assert ui.element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'} def test_props_parsing(): # pylint: disable=protected-access assert ui.element._parse_props(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison assert ui.element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'} assert ui.element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'} assert ui.element._parse_props('size=50%') == {'size': '50%'} assert ui.element._parse_props('href=http://192.168.42.100/') == {'href': 'http://192.168.42.100/'} assert ui.element._parse_props('hint="Your \\"given\\" name"') == {'hint': 'Your "given" name'} assert ui.element._parse_props('input-style="{ color: #ff0000 }"') == {'input-style': '{ color: #ff0000 }'} def test_style(screen: Screen): label = ui.label('Some label') def assert_style(style: str) -> None: assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]') screen.open('/') screen.wait(0.5) assert_style('') label.style('color: red') assert_style('color: red;') label.style('color: red') assert_style('color: red;') label.style('color: blue') assert_style('color: blue;') label.style('font-weight: bold') assert_style('color: blue; font-weight: bold;') label.style(remove='color: blue') assert_style('font-weight: bold;') label.style(replace='text-decoration: underline') assert_style('text-decoration: underline;') label.style('color: blue;') assert_style('text-decoration: underline; color: blue;') def test_props(screen: Screen): input_ = ui.input() def assert_props(*props: str) -> None: class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props] assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]') screen.open('/') screen.wait(0.5) assert_props('standard') input_.props('dark') assert_props('standard', 'dark') input_.props('dark') assert_props('standard', 'dark') input_.props(remove='dark') assert_props('standard') def test_move(screen: Screen): with ui.card() as a: ui.label('A') x = ui.label('X') with ui.card() as b: ui.label('B') ui.button('Move X to A', on_click=lambda: x.move(a)) ui.button('Move X to B', on_click=lambda: x.move(b)) ui.button('Move X to top', on_click=lambda: x.move(target_index=0)) screen.open('/') assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y'] screen.click('Move X to B') screen.wait(0.5) assert screen.find('A').location['y'] < screen.find('B').location['y'] < screen.find('X').location['y'] screen.click('Move X to A') screen.wait(0.5) assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y'] screen.click('Move X to top') screen.wait(0.5) assert screen.find('X').location['y'] < screen.find('A').location['y'] < screen.find('B').location['y'] def test_xss(screen: Screen): ui.label('') ui.label('Bold 1, `code`, copy&paste, multi\nline') ui.button('Button', on_click=lambda: ( ui.label(''), ui.label('Bold 2, `code`, copy&paste, multi\nline'), )) screen.open('/') screen.click('Button') screen.should_contain('') screen.should_contain('') screen.should_contain('Bold 1, `code`, copy&paste, multi\nline') screen.should_contain('Bold 2, `code`, copy&paste, multi\nline') def test_default_props(): ui.button.default_props('rounded outline') button_a = ui.button('Button A') button_b = ui.button('Button B') assert button_a._props.get('rounded') is True, 'default props are set' assert button_a._props.get('outline') is True assert button_b._props.get('rounded') is True assert button_b._props.get('outline') is True ui.button.default_props(remove='outline') button_c = ui.button('Button C') assert button_c._props.get('outline') is None, '"outline" prop was removed' assert button_c._props.get('rounded') is True, 'other props are still there' ui.input.default_props('filled') input_a = ui.input() assert input_a._props.get('filled') is True assert input_a._props.get('rounded') is None, 'default props of ui.button do not affect ui.input' class MyButton(ui.button): pass MyButton.default_props('flat') button_d = MyButton() button_e = ui.button() assert button_d._props.get('flat') is True assert button_d._props.get('rounded') is True, 'default props are inherited' assert button_e._props.get('flat') is None, 'default props of MyButton do not affect ui.button' assert button_e._props.get('rounded') is True