MindMap Gallery Selenium mind map
This is a mind map about Selenium. Selenium is a tool for web application testing. This mind map includes the first automation script, unittest testing framework, etc. Hope this helps!
Edited at 2023-11-03 10:47:21This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
Selenium
1. First automation script
from selenium import webdriver from time import sleep browser = webdriver.Chrome( ) browser.get('http://127.0.0.1/upload/forum.php') sleep(5) browser.quit()
#implicit wait browser.implicitly_wait(5)
2. WebDriver API
position
positioned element
1. id positioning
HTML stipulates that the id attribute must be unique within an HTML document
browser.find_element_by_id('ls_password').send_keys('123456')
2. name positioning
browser.find_element_by_name('username').send_keys('admin')
3. class positioning
browser.find_element_by_class_name('pn').click( )
4. tag name positioning
browser.find_element_by_tag_name('input').send_keys('123456')
5. link positioning
Specifically used to locate text links
browser.find_element_by_link_text('Default section').click( )
6. partial link positioning
Partial link positioning is a supplement to the link element. Sometimes the text link is relatively long. We can position part of the text link.
browser.find_element_by_partial_link_text('default').click( )
7. xpath positioning
//Indicates relative path
[@id=‘ls_username’] means that the id of the element is equal to ‘ls_username’
browser.find_element_by_xpath('//input[@name="password"]').send_keys('123456')
browser.find_element_by_xpath('//button[@class="pn vm"]').click( )
Attributes combined with hierarchical positioning
text Gets the text of the element <start tag>text (text)</end tag>
a = browser.find_element_by_xpath('//button[@class="pn vm"]/em').text print(a)
browser.find_element_by_xpath("//div/a[@class='xi2 xw1']").click( )
Fuzzy positioning
text( ) function
browser.find_element_by_xpath('//a[text()="Exit"]').click( )
contains() function
browser.find_element_by_xpath('//a[contains(@href,"logout")]').click()
starts-with() function
browser.find_element_by_xpath('//a[starts-with(@href,"member.php?mod=l")]').click( )
8. CSS Selector positioning
Use # for id
browser.find_element_by_css_selector('#ls_password').send_keys('123456')
browser.find_element_by_css_selector('#ls_username').send_keys('admin')
Use "." for class
browser.find_element_by_css_selector('.pn.vm').click( )
9. XPath vs. CSS
Position a group of elements
Locate a set of elements and return a list
control
Control browser
1. Control browser window size
set_window_size( )
driver.set_window_size(480,800)
browser.set_window_size(480,600)
2. maximize browser
maximize_window( )
driver.maximize_window( )
browser.maximize_window()
3. Control browser progress
back( )
driver.back()
browser.back()
forword( )
driver.forward( )
browser.forward()
4. Simulate browser refresh
refresh( )
driver.refresh( )
browser.refresh()
Simple element operations
from selenium import webdriver from time import sleep browser = webdriver.Chrome( ) browser.get('http://127.0.0.1/upload/forum.php') sleep(2) #Locate the username input box username=browser.find_element_by_id('ls_username') username.send_keys('admin') sleep(1) #clear text username.clear() #Get other attributes print('name',username.get_attribute('name')) print('class',username.get_attribute('class')) print('tabindex',username.get_attribute('tabindex')) #is_displayed() Returns whether the result of the element is visible. If it is visible, it returns True, otherwise it returns False. #Register Now next to #Login True print(browser.find_element_by_xpath("//td/a[@class='xi2 xw1']").is_displayed( )) #Register now under quick navigation, False print(browser.find_element_by_xpath("//div/a[@class='xi2 xw1']").is_displayed( )) sleep(5) browser.quit( )
Drop down list operations
from selenium.webdriver.support.select import Select #The operations of the drop-down list are encapsulated in the Select class #Generate a Select object, the parameter is the drop-down list to be selected #Automatic import, place the cursor on the Select class, then press Alt Enter, select the Select class to import s = Select(browser.find_element_by_id('education'))
call method
Select based on index
s.select_by_index(2) #Select Doctor
Select based on value, the value of the value attribute in HTML
s.select_by_value("2") #Select university
Select based on visible text, visible list items
s.select_by_visible_text("Doctor")
Mouse operation
The methods for mouse operations in WebDriver are encapsulated in the ActionChains class
perform action
perform( )
right click
context_click( )
double click
double_click( )
hover
move_to_element( )
drag
drag_and_drop(source,target)
Keyboard operation
WebDriver uses the Keys() class to operate all keys on the keyboard The send_keys() method can be used to simulate input keys on the keyboard.
Delete key (BackSpace)
send_keys(Keys.BACK_SPACE)
Space bar (Space)
send_keys(Keys.SPACE)
Enter key
send_keys(Keys.ENTER)
Tab key
send_keys(Keys.TAB)
Select all (Ctrl A)
send_keys(Keys.CONTROL,‘a’)
Copy (Ctrl C)
send_keys(Keys.CONTROL,‘c’)
Paste (Ctrl V)
send_keys(Keys.CONTROL,‘v’)
Frame switching
In web applications, we often encounter applications with frame/iframe nested pages.
WebDriver provides the switch_to.frame method to switch the currently positioned subject to the frame/iframe embedded page.
The switch_to.default_content() method jumps out of the current frame
Window switching
WebDriver provides the switch_to.window() method to switch between different windows.
current_window_handle: Get the current window handle
window_handles: Returns the handles of all windows, which exist in the list
Pop-up box operation
Switch to popup
switch_to.alert
Confirm button
accept()
Cancel button
dismiss()
Execute JS
WebDriver does not provide a browser scroll bar operation method. In this case, you can use JavaScript to control the scroll bar.
WebDriver provides the execute_script() method to execute JS code
window.scrollTo(x,y) is used in JavaScript to set the horizontal and vertical positions of the browser scroll bar. The first parameter represents the horizontal left spacing, and the first parameter represents the vertical top margin.
3. unittest testing framework
effect
Organization and management of automation use cases
Automated use case execution
Test result statistics and test report generation
Unittest is a unit testing framework for Python. It is not only suitable for unit testing, but also for the development and execution of WEB automated test cases.
Unittest can organize and execute test cases, and provides a wealth of assertion methods to determine whether the test cases pass, and finally generate test results.
TestCase
An instance of TestCase is a test case. This includes setting up the preparation environment before testing (setUp), executing the test code (run), and restoring the environment after testing (tearDown). A test case is a complete test unit. By running this test unit, a certain problem can be verified
TestSuite
Multiple test cases are gathered together to form TestSuite
TestRunner
Used to execute test cases, execute test suite/test case through the run() method provided by the TestRunner class
TestLoader
Load test cases
4. practise
Sign up now
from selenium import webdriver from time import sleep import random import time browser = webdriver.Chrome( ) browser.get('http://127.0.0.1/upload/forum.php') # i = random.randint(1,1000000000) i = time.time( ) username = 'test' str(int(i)) print(username) #LocationRegister now browser.find_element_by_link_text('Register now').click( ) #Enter your user name browser.find_element_by_id('Zp0luO').send_keys(username) #enter password browser.find_element_by_id('g033gz').send_keys('123456') #Confirm Password browser.find_element_by_id('w9kNs1').send_keys('123456') #email browser.find_element_by_id('7uxU99').send_keys(username '@qq.com') sleep(5) #Click to submit browser.find_element_by_id('registerformsubmit').click( ) sleep(5) browser.quit( )
test_login.py
import unittest from selenium import webdriver from time import sleep class LoginTestCase(unittest.TestCase): #Equivalent to preset conditions, executed before the use case def setUp(self): #self attribute global variable self.browser = webdriver.Chrome() self.browser.maximize_window() self.browser.implicitly_wait(5) self.browser.get('http://127.0.0.1/upload/forum.php') #Equivalent to run, use admin to log in, equivalent to 1 test case def test_login_admin(self): #Assignment simplified browser = self.browser browser.find_element_by_id('ls_username').send_keys('admin') browser.find_element_by_id('ls_password').send_keys('123456') browser.find_element_by_css_selector('button.pn.vm').click() sleep(3) #Get username username = browser.find_element_by_css_selector('a[title="Visit my space"]').text self.assertEqual('admin', username) #Log in using test1, the second test case def test_login_test1(self): # Simplify assignment browser = self.browser browser.find_element_by_id('ls_username').send_keys('test1') browser.find_element_by_id('ls_password').send_keys('123456') browser.find_element_by_css_selector('button.pn.vm').click() sleep(3) # Get username username = browser.find_element_by_css_selector('a[title="Visit my space"]').text self.assertEqual('test1', username) #Cleanup actions after the use case is executed def tearDown(self): sleep(3) self.browser.quit() if __name__ == '__main__': unittest.main()
test_post.py
import unittest from selenium import webdriver from time import sleep,time from selenium.webdriver import ActionChains class PostTestCase(unittest.TestCase): def setUp(self): self.browser = webdriver.Chrome() self.browser.maximize_window() self.browser.implicitly_wait(5) self.browser.get('http://127.0.0.1/upload/forum.php') #Test cases must start with test #Quickly post def test_post1(self): #Log in browser = self.browser browser.find_element_by_id('ls_username').send_keys('admin') browser.find_element_by_id('ls_password').send_keys('123456') browser.find_element_by_css_selector('button.pn.vm').click() #pause sleep(1) # Click on the default section browser.find_element_by_link_text('Default section').click() # Enter title title = 'test' str(int(time())) browser.find_element_by_id('subject').send_keys(title) # Input content browser.find_element_by_id('fastpostmessage').send_keys(title) # Click to post browser.find_element_by_id('fastpostsubmit').click() #Get post title subject = browser.find_element_by_id('thread_subject').text self.assertEqual(subject,title) #Post after clicking the post button def test_post2(self): # Log in browser = self.browser browser.find_element_by_id('ls_username').send_keys('admin') browser.find_element_by_id('ls_password').send_keys('123456') browser.find_element_by_css_selector('button.pn.vm').click() # pause sleep(1) # Click on the default section browser.find_element_by_link_text('Default section').click() # Move the mouse to the post button post_button = browser.find_element_by_id('newspecial') post_link = browser.find_element_by_css_selector('ul#newspecial_menu>li>a') ActionChains(browser).move_to_element(post_button).click(post_link).perform() # Enter title title = 'test' str(int(time())) browser.find_element_by_id('subject').send_keys(title) #Switch frame browser.switch_to.frame('e_iframe') # Input content browser.find_element_by_tag_name('body').send_keys(title) # Get out of the box browser.switch_to.default_content() # Click to post browser.find_element_by_id('postsubmit').click() #Get title # Get post title subject = browser.find_element_by_id('thread_subject').text self.assertEqual(subject, title) def tearDown(self): self.browser.quit()