Selenium web test

Posted by ali_p on Tue, 25 Jan 2022 05:36:47 +0100

I introduce

        1.Selenium is an open source testing tool for Web applications. It supports Firefox, IE, Chrome browsers, Java, C#, Ruby, Python and other mainstream languages.

        2. Composition: selenium IDE+webdriver, etc

        3. effect:

(a)selenium IDE: script recording tool for UI interface, limited processing logic and keyword driven mode.

(b) various webdriver packages improve the efficiency of browser testing for browser API(Application Program Interface).

II Page element positioning and operation

        1. Page elements refer to all elements that can be displayed in the browser, including text boxes, buttons, videos and pictures.

        2. Main steps of automated testing:

                1. Locate the object to be executed in some way, target;

                2. What operations have been performed on this object (command);

                3. Assign a value to the positioned element

                4. Add assertion action

        3. There are eight ways to locate elements:

WebDriver element location method and python implementation
WebDriver element positioning methodpython methodWebDriver element positioning methodpython method
idfind_element_by_id()link textfind_element_by_link_text( )
namefind_element_by_name()partial link textfind_element_by_partial_link_text( )
class namefind_element_by_class_name( )xpathfind_element_by_xpath( )
tag namefind_element_by_tag_name( )css selectorfind_element_by_css_selector( )

III Actions on page elements:

clear(): clear text

        send_ Keys (value): simulate keyboard input. In addition to sending letters, you can also send shortcut keys;

click(): click elements, such as buttons, hyperlinks, radio boxes and check boxes;

        Current_url: get the current url interface, which can be used to make assertions.

Title: returns the title of the current page

Text: get the text box displayed on the page (prompt box, warning box)

        get_ Attribute (name): get the attribute value. The value of the text box uses the value attribute value.

        is_displayed(): sets whether the element is visible to the user.

#Guide the package, create a browser object, and obtain the url address link_test applies to hyperlinks
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
#Locate using prime attribute: / / input[@id='kw ']
#driver.find_element_by_xpath("//input[@id='kw']").send_keys("selenium")
#driver.find_element_by_xpath("//input[@id='su']").click()
#time.sleep(2)
#driver.quit()
 #Copy using the xpath method on the web page
# driver.find_element_by_xpath('//*[@id="kw"]').send_keys("selenium")
# driver.find_element_by_xpath('//*[@id="su"]').click()
# time.sleep(2)
# driver.quit()
#css class selector positioning Baidu text box:
# . represents a class selector s_ipt
#  #express Id Selector,#kw,#su
# driver.find_element_by_css_selector(".s_ipt").send_keys("selenium")
# # driver.find_element_by_id("su").click
# driver.find_element_by_css_selector("#su").click
# time.sleep(3)
# driver.quit()
#Obtain the url address after the jump and judge whether it conforms to the result
# driver.find_element_by_link_text("news") click()
# url=driver.current_url
# print(url)
# time.sleep(3)
# driver.quit()
# if url=="https://www.baidu.com/":
#     print('page jump succeeded ')
# else:
#     print('jump failed ')
#Get the text of "Baidu hot search":
# 1 locate xpath: / / * [@ id = "s-hotsearch wrapper"] / div / a [1] / div
#Operation get text: Text
 # wenben =driver.find_element_by_xpath('//*[@id="s-hotsearch-wrapper"]/div/a[1]/div') .text
 # print(wenben)
 # driver.quit()

IV Browser operation

        1. Browser window size: Set_window_size()

        2. Browser forward and backward back() and forward()

        3. Browser refresh ()

        4. Screenshot operation: driver save_ screenshot()

        5. Simulation browser closes

        6.quit() closes all pages and close() closes individual pages

#Guide the package, create a browser object, and obtain the url address
from selenium import webdriver
import time
#driver is a variable
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
# #Set window size 1080 * 600
# #windowhandle is a handle
# driver.set_window_size(1080,600)
# #Maximize window display
# driver.maximize_window()
# #Minimize serial port
# driver.minimize_window()
# #Add element with minimum
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.maximize_window()
#Due to the handle problem, the forward and backward of the page are based on baidu home page.
# driver.find_element_by_link_text("news") click()
# time.sleep(2)
# driver.back()
# time.sleep(2)
# driver.forward()
# time.sleep(2)
# #Refresh
# driver.refresh()
driver.find_element_by_link_text("Journalism").click()
time.sleep(2)

#driver.save_screenshot(r"D:\sss.png")
driver.get_screenshot_as_file("{}.{}".format("d:/ddd","png"))#Two curly braces occupy positions, corresponding to "D: / DDD" and "PNG" respectively
#Close browser
driver.quit()
#Close Baidu home page
driver.close()

V Multi window switch (two pages)

Realize the following functions:

        1. Open Taobao

        2. Click Open poly cost-effective to enter the poly cost-effective page

        3. Click the login button to enter the login page

        4. Return to cost-effective button

        5. Close the cost-effective page

Note: each page of the handle has a value, which is unique to a page and an identification of the page. The driver only controls the page of the handle. Therefore, the following steps need to be implemented:

1. Get the handle of the page: handles = driver current_ window_ handle

2. Assign the handle of the page to be operated to the operation object: driver switch_ to. window(handles[1])

from selenium import webdriver
import time
driver=webdriver.Chrome()
#Open the specified web page
driver.get("https://www.taobao.com")
time.sleep(1)
#Get current handle
handle1=driver.current_window_handle
print(handle1)
#Output: cdwindow-9039c3f5ea24c378d09a750f3324bc
driver.find_element_by_link_text('Poly cost-effective').click()
time.sleep(1)
handles=driver.window_handles
print(handles)
#Output: ['cdwindow-909039c3f5ea24c378d09a750f3324bc ','cdwindow-fb22d870def596e42c31c561963be9bc']

#switch windows
driver.switch_to.window(handles[1])
driver.find_element_by_link_text('Please login').click()
time.sleep(2)
driver.back()
driver.close()

Vi Mouse operation

        1. Mouse operations: click, right-click, double-click, hover and drag

        2. Guide package required: from selenium webdriver. common. action_ chains import ActionChains

        3. Common methods:

(1) perform(): execute all actions stored in ActionChains

                (2)cintext_click(): right click

                (3)double_click: double click

                (4)drag_and_drop(): drag

                (5)move_to_element(): mouse over

        4. Function 1: open Baidu web page, maximize it, and hover over more pages. At this time, the corresponding content will appear.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
#Controls the mouse over settings button
setButton=driver.find_element_by_link_text("more")
#Encapsulate the operation behavior of the setting button into ActionChains
ActionChains(driver).move_to_element(setButton).perform()

        5. Function 2: right click in Baidu text box

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
webEdit=driver.find_element_by_id("kw")
ActionChains(driver).context_click(webEdit).perform()
time.sleep(5)
driver.quit()

VII Keyboard operation

        1. Keyboard operation: the key() class provides all keyboard operations, keyboard input, and key combinations, such as ctrl+A

        2. Guide Package: from selenium webdriver. common. keys import Keys

        3. Keyboard case:

                1. Baidu "selenium";  

                2. Delete redundant m;

                3. Tutorial on entering spaces“

                4.ctrl+a, select all text;

                5.ctrl+x to cut the selected content;  

                6.ctrl+V, paste the copied content;

                7. Press enter instead of clicking to complete the search;

                8. Launch browser

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
#1. Baidu "selenium";
driver.find_element_by_id("kw").send_keys("seleniumm")
# "2. Delete the redundant m;
time.sleep(1)
driver.find_element_by_id("kw").send_keys((Keys.BACK_SPACE))
time.sleep(1)
# 3. After entering "space tutorial", send the string again, which is spliced with the previous one
driver.find_element_by_id("kw").send_keys(" course")
time.sleep(1)
# 4.ctrl+a, select all text;
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"A")
time.sleep(1)
# 5.ctrl+x to cut the selected content;
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"x")
time.sleep(1)
# 6.ctrl+V, paste the copied content;
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"v")
time.sleep(1)
# 7. Press enter instead of clicking to complete the search;
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
time.sleep(3)
# 8. Launch browser
driver.quit()

VIII Warning box operation

        1. Mode window: other operations can be performed only when the window is closed. Non mode window: other operations are not affected.

        2. Warning box - alter is a mode box. The driver object is in the current window, but not in the alter window. It cannot be located in the window.

        3. operation

                 1. Use driver switch_ to. alert

                 2. Operation on alter warning box:

Text: returns (gets) the text information in alter/conform/prompt

accept(): accept the existing warning box, that is, click OK.

Disass(): discard the existing warning button and cancel

                         send_keys(keyToSend): sends text to the warning box

        4. Function: realize settings in Baidu page - > relevant settings of search settings - > Save Settings - > pop up alter warning box and accept it.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
#Control the mouse over the "Settings" button, otherwise the contents inside cannot be clicked
setButton=driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')
#Encapsulate the operation behavior of the setting button into ActionChains
ActionChains(driver).move_to_element(setButton).perform()
time.sleep(3)
#Search settings button
#Mode 1:
# driver.find_element_by_xpath('//*[@id="s-user-setting-menu"]/div/a[1]').click()
# #Method 2: use link_list, but this method needs to use a long extension time to make the interface display complete: time sleep(3)
driver.find_element_by_link_text("Search settings").click()
time.sleep(1)
#Click Save settings
driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]').click()
time.sleep(1)
dd=driver.switch_to.alert
#Get text information
tt=dd.text
print(tt)
#Accept text box
dd.accept()
driver.quit()

IX Multi form operation

        1. Existing problems: in Web applications, we often encounter the application of frame/iframe form nested pages. Webdriver can only find the identification and positioning of elements on one page, and cannot directly locate the elements on the frame/iframe form nested pages.

        2. Method: multi form processing is involved. The outer elements can be located, but the inner elements cannot be recognized #switch_to.frame() defaults to ID or name

        3. Implementation functions:

                1. Open Tencent web page http://www.qq.com

                2. Click the email icon

                3. enter one user name

                4. Input password

                5. Click login

                6. Close browser

from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.qq.com")
driver.find_element_by_link_text("Qmail").click()
#Jump to the mailbox login interface and design the processing of multiple windows
handls=driver.window_handles
driver.switch_to.window(handls[1])
driver.switch_to.frame('login_frame')
driver.find_element_by_xpath('//*[@id="u"]').send_keys('1687228614')
time.sleep(2)
driver.find_element_by_xpath('//*[@id="p"]').send_keys('wtq1029zdc')
driver.find_element_by_xpath('//*[@id="login_button"]').click()
time.sleep(2)
driver.quit()

Topics: software testing chrome