欢迎关注公众号 学习资料不会少

selenium数据驱动框架目录

├── common

├── data

├── logs

├── pom

├── report

├── screenshots

└── testcases

common/parse_data.py

import csv

import os

# c:\\Users\\xxxx\\Desktop\\data_demo   Windows Path

logindata_csv = os.path.join('/Users/zack/Desktop/

datadriver_demo','data','login_data.csv')

print("path=",logindata_csv)

def parse_csv():

    arr=[]

    with open(logindata_csv,encoding='utf-8') as f:

        reader = csv.reader(f)

        next(reader)

        for row in reader:

            arr.append(row)

    return arr

testcases/test_login.py

import sys

sys.path.append('/Users/zack/Desktop/datadriver_demo')

import unittest

from ddt import ddt,data,unpack,file_data

from common.parse_data import parse_csv

from selenium import webdriver

@ddt

class LoginTest(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        cls.driver = webdriver.Chrome()

        cls.driver.maximize_window()

        cls.driver.implicitly_wait(30)

    @classmethod

    def tearDownClass(cls):

        cls.driver.quit()

    # @data(['xiaoming','123123','fail','用户名或密码错误'],      ['','123123','fail','信息不完整。'])

    @data(*parse_csv())

    @unpack

    def test_login(self,username,password,status,check_

    msg):

        self.driver.get("http://118.31.19.120:3000/signin")                self.driver.find_element_by_id('name').send_keys

        (username)

        self.driver.find_element_by_id("pass").send_keys

        (password)

        self.driver.find_element_by_css_selector(".span-

        primary").click()

        # error_msg

        if status=='fail':

            result_text = self.driver.find_element_by_tag_

            name("strong").text

         else:

            result_text = self.driver.find_element_by_css_

            selector('span.user_name > a').text

         self.assertEqual(check_msg,result_text)

                if __name__ == '__main__':

    unittest.main()

testcase/test_login.py

import sys

sys.path.append('/Users/zack/Desktop/datadriver_demo')

import os

import time

import unittest

import HtmlTestRunner

from ddt import ddt,data,unpack,file_data

from common.parse_data import parse_csv

from pom.loginpg import LoginPage

from selenium import webdriver

@ddt

class LoginTest(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        cls.driver = webdriver.Chrome()

        cls.driver.maximize_window()

        cls.driver.implicitly_wait(30)

    @classmethod

    def tearDownClass(cls):

        cls.driver.quit()

    def tearDown(self):

        rootdir = '/Users/zack/Desktop/datadriver_demo'

        screenshots_path = os.path.join(rootdir,'screenshots

        ')

        #TODO 图片文件名格式为 2018_09_02-17_12_11.

        png

        # pngfile_name =os.path.join(screenshots_path,str

        (time.localtime())+'.png')

        # self.driver.save_screenshot(pngfile_name)

        self.driver.delete_all_cookies()

    # @data(['xiaoming','123123','fail','用户名或密码错误'],      ['','123123','fail','信息不完整。'])

    @data(*parse_csv())

    @unpack

    def test_login(self,username,password,status,check_

    msg):

        text = LoginPage().check_login(self.driver,username,

        password,status)

        self.assertEqual(check_msg,text)

if __name__ == '__main__':

    unittest.main(testRunner=HtmlTestRunner.HTMLTest

    Runner(output="report"))

pom/loginpg.py

class LoginPage():

    base_url="http://118.31.19.120:3000/"

   login_link_text = "登录"

    username_id="name"

    pass_id="pass"

    login_btn_css_selector=".span-primary"

    fail_tag_name="Strong"

    success_css_selector='span.user_name > a'

    def check_login(self,driver,username,password,status):

        driver.get(self.base_url)

        driver.find_element_by_link_text(self.login_link_text).

        click()

        driver.find_element_by_id(self.username_id).send_

        keys(username)

        driver.find_element_by_id(self.pass_id).send_keys

        (password)

        driver.find_element_by_css_selector(self.login_btn_

        css_selector).click()

        # error_msg

        if status=='fail':

            result_text = driver.find_element_by_tag_name

            (self.fail_tag_name).text

         else:

            result_text = driver.find_element_by_css_selector

            (self.success_css_selector).text

         return result_text

common/tool.py

import os

def getRootDir():

    filepath = os.path.abspath(__file__)

    current_dir = os.path.dirname(filepath)

    while current_dir:

        print(current_dir)

        if os.path.exists(os.path.join(current_dir,'readme.

        md')):

            break

            current_dir = current_dir[0:current_dir.rfind

            (os.path.sep)]

        return current_dir

Jenkins持续集成

生成测试报告

# tests.py

import random

try:

    import unittest2 as unittest

except ImportError:

    import unittest

class SimpleTest(unittest.TestCase):

    @unittest.skip("demonstrating skipping")

    def test_skipped(self):

        self.fail("shouldn't happen")

    def test_pass(self):

        self.assertEqual(10, 7 + 3)

    def test_fail(self):

        self.assertEqual(11, 7 + 3)

JUnit with pytest

py.test --junitxml results.xml tests.py

JUnit with nose

nosetests --with-xunit

JUnit with nose2

nose2 --plugin nose2.plugins.junitxml --junit-xml tests

JUnit with unittest-xml-reporting

test.py 文件追加如下代码

if __name__ == '__main__':

    import xmlrunner

    unittest.main(testRunner=xmlrunner.XMLTestRunner

    (output='test-reports'))

运行

python tests.py

往期精彩文章

喜报来了!凡猫学员薪资最高16K!

金融行业软件测试介绍

2020年为什么大家都开始学习自动化测试?

学习测试开发前 你需要掌握的python 代码水平

1万+软件测试人员都在学的精品课程免费送,大家别错过

b0cb732845c2233adbada1d01637417d.gif
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐