PyQt5,Appium:如何检索小部件的文本?
PyQt5, Appium: how to retrieve widget's text?
我使用 PyQt5 有一段时间了,希望在 GUI 上执行测试,Windows10。Appium 似乎是一个不错的选择。现在,我可以通过在 Qt Designer 中设置 accessbleName
来访问小部件。但是,如何访问 window 上显示的小部件文本?尚未在 official docs 上找到任何内容。谢谢。
例如,有一个登录 window。每次用户输入错误的用户名和密码组合时,都会出现一条消息显示 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 设计器中的accessbleName
允许Appium inspector 查找小部件,例如按钮。如果没有 accessbleName
,检查器的默认行为是搜索附加在小部件顶部的文本作为名称。所以,答案是直接找文字,不要把名字当作变量。
因此,我可以断言屏幕上显示的文本。
self.assertEqual(self.driver.find_element_by_name("Invalid username or password").is_displayed(), True)
我使用 PyQt5 有一段时间了,希望在 GUI 上执行测试,Windows10。Appium 似乎是一个不错的选择。现在,我可以通过在 Qt Designer 中设置 accessbleName
来访问小部件。但是,如何访问 window 上显示的小部件文本?尚未在 official docs 上找到任何内容。谢谢。
例如,有一个登录 window。每次用户输入错误的用户名和密码组合时,都会出现一条消息显示 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 设计器中的accessbleName
允许Appium inspector 查找小部件,例如按钮。如果没有 accessbleName
,检查器的默认行为是搜索附加在小部件顶部的文本作为名称。所以,答案是直接找文字,不要把名字当作变量。
因此,我可以断言屏幕上显示的文本。
self.assertEqual(self.driver.find_element_by_name("Invalid username or password").is_displayed(), True)