【发布时间】:2021-10-21 01:26:04
【问题描述】:
我使用 PyQt5 已经有一段时间了,并且希望在 GUI 上使用 Windows 10 执行测试。Appium 似乎是一个不错的选择。现在,我可以通过在 Qt 设计器中设置 accessbleName 来访问小部件。但是,如何访问窗口上显示的小部件文本?在official docs 上还没有找到任何东西。谢谢。
例如,有一个登录窗口。每次用户输入错误的用户名和密码组合时,都会有一条消息显示Invalid username or password。
这是我当前的PyQt5 代码:
class LoginScreen(QDialog):
''' The page where the client can login '''
def __init__(self):
super(LoginScreen, self).__init__()
loadUi('login.ui', self)
# before coding, change the objectName in the qt designer
self.passwordField.setEchoMode(QtWidgets.QLineEdit.Password)
self.login.clicked.connect(self.login_function)
def login_function(self):
user = self.emailField.text() # might be tested with appium webdriver
password = self.passwordField.text()
if len(user) == 0 or len(password) == 0:
self.errorMsg.setText("Please input all fields.") # how could I get the text?
else:
conn = sqlite3.connect(shop_data)
cur = conn.cursor() # execute the query
query = f"SELECT password FROM login_info WHERE username = '{user}' "
cur.execute(query)
result_pass = cur.fetchone()[0] # return tuple;
print(result_pass) # the password printed in the console
if result_pass == password:
print("Successfully logged in")
self.errorMsg.setText("") # how could I get the text?
else:
self.errorMsg.setText("Invalid username or password") # how could I get the text?
这里是Appium测试代码,只关注测试用例:
def testLoginFailure(self):
self.driver.implicitly_wait(10)
self.driver.find_element_by_name("login").click()
self.driver.find_element_by_name("username").send_keys("marysmith")
self.driver.find_element_by_name("password").send_keys("123456")
self.driver.find_element_by_name("loginPageLogin").click()
# the text would be "errorMsg"; "errorMsg" != "Invalid username or password"
self.assertEqual(self.driver.find_element_by_name("errorMsg").text, "Invalid username or password")
【问题讨论】:
-
Qt属性可以作为函数调用访问,不应该是
self.driver.find_element_by_name("errorMsg").text()吗? -
我试过了,它给了我
TypeError: 'str' object is not callable。 -
目前,我只能使用
.is_displayed()来确保文本显示,但不能确保确切的文本。 -
哦,抱歉,我现在明白了:
find_element_by_name不返回 Qt 小部件。不幸的是,我不知道 Appium 是如何工作的,但无论如何,您是否尝试过打印该文本并将默认标签文本设置为其他内容,以确保至少正确读取该属性并且问题可能在别处。另外,我看到有人提到getText(),你试过吗? -
如果您找到了合适的解决方案,请使用相关字段为您的问题提供您自己的答案。
标签: python pyqt5 python-appium