python for selenium 数据驱动测试

 1 # -*- coding:utf-8 -*-

 2 """

 3 数据驱动测试,从 csv 文件中读取数据

 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()

 

你可能感兴趣的:(selenium)