登录模块化
通过上一节对测试模型的学习可以发现,在我们的目前的脚本中有很多代码是可以模块化的,比如登录模块。我们的每一个用例的执行都需要登录脚本,那可我们是否可以将登录脚本独立到单独的文件调用。下面以快播私有云的登录退出测试用例为例:
webcloud.py
代码示例:
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://passport.kuaibo.com"
self.verificationErrors = []
self.accept_next_alert = True
#私有云登录用例
def test_login(self):
driver = self.driver
driver.get(self.base_url +"/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")
driver.maximize_window()
#登录
driver.find_element_by_id("user_name").clear()
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").clear()
driver.find_element_by_id("user_pwd").send_keys("123456")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
#新功能引导
driver.find_element_by_class_name("guide-ok-btn").click()
time.sleep(3)
#退出
driver.find_element_by_class_name("Usertool").click()
time.sleep(2)
driver.find_element_by_link_text("退出").click()
time.sleep(2)
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
注明:本用例暂不关注unittest 内容,第六章会详细的讲解unittest 单元测试框架。
从业务流程及用例分析,每一个自动化测试用例的执行过程为:先执行登录操作,然后执行具体的操作(如文件/文件夹的创建、删除、移动、重命名等操作),最后执行退出操作。如上面的测试用例,登录与退出操作是相对固定的,那么我们可以把登录与退出操作进行模块化,然后调用,一方面不用写重复代
码,另一方面可以使测试用例更关注具体的用例代码。
在与webcloud.py 相同的目录下创建login.py 文件,脚本内容如下:
代码示例:
#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time
#登录模块(函数)
def login(self):
driver = self.driver
driver.maximize_window()
driver.find_element_by_id("user_name").clear()
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").clear()
driver.find_element_by_id("user_pwd").send_keys("123456")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
webcloud.py
代码示例:
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time
import login #导入登录文件
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://passport.kuaibo.com"
self.verificationErrors = []
self.accept_next_alert = True
#私有云登录用例
def test_login(self):
driver = self.driver
driver.get(self.base_url +"/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")
#调用登录模块
login.login(self)
#新功能引导
driver.find_element_by_class_name("guide-ok-btn").click()
time.sleep(3)
#退出
driver.find_element_by_class_name("Usertool").click()
time.sleep(2)
driver.find_element_by_link_text("退出").click()
time.sleep(2)
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
posted on 2017-01-06 17:11 chenzx0918 阅读(237) 评论(0) 编辑 收藏 举报