Background:
Recently, I started to sort out the related issues in the Selenium+python automated testing project. I accidentally turned over the script I wrote when I was learning automation at the time, and found that I could hardly recognize the ghost running account I wrote, so today I specially sorted out the automated development of Selenium+python. Basic example of script;
Example script:
1. Take the simplest example code here to explain the points that need to be paid attention to when writing scripts, and the role of each module;
# -*- coding:utf-8 -*- __author__='dong.c' from selenium import webdriver import unittest import HTMLTestRunner import sys from time import sleep reload(sys) sys.setdefaultencoding('utf-8') class BaiduTest(unittest.TestCase): """Baidu homepage search test case""" def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = u"http://www.baidu.com" def test_baidu_search(self): driver = self.driver print u"Start [case_001] Baidu search" driver.get(self.base_url) #validate title self.assertEqual(driver.title,u"Baidu, you will know") driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(u"博客园") driver.find_element_by_id("su").click() sleep(3) #Validate search result title self.assertEqual(driver.title, u"Blog Park_Baidu Search") def tearDown(self): self.driver.quit() if __name__ == '__main__': testunit = unittest.TestSuite() textit.addTest (BaiduTest ('test_baidu_search')) #Define report output path htmlpath = u"testReport.html" fp = file(htmlpath,"wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"Baidu Test",description=u"Test Case Results") runner.run(testunit) fp.close()
Code Explanation:
1. In general, the code is divided into five blocks:
a. File preservation code and author definition
# -*- coding:utf-8 -*-
__author__='dong.c'
b. Import relevant basic modules
#Import the webdriver module from selenium
from selenium import webdriver
#Import the unittest module as the base class for use cases
import unittest
#Import the html report generation module for report generation in html format
import HTMLTestRunner
#import the sys module
import sys
#Import the sleep module for forced waiting
from time import sleep
c. Set the current pythony running environment to utf-8
#Set the current python to run under utf-8 encoding, so that your Chinese will not be garbled
reload(sys)
sys.setdefaultencoding("utf-8")
d. Define and implement test cases
#Inherit from unittest.TestCase
class BaiduTest(unittest.TestCase):
"""Baidu homepage search test case"""
#Use case initialization function, automatic execution
def setUp(self):
#Initialize the instance based on firefox browser
self.driver = webdriver.Firefox()
#Set the global implicit waiting time for the current webdriver, the maximum is 30s
self.driver.implicitly_wait(30)
#set url home page
self.base_url = u"http://www.baidu.com"
def test_baidu_search(self):
#Simple assignment, so that you don't have to write self.driver every time after this test;
driver = self.driver
# print output to console
print u"Start [case_001] Baidu search"
#Start the browser and visit the home page
driver.get(self.base_url)
#validate title
self.assertEqual(driver.title,u"Baidu, you will know")
#Clean up the data in the input box
driver.find_element_by_id("kw").clear()
#Enter blog garden in the input box
driver.find_element_by_id("kw").send_keys(u"博客园")
#Click on Baidu search
driver.find_element_by_id("su").click()
#Force wait 3s
sleep(3)
#Validate search result title
self.assertEqual(driver.title, u"Blog Park_Baidu Search")
#Use case level cleanup function, automatic execution
def tearDown(self):
#Exit webdriver and close all browser windows under the current webdriver session
self.driver.quit()
e. The main running entry of the test script
#python main function
if __name__ == '__main__':
#Initialize a use case suite
testunit = unittest.TestSuite()
#Add a test to the use case suite
textit.addTest (BaiduTest ('test_baidu_search'))
#Define the report output path, here is the current directory
htmlpath = u"testReport.html"
#Open the test report file
fp = file(htmlpath,"wb")
#Build a HTMLTestReport executor
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"Baidu Test",description=u"Test Case Results")
# run the test set
runner.run(testunit)
#Close the open test report file
fp.close()
Run the code:
After running the above command, it is found that the Baidu homepage is opened. After entering the blog park, click the Baidu button, and the browser is closed after the search results are displayed; the test report file in html format is generated in the current directory;
Summarize:
The key points to pay attention to, ensure that the selenium environment has been configured and the corresponding browser driver has been downloaded;
Make sure the HTMLTestRunner file is downloaded;
Corresponding to the introduction of environmental issues in the next blog;