python for selenium 数据驱动测试
生活随笔
收集整理的這篇文章主要介紹了
python for selenium 数据驱动测试
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 # -*- coding:utf-8 -*-
2 """
3 數(shù)據(jù)驅(qū)動(dòng)測(cè)試,從 csv 文件中讀取數(shù)據(jù)
4 """
5 from selenium import webdriver
6 import csv, sys
7
8
9 test_executed = 0
10 test_passed = 0
11 test_failed = 0
12 test_status = True
13
14
15 try:
16 driver = webdriver.Firefox()
17 driver.get('C:\BMICalculator.html')
18
19 datafile = open(r'c:\data.csv', 'rb')
20 reader = csv.reader(datafile)
21
22 test_executed = 0
23
24 for row in reader:
25 test_executed += 1
26 print 'Test' + str(test_executed)
27
28 heightField = driver.find_element_by_name('heightCMS')
29 heightField.clear()
30 heightField.send_keys(row[0])
31
32 weightField = driver.find_element_by_name('weightKg')
33 weightField.clear()
34 weightField.send_keys(row[1])
35
36 calculateButton = driver.find_element_by_id('Calculate')
37 calculateButton.click()
38
39 bmiLabel = driver.find_element_by_name('bmi')
40 bmiCategoryLabel = driver.find_element_by_name('bmi_category')
41
42 if bmiLabel.get_attribute('value') == row[2]:
43 print "PASS, expected value for BMI <" + row[2] + "> actual <" + bmiLabel.get_attribute('value') + ">"
44 else:
45 print "FAIL, expected value for BMI <" + row[2] + "> actual <" + bmiLabel.get_attribute('value') + ">"
46 test_status = False
47
48 if bmiCategoryLabel.get_attribute('value') == row[3]:
49 print "PASS, expected value for BMI Category <" + row[3] + "> actual <" + bmiCategoryLabel.get_attribute('value') + ">"
50 else:
51 print "Fail, expected value for BMI Category <" + row[3] + "> actual <" + bmiCategoryLabel.get_attribute('value') + ">"
52 test_status = False
53
54 if test_status == True:
55 test_passed = test_passed + 1
56 else:
57 test_failed = test_failed + 1
58
59 except :
60 print "Unexpected error: ", sys.exc_info()[0]
61 raise
62
63 finally:
64 print "---------------------------------------------------------------"
65 print "Total (" + str(test_executed) + ") Tests Executed"
66 print "Total (" + str(test_passed) + ") Tests Passed"
67 print "Total (" + str(test_failed ) + ") Tests Failed"
68 driver.close()
69 datafile.close()
?
轉(zhuǎn)載于:https://www.cnblogs.com/Roger1227/p/3192116.html
總結(jié)
以上是生活随笔為你收集整理的python for selenium 数据驱动测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 1293 - Duty Free
- 下一篇: 利用sqlmap和burpsuite绕过