• 【转】数据驱动和关键字驱动简单例子


    数据驱动和关键字驱动简单例子

     

    数据驱动和关键字驱动简单例子(登录)

    数据驱动:程序和数据分离,测试数据存入一个文件中,脚本存入另一个文件中

    公司项目为保密项目,地址使用xxx代替

    数据文件:D:\test\loginData.txt
    文件内容:
    admin_cyl||123456a
    admin_test||a123456
    test_shi||a123456

    代码:

    #encoding=utf-8

    import unittest
    from selenium import webdriver
    import time
    class VisitClosePlaceByIe(unittest.TestCase):
        def setUp(self):
            #启动Ie浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_visitURL(self):
            with open("D:\test\loginData.txt") as fp:
                for line in fp:
                    username,password = line.split("||")
                    password = password.strip().decode("gbk")
                    self.driver.get("xxx")
                    self.driver.find_element_by_id("username")
                    self.driver.find_element_by_id("username").send_keys(username)
                    self.driver.find_element_by_id("password")
                    self.driver.find_element_by_id("password").send_keys(password)
                    self.driver.find_element_by_id("button")
                    self.driver.find_element_by_id("button").click()
                    time.sleep(3) #不加等待时间,则page_source获取的是登录页面的源代码
                    assert u"封闭学校管理平台" in self.driver.page_source ,"Keyword not in page"

        def tearDown(self):
            self.driver.quit()


    关键字驱动:将测试用例的执行步骤存放在文件中,每个步骤单独封闭成一个函数,以这个函数名作为关键字,将函数名及传参写入文件中,第个步骤对应一行文件
    数据文件:D:\test\schoolLogin.txt
    文件内容:
    visitUrl||["xxx"]
    find_ele||["username","admin_cyl"]
    find_ele||["password","123456a"]
    click_login||["button"]
    assert_word||[u"封闭学校管理平台"]


    代码:school_login.py
    #encoding=utf-8
    import unittest
    from selenium import webdriver
    import time

    class VisitSchool(unittest.TestCase):
        def setUp(self):
            #启动IE浏览器
            self.driver = webdriver.Ie(executable_path="D:\IEDriverServer")
        def visitUrl(self,url):
            #由于参数个数不一致,所以使用列表做为参数,再用eval将列表字符串转化为列表
            url = eval(url)
            #打开网页
            self.driver.get(url[0])
        def find_ele(self,arg):
            arg = eval(arg)
            #定位输入框并输入值
            self.driver.find_element_by_id(arg[0]).send_keys(arg[1])
            #本来想用两个函数,一个定位,一个输入值,但是输入函数中无法使用定位,所以合并成一个函数
        def click_login(self,id):
            id = eval(id)
            #定位按钮并点击
            self.driver.find_element_by_id(id[0]).click()
        def assert_word(self,keyword):
            #等待3秒,以便页面加载,否则page_source是登录页面的
            time.sleep(3)
            keyword = eval(keyword)
            #断言
            assert keyword[0].strip() in self.driver.page_source, "Keyword not in page"

        def test_schoolLogin(self):
            with open("D:\test\schoolLogin.txt") as fp:
                for line in fp:
                    action,data = line.split("||")
                    action = action.strip()
                    data = data.decode("gbk").strip()
                    #拼接执行命令
                    exec("self."+action+"(u'"+data+"')")

        def tearDown(self):
             self.driver.quit()

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

     
    转自:https://www.cnblogs.com/test-chen/p/10321164.html
  • 相关阅读:
    实用SQL命令收集
    ZedGraph在Asp.net中的应用
    怎样制作一张万能的Win XP安装光盘
    【转】poj 1823 hotel 线段树【Good】
    【转】unique()函数
    POJ1389Area of Simple Polygons
    【转】poj 1823
    【转】POJ 1177 (线段树+离散化+扫描线) 详解
    【转】POJ各题算法分类和题目推荐
    【转】sort()函数定义在头文件<algorithm>中,它把容器中的数据重新排序成非递减序列
  • 原文地址:https://www.cnblogs.com/mumuluo/p/14466513.html
Copyright © 2020-2023  润新知