Esempio n. 1
0
def test_execute_form_fill_with_one_settable_widget_returns_true(
        runner, form_expert):
    # Arrange
    actionable_state = ActionableState()
    widget = {
        'actions': ['set'],
        'label': 'widget_label',
        'key': 'widget_key',
        'selector': 'widget_selector'
    }
    actionable_state.add_widget(widget)
    form_fill_strategy = FillEntireForm(form_expert)

    # Act
    result = form_fill_strategy.execute(runner, actionable_state)

    # Assert
    assert result is True
Esempio n. 2
0
def test_execute_form_fill_with_one_non_settable_widget_does_not_perform_action(
        runner, form_expert):
    # Arrange
    actionable_state = ActionableState()
    widget = {
        'actions': [],
        'label': 'widget_label',
        'key': 'widget_key',
        'selector': 'widget_selector'
    }
    actionable_state.add_widget(widget)
    form_fill_strategy = FillEntireForm(form_expert)

    # Act
    form_fill_strategy.execute(runner, actionable_state)

    # Assert
    runner.perform_action.assert_not_called()
Esempio n. 3
0
def test_execute_form_fill_with_two_settable_widgets_calls_runner_perform_action_twice(
        runner, form_expert):
    # Arrange
    actionable_state = ActionableState()
    widget = {
        'actions': ['set'],
        'label': 'widget_label',
        'key': 'widget_key',
        'selector': 'widget_selector'
    }
    actionable_state.add_widget(widget)
    actionable_state.add_widget(widget)
    form_fill_strategy = FillEntireForm(form_expert)

    # Act
    form_fill_strategy.execute(runner, actionable_state)

    # Assert
    assert runner.perform_action.call_count == 2
Esempio n. 4
0
def test_execute_form_fill_returns_false_when_perform_action_fails(
        runner, form_expert):
    # Arrange
    actionable_state = ActionableState()
    widget = {
        'actions': ['set'],
        'label': 'widget_label',
        'key': 'widget_key',
        'selector': 'widget_selector'
    }
    actionable_state.add_widget(widget)
    runner.perform_action.return_value = False
    form_fill_strategy = FillEntireForm(form_expert)

    # Act
    result = form_fill_strategy.execute(runner, actionable_state)

    # Assert
    assert result is False
Esempio n. 5
0
def test_execute_form_fill_with_no_actionable_widgets_returns_true(
        runner, form_expert):
    # Arrange
    actionable_state = ActionableState()
    form_fill_strategy = FillEntireForm(form_expert)

    # Act
    result = form_fill_strategy.execute(runner, actionable_state)

    # Assert
    assert result is True
Esempio n. 6
0
def test_get_all_widgets():
    # Arrange
    actionable_state = ActionableState()
    actionable_state.widgets = [
        {
            'key': 'key_1',
            'label': 'label_1',
            'actions': ['set']
        },
        {
            'key': 'key_2',
            'label': 'label_2',
            'actions': []
        }
    ]
    actionable_state.static_widgets = [
        {
            'key': 'key_3',
            'label': 'label_3',
            'actions': []
        },
        {
            'key': 'key_4',
            'label': 'label_4',
            'actions': ['set']
        }
    ]

    # Act
    result = actionable_state.get_all_widgets()

    # Assert
    assert len(result) == 4
    assert result[0]['key'] == 'key_1'
    assert result[1]['key'] == 'key_2'
    assert result[2]['key'] == 'key_3'
    assert result[3]['key'] == 'key_4'
Esempio n. 7
0
def test_calculate_hash(frozenset_mock, hash_mock):
    # Arrange
    actionable_state = ActionableState()
    actionable_state.widgets = [
        {
            'key': 'key_1',
            'label': 'label_1',
            'actions': ['']
        },
        {
            'key': 'key_2',
            'label': 'label_2',
            'actions': []
        }
    ]
    actionable_state.static_widgets = [
        {
            'key': 'key_3',
            'label': 'label_3',
            'actions': []
        },
        {
            'key': 'key_4',
            'label': 'label_4',
            'actions': ['set']
        }
    ]
    frozenset_mock_return_value = Mock()
    frozenset_mock.return_value = frozenset_mock_return_value
    hash_mock_return_value = Mock()
    hash_mock.return_value = hash_mock_return_value

    # Act
    actionable_state.calculate_hash()

    # Assert
    assert actionable_state.hash == hash_mock_return_value
    assert 'key_1' in frozenset_mock.call_args[0][0]
    assert 'key_2' in frozenset_mock.call_args[0][0]
    hash_mock.assert_called_with(frozenset_mock_return_value)
Esempio n. 8
0
def test_loop_iteration_no_generated_test_flow_but_flows_in_queue():
    # Arrange.
    sut_url = "TEST"
    runner_url = "TEST"
    form_expert_client = Mock()
    runner_client = Mock()
    page_analysis_client = Mock()
    flow_generator_client = Mock()

    page_analysis = {
        'analysis': {
            'labelCandidates': ['Label_FirstName'],
            'COMMIT': ['C1'],
            'ERRORMESSAGE': ['E1'],
            'errorMessages': ['E1']
        }
    }

    page_analysis_client.run_analysis.return_value = page_analysis

    widget_first_name_label = {
        'key': 'Label_FirstName',
        'label': 'FirstNameLabel',
        'actions': [],
        'selector': '#lblFirstName',
        'properties': {
            'tagName': 'LABEL',
            'text': 'First Name',
            'x': 10,
            'y': 10
        }
    }

    widget_first_name = {
        'key': 'FIRSTNAME',
        'label': 'FirstName',
        'actions': ['set'],
        'selector': '#firstName',
        'properties': {
            'tagName': 'INPUT',
            'x': 20,
            'y': 20
        }
    }

    widget_save = {
        'key': 'C1',
        'label': 'Save',
        'actions': ['click'],
        'selector': '#save',
        'properties': {
            'tagName': 'BUTTON',
            'x': 40,
            'y': 40
        }
    }

    widget_error = {
        'key': 'E1',
        'label': 'Error',
        'properties': {
            'tagName': 'LABEL',
            'x': 60,
            'y': 60
        }
    }

    target_concrete_state = {
        'widgets': {
            'Label_FirstName': widget_first_name_label,
            'FIRSTNAME': widget_first_name,
            'C1': widget_save,
            'E1': widget_error
        }
    }

    abstract_state = ActionableState()

    abstract_state.add_static_widget(widget_first_name_label)
    abstract_state.add_widget(widget_first_name)
    abstract_state.add_widget(widget_save)
    abstract_state.add_widget(widget_error)

    abstract_state.hash = "HASH"

    flow_generator_client.generate_flow.return_value = None

    runner_client.concrete_state.return_value = target_concrete_state

    flow_publisher = Mock()

    flow_executor = Mock()

    # Act.
    with patch(AgentLoop.__module__ + '.StateAbstracter') as state_abstracter:
        with patch(AgentLoop.__module__ + '.PriorityMemory') as memory_mock:

            actual_memory_mock = Mock()
            memory_mock.return_value = actual_memory_mock

            actual_mapper = Mock()
            state_abstracter.return_value = actual_mapper

            actual_mapper.process.return_value = abstract_state

            loop = AgentLoop(sut_url, runner_url, form_expert_client,
                             runner_client, page_analysis_client,
                             flow_generator_client, flow_publisher,
                             flow_executor)

            loop.loop_iteration()

            # Assert.
            assert flow_generator_client.generate_flow.called
            assert not flow_publisher.publish.called
            assert flow_executor.execute.called
            assert not actual_memory_mock.in_memory.called
            assert not actual_memory_mock.update_memory.called
            assert not form_expert_client.get_concrete_value.called
            assert not runner_client.perform_action.called
Esempio n. 9
0
def test_unable_to_perform_runner_click_action():
    # Arrange.
    form_expert = Mock()
    page_analyzer = Mock()
    state_abstracter = StateAbstracter()
    label_extracter = LabelExtraction()
    observer = StateObserver()
    defect_rep = Mock()

    flow_executor = FlowExecutor(form_expert, page_analyzer, state_abstracter,
                                 label_extracter, observer, defect_rep)
    flow_executor.form_fill_strategy = Mock()

    to_parse = "OBSERVE TEXTBOX EMAIL TRY VALID EMAIL CLICK COMMIT NOTOBSERVE ERRORMESSAGE"
    parser = SequenceParser()
    abstract_flow = parser.parse(to_parse)

    flow_planner = FlowPlanner()

    page_analysis = {
        'analysis': {
            'labelCandidates': ['Label_Email'],
            'COMMIT': ['C1'],
            'ERRORMESSAGE': ['E1'],
            'errorMessages': ['E1']
        }
    }

    page_analyzer.run_analysis.return_value = page_analysis

    abstract_state = ActionableState()

    widget_email_label = {
        'key': 'Label_Email',
        'label': 'EmailLabel',
        'actions': [],
        'selector': '#lblEmail',
        'properties': {
            'tagName': 'LABEL',
            'text': 'E-mail',
            'x': 10,
            'y': 10
        }
    }

    widget_email = {
        'key': 'EMAIL',
        'label': 'Email',
        'actions': ['set'],
        'selector': '#email',
        'properties': {
            'tagName': 'INPUT',
            'x': 20,
            'y': 20
        }
    }

    widget_save = {
        'key': 'C1',
        'label': 'Save',
        'actions': ['click'],
        'selector': '#save',
        'properties': {
            'tagName': 'BUTTON',
            'x': 40,
            'y': 40
        }
    }

    widget_error = {
        'key': 'E1',
        'label': 'Error',
        'properties': {
            'tagName': 'LABEL',
            'x': 60,
            'y': 60
        }
    }

    abstract_state.add_widget(widget_email_label)
    abstract_state.add_widget(widget_email)
    abstract_state.add_widget(widget_save)
    abstract_state.add_widget(widget_error)

    target_concrete_state = {
        'widgets': {
            'Label_Email': widget_email_label,
            'EMAIL': widget_email,
            'C1': widget_save,
            'E1': widget_error
        }
    }

    runner_mock = Mock()
    runner_mock.concrete_state.return_value = target_concrete_state

    def click_side_effect(*args, **kwargs):
        if args[1] == 'click':
            return False
        return True

    runner_mock.perform_action.side_effect = click_side_effect

    planned = flow_planner.plan(abstract_state, page_analysis, abstract_flow)
    planned_flow = planned[0]

    # Act.
    resp = flow_executor.execute(abstract_state, runner_mock, planned_flow)

    # Assert.
    assert resp is False