我正在用python创建自动化框架,但是我却坚持创建Web驱动程序的单个实例。这是我的框架设计的摘录:
从selenium import webdriver 类驱动程序开始:#创建 一个类变量 Instance = None @staticmethod def Initialize(): Instance = webdriver.Firefox() 返回实例
从驱动程序导入驱动程序 类LoginPage: @staticmethod def GoToURL(): Driver.Instance.get(“ sample url”) @staticmethod def Login(): Driver.Instance.find_element_by_id(“ session_key-login”)。send_keys(“ sample @ gmail.com“) Driver.Instance.find_element_by_id(” session_password-login“)。send_keys(” sample_password“) Driver.Instance.find_element_by_id(” btn-primary“)。click()
问题是Driver.Instance.get()或与此相关的Driver.Instance.find_element ...正在引发错误。可能是这里没有识别Driver.Instance。
我已经解决了我的问题!!!我没有在文件Driver.py中创建类变量,而是这样做的:
从硒导入webdriver 实例=无 def Initialize(): 全局实例 Instance = webdriver.Chrome(“驱动程序路径”) Instance.implicitly_wait(5) 返回实例 def CloseDriver(): 全局实例 Instance.quit()
我必须使用此实例的地方,我这样做:
导入驱动程序 类LoginPage: @staticmethod def GoToURL(): Driver.Instance.get(“示例url”) @staticmethod def Login(): Driver.Instance.find_element_by_id(“ session_key-login”)。send_keys(“示例用户名”) Driver.Instance.find_element_by_id(“ session_password-login”)。send_keys(“示例密码”) Driver.Instance.find_element_by_id(“ btn-primary”)。click()
我正在运行此测试的文件是这样的:
从LoginPage导入unittest 导入驱动程序从LoginPage导入LoginPage 类LoginTest(unittest.TestCase): def setUp(self): Driver.Initialize() def testUserCanLogin(self): #转到登录URL LoginPage.GoToURL() #输入用户名,密码和点击登录 LoginPage.Login() #在右上角,检查是否正确的用户已登录 def tearDown(self): Driver.CloseDriver()
这像魅力一样运作...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句