如何在另一个具有不同类的python文件中使用pyqt5类中存在的函数的输出?

时间:2019-04-17 17:19:14

标签: python python-3.x pyqt5 python-unittest

我使用PYQT5创建了简单的GUI,要求用户提供服务器名称。并想在另一个python unittest脚本中使用用户输入。我是python世界中的新手,正在尝试学习。我已经在psft.py中导入了test_launch.py​​文件,并尝试了以下代码:

self.obj= test_launch.Ui_MainWindow() 
self.host1=self.obj.host() 
print(self.host1) 

,但是在test_launch中单击GUI lauch按钮上的启动按钮并调用执行函数并调用脚本psft.py却无法打印时,但是在psft.py中却无法打印用户输入。

test_launch.py​​

from PyQt5 import QtCore, QtWidgets
import subprocess

class Ui_MainWindow(object):
    def execute(self):
        subprocess.call('python .\psft.py')

    def host(self, server):
        self.server=self.target_details.text()
        #print (self.server)
        return self.server

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(520, 215)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.target_details = QtWidgets.QLineEdit(self.centralwidget)
        self.target_details.setGeometry(QtCore.QRect(190, 40, 113, 20))
        self.target_details.setObjectName("target_details")
        self.sitename = QtWidgets.QLineEdit(self.centralwidget)
        self.sitename.setGeometry(QtCore.QRect(190, 70, 113, 20))
        self.sitename.setObjectName("sitename")
        self.nodes = QtWidgets.QLineEdit(self.centralwidget)
        self.nodes.setGeometry(QtCore.QRect(190, 100, 113, 20))
        self.nodes.setObjectName("nodes")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(120, 40, 91, 20))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(120, 70, 47, 13))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(120, 100, 47, 13))
        self.label_3.setObjectName("label_3")
        self.launch_btn = QtWidgets.QPushButton(self.centralwidget)
        self.launch_btn.setGeometry(QtCore.QRect(190, 150, 75, 23))
        self.launch_btn.setObjectName("launch_btn")
        self.launch_btn.clicked.connect(self.execute)
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(320, 40, 171, 16))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(320, 70, 171, 16))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(320, 100, 171, 16))
        self.label_6.setObjectName("label_6")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Test Launcher"))
        self.label.setText(_translate("MainWindow", "Target server"))
        self.label_2.setText(_translate("MainWindow", "Site Name"))
        self.label_3.setText(_translate("MainWindow", "Nodes"))
        self.launch_btn.setText(_translate("MainWindow", "Launch Test"))
        self.label_4.setText(_translate("MainWindow", "ex(http://server:port)"))
        self.label_5.setText(_translate("MainWindow", "ex(PSFINDMO)"))
        self.label_6.setText(_translate("MainWindow", "ex(PSFT_HR,HRMS)"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

psft.py

from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from time import sleep
import HtmlTestRunner
from selenium.common.exceptions import NoSuchElementException
import test_launch

class PeoplesoftTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(executable_path='../Drivers/chromedriver.exe')
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    def test_1_search_login_test(self):

        self.obj= test_launch.Ui_MainWindow()
        self.host1=self.obj.host()

        print(self.host1)
       ---code continue---
    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")

if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/py/projects/test_automation/HTML'))

1 个答案:

答案 0 :(得分:0)

您正在启动一个进程,因此使用脚本不会使您获得所需的值。

使用this answer,您可以通过环境变量传递数据

test_launch.py​​

import os
from PyQt5 import QtCore, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(520, 215)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.target_details = QtWidgets.QLineEdit(self.centralwidget)
        self.target_details.setGeometry(QtCore.QRect(190, 40, 113, 20))
        self.target_details.setObjectName("target_details")
        self.sitename = QtWidgets.QLineEdit(self.centralwidget)
        self.sitename.setGeometry(QtCore.QRect(190, 70, 113, 20))
        self.sitename.setObjectName("sitename")
        self.nodes = QtWidgets.QLineEdit(self.centralwidget)
        self.nodes.setGeometry(QtCore.QRect(190, 100, 113, 20))
        self.nodes.setObjectName("nodes")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(120, 40, 91, 20))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(120, 70, 47, 13))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(120, 100, 47, 13))
        self.label_3.setObjectName("label_3")
        self.launch_btn = QtWidgets.QPushButton(self.centralwidget)
        self.launch_btn.setGeometry(QtCore.QRect(190, 150, 75, 23))
        self.launch_btn.setObjectName("launch_btn")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(320, 40, 171, 16))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(320, 70, 171, 16))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(320, 100, 171, 16))
        self.label_6.setObjectName("label_6")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Test Launcher"))
        self.label.setText(_translate("MainWindow", "Target server"))
        self.label_2.setText(_translate("MainWindow", "Site Name"))
        self.label_3.setText(_translate("MainWindow", "Nodes"))
        self.launch_btn.setText(_translate("MainWindow", "Launch Test"))
        self.label_4.setText(_translate("MainWindow", "ex(http://server:port)"))
        self.label_5.setText(_translate("MainWindow", "ex(PSFINDMO)"))
        self.label_6.setText(_translate("MainWindow", "ex(PSFT_HR,HRMS)"))


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.launch_btn.clicked.connect(self.execute)
        self._process = QtCore.QProcess(self)

    @QtCore.pyqtSlot()
    def execute(self):
        host = self.target_details.text()
        os.environ["TEST_HOST"] = host
        self._process.start("python", ["psft.py"])

    @QtCore.pyqtSlot()
    def on_finished(self):
        del os.environ["TEST_HOST"]


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

psft.py

import os

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

import unittest
import HtmlTestRunner


class PeoplesoftTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        path = "../Drivers/chromedriver.exe"
        cls.driver = webdriver.Chrome(executable_path=path)
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    def test_1_search_login_test(self):
        host = os.environ.get("TEST_HOST")
        if host is not None:
            print(host)
        else:
            print("not found")

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")


if __name__ == "__main__":
    html_output = "C:/py/projects/test_automation/HTML"
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=html_output))