Automatic measurement foundation operation

1, Automated testing

1.1 automated testing

Automated testing is the process of transforming human driven testing into machine execution.

1.2 what kind of projects are suitable for automated testing

1) Software requirements change infrequently

2) The project cycle is relatively long (at least one year, preferably self-developed products)

3) Automated scripts can be reused for a long time

1.3 when do automated scripts usually intervene

Generally, it is involved in the system test stage for the regression test of the system

1.4 automated test process

Feasibility analysis - > test requirement analysis - > plan making - > automated use case design (write an automated test separately, or take a part of the functional test as a sample and write a use case test) - > script development - > unattended - > automatically generate an automated report - > foot Book maintenance.

2, Common automated test tools or frameworks

1.selenium
2.Robotframework(RF)
3.Appium
4.QTP(UFT, gradually eliminated after 04)
5. Others (Monkey, monkeyrunner, uiautomation, etc.)

3, Function features and components of selenium

1. Function

Latest version 3.141 0, web-based automation tools, element positioning, window jump, frame processing

2. Features

Open source, free

Support multiple languages: python, java, C#, ruby, php, etc

​ python: find_element_by_id()

Support multiple platforms: windows, linux, mac

Support distributed running test cases: Grid

3. Components

IDE(Firefox plugin)

Webdriver (core component, encapsulating a complete set of APIs for operating the browser)

Grid (distributed)

4, Build python+selenium automation environment

4.1 download the driver of the specified browser and put it in the path of python installation (unzipped file)

4.2 write the following script

from selenium import webdriver
# Load Google browser driver
br = webdriver.Chrome()
# Open Baidu
br.get('https://www.baidu.com')

5, General operation of browser

from selenium import webdriver
from time import sleep
# Open browser
br = webdriver.Chrome()
# Load web page
br.get('https://www.baidu.com')
sleep(3)
# Browser maximization
br.maximize_window()
sleep(3)
# Set browser size
br.set_window_size(1200,880)
# Refresh
sleep(3)
br.refresh()
# return
br.back()
sleep(3)
# forward
br.forward()
sleep(3)
# screenshot
br.get_screenshot_as_file('e:\\error.png')
# Getting URLs is generally used with assertions
print(br.current_url)
# Gets the title of the browser
print(br.title)
# Get web source code
print(br.page_source)
# Close current window
br.close()
# Exit br, close all windows
br.quit()

6, Eight methods of locating elements

F12 right click: check

Premise: unique

6.1 positioning by id
6.2 positioning by name
6.3 positioning by class
6.4 through link_ Textpositioning
6.5 passing partall_ link_ Textpositioning
6.6 passing tag_name positioning
6.7 locating via xpath
1) Absolute path (/): / HTML / body / div / div / div / div / form / span / input
2) Relative path (/ /): / / form/span/input
  1. Locate by element index: (index starts from 1)
6.8 positioning via css

example:

# Quick guide package alt+enter or ctrl+alt + space
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
dr = webdriver.Chrome()
dr.get('https://www.baidu.com')
time.sleep(0.5)
# Locate by id
# dr.find_element_by_id('kw').send_keys('test clouds')
# Locate by name
# dr.find_element_by_name("wd").send_keys('test clouds')
# Pass class_name positioning
# dr.find_element_by_class_name('s_ipt').send_keys('vast clouds')
# Via link_ Text (hyperlink for a tag) positioning
# dr.find_element_by_link_text('news') click()
# Via partail_ link_ Textpositioning (for a tag)
# dr.find_element_by_partial_link_text('New ') click()
# It is located by tag name, which is generally not used. It's hard to be unique
# print(dr.find_element_by_tag_name('form').get_attribute('action'))
# Locate through xpath. Complete complex positioning (xpath,css)
# Absolute path starts with /
# dr.find_element_by_xpath('/html/body/div/div/div/div/div/form/span/input').send_keys('su Bingtian ')
# Relative path starts with / /
# dr.find_element_by_xpath('//form/span[1]/input').send_keys('su Bingtian ')
# Index location
# dr.find_element_by_xpath('//form/span[2]/input').click()
# Attribute positioning
# dr.find_element_by_xpath('//input[@autocomplete="off"]').send_keys('su Bingtian ')
# dr.find_element_by_xpath('//input[@autocomplete="off" and @name="wd"]').send_keys('su Bingtian ', Keys.ENTER)
# Property value of the section
# dr.find_element_by_xpath('//input[starts-with(@autocomplete,"of")]').send_keys('boundless clouds', Keys.ENTER)
# dr.find_element_by_xpath('//input[contains(@class,"pt")]').send_keys('boundless clouds', Keys.ENTER)
# Positioning by text
# dr.find_element_by_xpath('//a[text() = "news"]') click()
# Various xpath combinations
# dr.find_element_by_xpath('//a[contains(text(), "new")]') click()
# dr.find_element_by_xpath('//form[@id="form"]/span[1]/input').send_keys('boundless clouds', Keys.ENTER)
# The wildcard * represents all

# adopt css location(html,javaScript,css,h5+css3),adopt.look for class,adopt#Find id
'''
 1. Absolute path
 2. Relative path(adopt class and id location)
 3. Positioning by attributes
 4. Through partial attribute values
 5. By querying child elements
 6. Find sibling element
'''
# 1. Absolute path (space elements or >)
# dr.find_element_by_css_selector('html body div div div div div form span input').send_keys('su Bingtian ', Keys.ENTER)
# dr.find_ element_ by_ css_ selector('html>body>div>div>div>div>div>form>span>input'). send_ Keys ('su Bingtian ', Keys.ENTER)
# dr.find_element_by_css_selector('html body div.wrapper_new div#head div.head_wrapper.s-isindex-wrap.nologin div div form span input').send_keys('su Bingtian ', Keys.ENTER)

# 2. Relative path (located by class and id)
# dr.find_element_by_css_selector('#kw').send_keys('su Bingtian ', Keys.ENTER)
# dr.find_element_by_css_selector('input#kw').send_keys('su Bingtian ', Keys.ENTER)
# class
# dr.find_element_by_css_selector('input.s_ipt').send_keys('su Bingtian ', Keys.ENTER)

# 3. Attribute value positioning
# dr.find_element_by_css_selector('input[autocomplete="off"]').send_keys('su Bingtian ', Keys.ENTER)
# dr.find_element_by_css_selector('input[@autocomplete="off"][@name="wd"]').send_keys('su Bingtian ', Keys.ENTER)

# 4. The value of some attributes ^ indicates the end, and $indicates the beginning * contains
# dr.find_element_by_css_selector('input[autocomplete^="of"]').send_keys('su Bingtian ', Keys.ENTER)
# dr.find_element_by_css_selector('input[autocomplete$="of"]').send_keys('su Bingtian ', Keys.ENTER)
# dr.find_element_by_css_selector('input[autocomplete*="of"]').send_keys('su Bingtian ', Keys.ENTER)

# 5. Query the child elements and find the child elements through spaces and greater than signs (only one level at a time)
# dr.find_element_by_css_selector('div#s-top-left a:nth-child(3)').click()
# 6. Find sibling elements
dr.find_element_by_css_selector('div#s-top-left a +a').click()

7, Packaging positioning in automation framework

from selenium.webdriver.common.by import By
dr.find_element(By.ID,"kw").send_keys('Vast clouds')

1, Some common methods and attributes of operation elements

import time
from selenium import webdriver


dr = webdriver.Chrome()
dr.get("http://47.107.116.139/shopnc/admin/index.php?act=login&op=login")
time.sleep(2)
dr.find_element_by_id('user_name').send_keys('Default value')
time.sleep(2)
# Clear the input box
dr.find_element_by_id('user_name').clear()
# Enter account number
dr.find_element_by_id('user_name').send_keys('admin')
time.sleep(2)
# Input password
dr.find_element_by_id('password').send_keys('admin123')
print(dr.find_element_by_id('password').get_attribute('name'))
time.sleep(6)
# dr.find_element_by_id('//input[@value = "login"]') click()
dr.find_element_by_id('form_login').submit()





'''
# When a web page has an element (with attribute: disable) that cannot be used, you can use js to execute the attribute of the element, and then use the element
js = "document.getElementById('username').removeAttribute('disabled')"
dr.execute_script(js)
'''

''' 
# When some elements of the web page are hidden and invisible, you can execute js, and then you can operate
js = "document.getElementById('username').style.display=''"
dr.execute_script(js)
'''

'''
# Judge whether an item in the drop-down box is selected
print(dr.find_element_by_xpath('//select/option(2)').is_selected())

# Determine whether the drop-down box is available
dr.find_element_by_xpath('//select/option(2)').is_enabled()

# Determine whether the drop-down box is hidden
dr.find_element_by_id('//select/option(2)').is_displayed()
'''

2, Mouse and keyboard operation

Use actioncharins (action chain) to execute through perform().

2.1 mouse related operations

Left click or actionchains (DR) click(ele2). perform()

dr = webdriver.Chrome()
dr.get('https://www.baidu.com')
ele = dr.find_element_by_id('kw')
ele.send_keys('Vast clouds')
ele2 = dr.find_element_by_id('su')
# single click
ActionChains(dr).click(ele2).perform()

Right click

ActionChains(dr).context_click(ele).perform()

double-click

# double-click
ActionChains(dr).double_click(ele).perform()

Long press

ActionChains(dr).click_and_hold(ele).perform()

suspension

# suspension
xinwen = dr.find_element_by_link_text('Journalism')
ActionChains(dr).move_to_element(xinwen).perform()
# # Coordinates of suspended "news"
ActionChains(dr).move_by_offset(xinwen.location['x'],xinwen.location['y']).perform()

3, Common automation scenario operations

3.1 verification code cookie bypass

1) Let's develop shielding verification code

2) Set universal code in the background

3) Log in by bypassing the verification code through a cookie

Browser

What is a cookie?

It is a dictionary format string stored on the client

Cookie: what is the difference between cookie, seesion and token?

What is session?

Session is the data stored in the server, and session can be passed through cookie s. The name is usually xxSessionID

1. Know the correct user and password

2. Get cookie Dictionary (successful login)

		Interface, APP

What is a token?

A token is an authentication code: it is passed through the login interface.

3.2 frame/iframe framework processing

 dr.switch_to.frame('workspace')
        # Jump out of frame
        # dr.switch_to.default_content()

3.3 drop down box processing

 # Drop down box
        sel = Select(dr.find_element(By.NAME,'search_brand_id'))
        # How to get the value of the drop-down box 1: select the value in the drop-down box by value
        sel.select_by_value('80')
        # How to get the value of the drop-down box 2: get the value through the index, and the index starts from 0
        sel.select_by_index('0')
        dr.find_element(By.ID,'ncsubmit').click()

3.4 file upload processing

 # File upload processing
        dr.find_element(By.ID, '_pic').send_keys('c:/users/Administrator/Desktop/b.jpg')
        sleep(2)
        dr.find_element('pic_submit').click()
        sleep(2)
        dr.find_element(By.ID, 'submit').click()

3.5 locating a set of elements

 # 3. Locate a set of elements
        checkboxList = dr.find_elements(By.NAME,'id[]')
        checkboxList[0].click()

3.6 browser multi window processing

 # 5. Browser multi window processing
        # Gets the handle of the current window
        currentHandle = dr.current_window_handle
        # Get handles to all windows
        allHandle = dr.window_handles
        for i in allHandle:
            if i!= currentHandle:
                # Navigate to the new window through the handle of the window
                dr.switch_to.window(i)
        # Assert to see if the page title is consistent
        self.assertEqual(dr.title,"Page title")
        sleep(10)
        # Return to old window
        dr.switch_to.window(currentHandle)

3.7 scroll bar processing

# Scroll bar processing
    def test2_gundongtiao(self):
        global dr
        dr = webdriver.Chrome()
        dr.get('http://baidu.com')
        dr.implicitly_wait(10)
        js = 'document.documentElement.scrollTop=1000'
        dr.execute_script(js)

Keywords: Selenium software testing

Added by DChiuch on Thu, 23 Dec 2021 16:29:23 +0200