Web Automation Test Apocalypse

Source: Internet
Author: User

1. First, for a friend who wants to learn automated testing, you need to know a language that is commonly used such as Java or Python. Because there is no language base, you cannot write automated scripts.

I personally choose Java

2. With the development of the language of the foreshadowing, then began to start selenium. Understand its development, understand its composition, and understand its implementation principles.

Development : SELENIUM1 Selenium2,selenium experienced two versions, Selenium 1.0 and Selenium 2.0, this article only introduces Selenium2 principle, in Selenium 2.0 The main thrust is webdriver,selenium2 aka Selenium webdriver. Selenium is a Web application testing tools, support multi-platform, multi-browser, multi-language to implement automated testing, Selenium2 the browser native API into Webdriver API, can directly manipulate the elements of the browser page, Even operate the browser itself (screenshots, window size, start, close, install plugins, configure certificates, etc.), so just like the real user in action.

composition : Webdriver is designed according to the classic design pattern of server–client, the composition of C/s structure, c. Our test script, S is the remote server

Implementation Principle :

Webdriver bind the target browser to a specific port, launch a browser for a specific service, receive a test script command

The test script sends an HTTP request to the server via Comandexecutor

A simple architecture diagram of Webdriver, as shown in:

Selenium2 principle

Webdriver is designed according to the classic design pattern of server–client:

Server side is remote server, can be any browser: After our script launches the browser, the browser is remote server, its role is to wait for the client to send the request and make the corresponding;

The client side is simply our test code: We test some of the behavior in the code, such as opening the browser, jumping to a specific URL and so on is sent to the server side (that is, the browser) server to accept the request, and do the corresponding action, and returns the execution status, return value and other information in response;

A brief introduction to how Webdriver works:

    • When the browser is started, Selenium-webdriver binds the target browser to a specific port, and the launched browser acts as a remote server for Webdriver.
    • The client (that is, the test script) sends an HTTP request to the sever end with Comandexecutor (communication protocol: The Webdriver wire Protocol, in the body of the HTTP request, Webdriver The wire protocol specifies a JSON-formatted string to tell selenium what we want the browser to do next).
    • The sever side needs to rely on the native browser component, and the command to convert the Web service to the browser native call to complete the operation.

Note:

The Webdriver wire protocol is a protocol defined by selenium's own design, which is very powerful and can operate almost anything from the browser, including opening, closing, maximizing, minimizing, element positioning, element clicking, uploading files, and so on.

The Webdriver wire protocol is generic, meaning that, regardless of firefoxdriver or chromedriver, a Web Service based on this protocol will be launched on a port after startup.

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------

3. When you have a general understanding of selenium, you can begin to learn.

Develop, compose, implement the principle, and then begin to understand the HTML, with the method of positioning the page elements, the method of controlling elements.

4. (STEP1) First you know: What is an element? How to locate a page element? How to control the elements? How do I control the browser?

Eight methods of locating,

5. (step2) Skilled writing page Automation script: encounter complex pages, such as a lot of nesting, know how to jump. Complex elements, how to locate, various page elements of processing, such as tables, drop-down boxes, pop-up boxes and so on.

6. (STEP3) After mastering the above skills, start learning how to separate page elements from code, learn data-driven (TestNG), and how to do automated testing with Excel.

7. (STEP4) Learn how to continuously integrate and how to "automate" automated testing. At the same time, began to learn the automated testing framework, according to the actual situation of the product to write a suitable test framework.

Browser actions

# refresh Driver.refresh () # Forward Driver.forward () # Back Driver.back ()

Get tag Element

# Locate the target element driver.find_element_by_id (' I1 ') by ID # driver.find_element_by_class_name (' C1 ') by locating the target element by classname # Locate the target element driver.find_element_by_name (' N1 ') by using the Name property # Driver.find_element_by_xpath ('//*[@id = ' i1 ') by using XPath to locate the target element # Positioning the target element driver.find_element_by_css_selector (' #i1 ') via CSS selector # by tag name positioning (Note: In a page, the label must be repeated, so do not use this to locate) Driver.find_element_by_tag_name (' input ') # Find element driver.find_element_by_link_text (' login ') by text in tag # Find Driver.find_elements_by_partial_link_text (' record ') by fuzzy matching of text in tags

Get tag elements commonly used a total of 8 positioning methods, and selenium actually provides 18 kinds of positioning methods, there are 8 are the above plural form, here does not introduce, the actual use is not commonly used, there are 2 is actually the above said 16 of the underlying package. A method of invocation that is parameterized.

Cookie manipulation

# based on Cookiekey, get cookie information cookie = Driver.get_cookie (' Cookiekey ') # Get all cookie Information cookies = driver.get_cookies () # Add Cookie , in strict accordance with the format, the cookie key is Name,value for Valuedriver.add_cookie ({' name ': ' tmp ', ' value ': ' 123123123 '}) # Delete all cookie Information driver.delete_all_cookies () # Delete corresponding Cookiedriver.delete_cookie (' UICode ') according to Cookiekey

Window operations

# Get the size of the current browser driver.get_window_size () # Set browser size by pixel driver.set_window_size (' width ', ' height ') # Gets the coordinates of the current window for the location of Windows X,ydriver.get_window_position () # Sets the current window for the location of Windows, X,ydriver.set_window_position (20,20) # Maximizes the current window without the need for a parameter Driver.maximize_window () # Returns the browser handle of the current operation Driver.current_window_handle # returns all browser handles to open the server Driver.window _handles

Intercept current Page

# to get the binary picture data of the current page, you need to write the file Driver.get_screenshot_as_png () # As_png in the upper package, only need to pass in the picture name automatically write the picture Driver.get_screenshot_as_ File (' Filename.png ')

Executing javascript statements

# Execute JavaScript Statement driver.execute_script (' JavaScript commond ') # example: # using JS to manipulate the scrollbar # parameter 1:x  parameter 2:ywindow.scrollto (100,400 );

Close and exit

# When multiple is turned on, close the current page driver.close () # Exit and close all page drivers driver.quit ()

Other

# Back to Page source Driver.page_source # returns the tag title Driver.title # returns the current Urldriver.current_url # gets the browser name such as: Chromedriver.name

Elementapi interface

# depending on the tag property name, get the property Valueelement.get_attribute (' style ') # Enter a string into the input box if the type of input is the file type, you can enter the absolute path of the files to upload the file Element.send_keys ( ) # Clear text content element.clear () # left mouse button click Action Element.click () # Gets the property by its name Element.get_property (' ID ') # Returns whether the element is visible True or falseelemen T.is_displayed () # Returns whether the element is selected True or falseelement.is_selected () # Returns the name of the label element Element.tag_name # Gets the width and height of the current label Element.size # gets The text content of the element Element.text # mimics the return button commit data Element.submit () # Gets the coordinates of the current element element.location # Capture Picture Element.screenshot ()

Common exceptions

nosuchelementexception: no element foundnosuchframeexception: No iframe foundnosuchwindowexception: Window handle not found handle nosuchattributeexception: Property Error noalertpresentexception: No alert pop-up box foundelmentnotvisibleexception: Element not visibleelementnotselectableexception: Element not selectedtimeoutexception: Find element Timeout

Web Automation Test Apocalypse

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.