Python webdriver test Framework-examples of data-driven DDT

Source: Internet
Author: User

run pip install DDT install data-driven DDT module in CMD environment first

Script:

#encoding =utf-8

From selenium import Webdriver

Import Unittest,time

Import Logging,traceback

Import DDT

From selenium.common.exceptions import nosuchelementexception

#初始化日志对象

Logging.basicconfig (

#日志级别

Level=logging.info,

#日志格式

#时间, the name of the code, the code line number, the log level name, the log information

format= '% (asctime) s% (filename) s[line:% (Lineno) d]% (levelname) s% (message) s ',

#打印日志的时间

Datefmt= '%a,%d%b%Y%h:%m:%s ',

#日志文件存放的目录 (directory must exist) and log file name

Filename= ' D:/report.log ',

#打开日志文件的方式

Filemode= ' W '

)

@ddt. ddt# Decorators

Class Testdemo (UnitTest. TestCase):

def setUp (self):

Self.driver=webdriver. Firefox (executable_path= ' c:\\geckodriver ')

#数据驱动时指定的三个数据, each data is a list

@ddt. Data ("Where is the magical animal", U "Yates"),

[U "Crazy Animal City", U "Goodwin"],

[u] "Stephen Chow", "U" ("the Moonlight Box")

@ddt. unpack#, the test data corresponding to TestData and Expectdata, the top of the list of two parameters assigned to the function, below the example of unpacking

def test_datadrivenbyobj (self,testdata,expectdata): #传参数

Url= "Http://www.baidu.com"

#访问百度首页

Self.driver.get (URL)

#设置隐式等待时间为10秒, individual browser version drivers may be problematic, pending verification

Self.driver.implicitly_wait (10)

Try

#找到搜索输入框 and enter the test data

self.driver.find_element_by_id ("kw"). Send_keys (TestData)

#找到搜索按钮, and click

self.driver.find_element_by_id (' su '). Click ()

Time.sleep (3)

#断言期望结果是否出现在页面源代码中

Self.asserttrue (Expectdata in Self.driver.page_source)

Except Nosuchelementexception,e:

Logging.error ("element in page not existed, abnormal stack info:" +str (Traceback.format_exc ()))

Except Assertionerror,e:

Logging.info ("Search '%s ', expected '%s ', failed"% (testdata,expectdata))

Except Exception,e:

Logging.error ("Unknown error, error message:" +str (Traceback.format_exc ()))

Else

Logging.info (' Search '%s ', expected '%s ' passed '% (testdata,expectdata))

def tearDown (self):

Self.driver.quit ()

If __name__== ' __main__ ':

Unittest.main ()

Results:

If there is no problem with the log object, the log will be hit into the file, as follows (the first run-time log object filename in the wrong written fimename, there is no generated report.log file, but printed in the cmd)

Garbled because the English system does not support the Chinese cause

D:\test_python>python task_test.py

Tue, June 2018 12:48:12 Task_test.py[line:54]info search "Tau?? Σ?? Σè?τ?? Σ£?σ?? Theta??? ", expected" σ? What's the " Φ "? "Passed

. Tue, June 2018 12:48:28 Task_test.py[line:54]info search "Tau?? Τ?éσè?τ?? Σ??? ", expected" σ?? Σ╛╖μ? "Passed

. Tue, June 2018 12:48:43 Task_test.py[line:54]info Search "σ?oφ?¥φ?┐μ╕╕σ? μ£êσà? Σ?¥τ¢? ", expected" σ?? Μ?? Theta? ? "Passed

.

----------------------------------------------------------------------

Ran 3 Tests in 47.395s

Ok

The second run, modified the log object, the log was hit in the Report.log, the console did not hit the log

D:\test_python>python task_test.py

...

----------------------------------------------------------------------

Ran 3 Tests in 46.251s

Ok

Report.log:

Content:

Tue, June 2018 12:49:41 Task_test.py[line:54]info Search "Where are the magical animals", expected "Yates" passed

Tue, June 2018 12:49:56 Task_test.py[line:54]info Search "Crazy Animal City", expected "Goodwin" passed

Tue, June 2018 12:50:12 Task_test.py[line:54]info Search "The Moonlight Box of the West Tour", expected "Stephen Chow Chi" passed

Examples of unpacking:

>>> def add (A, B):

... return A+b

...

>>> add

3

>>> Add (())

Traceback (most recent):

File "<stdin>", line 1, in <module>

Typeerror:add () takes exactly 2 arguments (1 given)

#直接传元祖会报错, but adding a * to the front is the process of unpacking, splitting the elements and assigning the elements to the Add function separately.

>>> Add (* ())

3

Python webdriver test Framework-examples of data-driven DDT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.