1

I want to automate the testing of a GUI. In particular I want to test the "save" option from the file menu. I.e. when the save button is clicked the data from the fields in the UI are collected and then written to a json file.

The problem is, when the QFileDialog pops up asking for the user to enter the name of the file to save to, I cant get a handle on the dialog to continue testing. How do you automate this bit? I am not sure how to get a handle on the QFileDialog.

    def hdl_save_report_as(self):
        try:
            self.dialog.setDefaultSuffix('json')
            save_file, _ = self.dialog.getSaveFileName(caption="Save Report", filter="JSON Files (*.json)")
            if save_file:
                score = self.parent.main_tab_view.get_fields()
                self.ui_model.report_path = save_file
                with open(save_file, 'w') as f:
                    json.dump(score, f, indent=4)
        except Exception as e:
            result = dlg.message_dialog("Exception", "We ran into an error!", QMessageBox.Warning, e)
            print(e)
    def test_save_report(self):
        self.main_window.menu.bt_save_file.trigger()
        self.main_window.menu.dialog.selectFile('a_test_report.json')

        ## save_dialog = QApplication.activeModalWidget()
        # save_dialog = QApplication.activeWindow()
        # children = save_dialog.findChildren()
        # active_line_edit = None
        # confirm_button = None
        # for c in children:
        #     if type(c) is QLineEdit:
        #         if c.hasFocus():
        #             active_line_edit = QLineEdit(c)
        #     # if type(c) is QPushButton:
        #     #     if
        # active_line_edit.setText("a_test_report")
        self.assertTrue(os.path.exists(os.path.join(os.getcwd(), 'a_test_report.json')))

I have tried a few different approaches, is there a standard way to do this? Hopefully I have missed something obvious.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

From what I can see and as explained in another post it is not possible to simulate interaction with the special QFileDialogs like getSaveFileName or getOpenFileName.

My approach is now managing the files in setup function like

    def setUp(self):
        self.test_report = "a_test_report.json"
        
        # Remove an existing test report
        if os.path.exists(self.test_report):
            os.remove(self.test_report)
        self.assertFalse(os.path.exists(os.path.join(os.getcwd(), self.test_report)))
        
        # Create a file to test reading / writing
        with open(self.test_report, 'w') as f:
            f.write("")
        self.assertTrue(os.path.exists(os.path.join(os.getcwd(), self.test_report)))
        self.assertTrue(os.stat(self.test_report).st_size == 0)
        
        self.main_window = MainWindow()

And testing like

    def test_save_action(self):
        # Trigger the save button - check if the file was overwritten with new info
        self.main_window.menu.bt_save_file.trigger()
        self.assertTrue(os.stat(self.test_report).st_size > 0)

Although it seems it is not possible to test a "Save As" function.