How to write good Python+selenium automation?

Source: Internet
Author: User
Tags selenium grid



Hello, everyone sir Good, today I tell you how to use Python+selenium to do automated testing, landlord in the development test before doing Java development, for various reasons, the landlord turned into a development test, and then self-taught the script language Linux and Python, for these two scripts, I am very fond of, for what, because the efficiency is too high, so the landlord abandoned Java to Python, as for why, do not long explanation, you understand. Next I will tell you how to use Python+selenium automation



1, what is selenium



Selenium is an open source automated testing framework, mainly applicable to Web testing, can support multiple languages (Java, C #, python,php, etc.), since the support of multi-language development, the cross-platform nature does not have to say more, selenium there are several versions, one is selenium IDE (is a plugin installed in Firefox, can be used to record scripts, and then export automatically generated corresponding development language files), selenium Grid (automation tools, landlord has not yet in-depth research), Seleniun RC (a remote control access to achieve testing purposes, Server side and client side), the above is my summary, the specific definition, you can go to the official website to see, here do not explain, we just need to know what it is enough.



How 2,selenium Works



about how selenium work, but also a lot of small partners want to know, selenium has a driver called Webdriver, which is the core of selenium, support a variety of browsers (Firefox, Chrome, Safiri, ie, etc.), Chrome, IE requires additional webdriver drives (Iedriver.exe, Chromedirver.exe) for browsing, here is a simple example:


1 # coding: utf-8
  2 
  3 from selenium import webdriver #import selenium
  4 import time
  5
  6 browser = webdriver.Chrome (r‘C: \ Python27 \ chromedriver.exe ’) #Load the chrome driver and create an object
  7
  8 browser.get (‘http://www.baidu.com’) #Get the address of the Web to be tested
  9 
10 browser.maximize_window () # Window maximize
11
12 browser.find_element_by_id (‘kw’). Send_keys (‘selenium‘) #Get elements in the webpage
13
14 browser.find_element_by_id (‘su‘). Click ()
15
16 time.sleep (3)
17
18 browser.quit () # Close the driver object 


Looking at the above code, we can know that the main work of selenium is to crawl the elements in the Web page, and then make the corresponding assignment, events, etc., selenium have a variety of methods to obtain elements, have access to Id,name,css,xpath, etc.



How 3,selenium work with Python



We use Selenium+python is certainly not a few of the more simple lines of code that we write, in normal automation, in order to facilitate the management of maintenance scripts, generally use the appropriate framework to improve the quality of the code and maintenance costs, about the framework is mainly uinittest ( Python comes with a unit test framework), Htmltestrunner (the main user generates test reports and results, the detailed use of which you can check the relevant documents), Smtplib (mail package, as long as the test results can be sent to PM in the form of mail), in addition to the test framework, There is also the code separation, reuse, and so on, this part is not much more detailed, mainly environmental construction and architecture of things, the next time to do a detailed explanation.



The content of this article is not much value, mainly to let you understand the selenium and Python automation, purely one-sided talk, the shortcomings please leave a message below, thank you!



(article original, reproduced when please indicate the source, thank you!) ))



How to write good Python+selenium automation?


Related Article

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.