首页 文章

仅在使用unittest类时才能找到xpath元素

提问于
浏览
0

最奇怪的事情:我正在从一个模块运行一个程序,该模块从另一个模块调用一些函数 . 问题是相同的代码在使用基本函数时会完美运行,但第三个函数只有在unittest类中使用它时才会失败,并带有以下消息:

NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// * [@ id =”app_skeleton“] / tbody / tr [6] / td / form /表/ tbody的/ TR [2] / TD /表/ tbody的/ TR [2] / TD [9] /一个“} .

以下是全班:

from Main_Package.General_Functions.Functions import 
  login,search_by_customer_id,add_subscriber_iden_and_activate,browser

class FirstTest(unittest.TestCase):
  @staticmethod
  def test_login():
     login()

 @staticmethod
 def test_search_by_customer_id():
     search_by_customer_id()

@staticmethod
def test_add_subscriber_iden_and_activate():
    add_subscriber_iden_and_activate()




#login()   ******this is the block of code that will run perfectly without # 
****
#search_by_customer_id()
#add_subscriber_iden_and_activate()


if __name__ == '__main__':
    unittest.main()

1 回答

  • 0

    执行单元测试时,必须确保特定测试已准备好“环境”以执行测试 .

    测试的执行不按顺序进行,所以:

    test_add_subscriber_iden_and_activate()
    

    可以在之前执行

    test_login()
    

    如果要在 each 测试之前和之后执行某些操作,可以使用方法 setUp()tearDown() ,如果要在 all 测试之前和之后执行某些操作,则可以使用 setUpClass()tearDownClass() .

    Here文档 .

相关问题