begins
personal Blog "Python+selenium GUI Automation implementation" mentioned in Chrome and IE browser call plugin has been uploaded to 51CTO download, the corresponding links are : Chrome,/http/ Down.51cto.com/data/2171584;ie,http://down.51cto.com/data/2171585; direct download is required;
Body
With regard to automation, the essence is to use machine operation instead of manual execution, thereby reducing manpower input and saving project operation success. Excellent automation framework, a possible development process, the early automation use case writing implementation process, may need to have a certain amount of manpower input, after the use case molding, put the maintenance personnel, the need for the corresponding streamlining, if the input and maintenance personnel still high, such automation can be discarded, The concept of an excellent automation framework is derived from this.
Before talking about the concept of excellent automation framework, talk about the idea of automation:
1. For the business to be realized, there is a certain familiarity and understanding, or can be based on text use cases, do the automated use cases easily implemented, that is, to do automation, need based on a certain business foundation, that is, familiar with the business;
2. The 2nd is that the script and data separation, which involves the automation of use case maintenance personnel input this piece, good automation use case, once the implementation of the script and data separation, maintenance personnel only need to maintain the Automation data section based on different scene requirements, the corresponding automation script parts do not need too much maintenance, The ultimate goal is to reduce personnel input;
3. The automation use case is the use case design for the GUI, needs the use case realization personnel and the maintenance personnel to have certain element localization skill, namely XPath, just as the life distinguishes Zhang San and the John Doe, the element localization is to differentiate the input box and the button and so on the element space, thus determines to which control does what action;
4. Excellent automated mine construction, requires simple and easy to use, reduce use cases to achieve personnel and maintenance personnel learning cycle, can be quickly put into project automation, its essence is to reduce human input, automation framework to do the more fools the better;
python+selenium Automation Optimization-common logic step encapsulation
Only the click and input implementations in Python+selenium are implemented as code packages, as follows:
#encoding =utf-8
#针对Python中用到的selenium公共方法做封装
Class Webdriver:
def click (self,webdriver= ' Driver ', method= ' Id ', path= '):
' Method value has name/id/xpath/class;webdriver is the ' driver '
If path== ':
print ' You must input the XPath '
Else
If method== ' id ' or method== ' id ': #根据控件元素的ID属性定位元素, fastest
print ' The method useing is Id '
elem=webdriver.find_element_by_id (PATH)
Elem.click ()
Elif method== ' name ' or method== ' name ': #根据控件元素的name属性定位元素, second speed
Elem=webdriver.find_element_by_name (PATH)
Elem.click ()
Elif method== ' class ' or method== ' class ': #根据控件元素的class属性定位元素, slightly slower
Elem=webdriver.find_element_by_class_name (PATH)
Elem.click ()
Elif method== ' XPath ' or method== ' XPath ': #通过xpath方式定位元素, slowest
Elem=webdriver.find_element_by_xpath (PATH)
Elem.click ()
def text_input (self,webdriver= ' driver ', method= ' Id ', path= ', key= '):
' Method value has name/id/xpath/class;webdriver is the ' driver '
If path== ':
print ' You must input the XPath '
Else
If method== ' id ' or method== ' id ': #根据控件元素的ID属性定位元素, fastest
print ' The method useing is Id '
elem=webdriver.find_element_by_id (PATH)
Elem.clear ()
Elem.send_keys (Key)
Elif method== ' name ' or method== ' name ': #根据控件元素的name属性定位元素, second speed
Elem=webdriver.find_element_by_name (PATH)
Elem.clear ()
Elem.send_keys (Key)
Elif method== ' class ' or method== ' class ': #根据控件元素的class属性定位元素, slightly slower
Elem=webdriver.find_element_by_class_name (PATH)
Elem.clear ()
Elem.send_keys (Key)
Elif method== ' XPath ' or method== ' XPath ': #通过xpath方式定位元素, slowest
Elem=webdriver.find_element_by_xpath (PATH)
Elem.clear ()
Elem.send_keys (Key)
Call
>>> Import Sys
>>> sys.path.append (' E:\Develop\Python ') #笔者封装文件存放路径
>>> Import Hellowebdriver #笔者封装文件名
>>> Explore=hellowebdriver.webdriver () #公共逻辑步骤实现类实例化
>>> Explore.text_input (webdriver=driver,method= ' class ', path= ' js_uname ', key= ' 123456 ') #封装类方法调用
Through the above code, it can be found that the public logic steps in the encapsulation, can reduce the redundancy of the code, the automation use case only call public methods, reduce the cost of code maintenance, and easy to maintain;
Optimization:
1. For the public logic step encapsulation, you can do the existence of the element control assertion operation, through the assertion to achieve authentic automation, if the control does not exist, the direct assertion fails, the use case stops running, if the control exists, do not assert failure, the use case is normal operation, thus the perfect realization of automation;
2. Packaging perfect Optimization, package of public logic method can meet the different scenarios business requirements, such as English input or Chinese input and other different coding methods, to achieve the robustness of encapsulation method;
3. Graphical, for the automation of public methods can do a graphical implementation, become a control, use case writers simply drag the control to be stored in the automation case set;
This article is from the "River a Drop" blog, please be sure to keep this source http://yidishui.blog.51cto.com/6297932/1742301
Python+selenium Automated Public Logic step encapsulation