【问题标题】:PyQt5, Appium: how to retrieve widget's text?PyQt5,Appium:如何检索小部件的文本?
【发布时间】: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


【解决方案1】:

Qt 设计器中的accessbleName 允许Appium 检查器查找小部件,例如按钮。如果没有accessbleName,检查器的默认行为将是搜索附加在小部件顶部的文本作为名称。因此,答案是直接查找文本,而不是将名称视为变量。

因此,我可以断言屏幕上显示的文本。

self.assertEqual(self.driver.find_element_by_name("Invalid username or password").is_displayed(), True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2021-09-06
    • 2022-10-24
    相关资源
    最近更新 更多