大家好,我是阿萨,昨天学习了随机测试。立即搜索了随机测试的小工具。发现一个真正的傻瓜是的chrome 插件来做随机测试的。1. Chrome 插件欢迎从如下链接安装:https://chromeload.com/extension-41598-monkey-testing
安装完,开始应用,点开插件, 点Start 按钮。 随机测试就开始了。然后再点一下stop 就结束了。2. Playwright 可以测试输入框和按钮的小代码。使用Playwright 写一个小程序,随机生成小程序。
from playwright.sync_api import Playwright, sync_playwright def fill_and_submit_form(page): # Find all input fields on the page input_fields = page.query_selector_all('input') # Fill in each input field with a unique value for i, input_field in enumerate(input_fields): input_field.fill(f'test{i}') # Find the submit button and click it submit_button = page.query_selector('button[type="submit"]') submit_button.click() with sync_playwright() as playwright: # Launch the browser and create a new page browser = playwright.chromium.launch() page = browser.new_page() # Navigate to the target web page page.goto('https://example.com') # Fill in and submit the form on the page fill_and_submit_form(page) # Close the browser browser.close()
3. 可以测试Web页面所有组件的Monkey 测试工具使用如下代码可以测试所有页面上的随机组件。
import randomimport timefrom playwright.sync_api import Playwright, sync_playwright def monkey_test_page(page, num_iterations): # Define a list of action functions to perform on the page actions = [click_random_element, fill_random_input, navigate_random_link] for i in range(num_iterations): # Randomly choose an action function from the list action = random.choice(actions) # Perform the chosen action on the page action(page) # Wait for a random amount of time between actions time.sleep(random.uniform(0.5, 1.5)) def click_random_element(page): # Find all clickable elements on the page clickable_elements = page.query_selector_all('a, button') # Randomly choose an element and click it random_element = random.choice(clickable_elements) random_element.click() def fill_random_input(page): # Find all input fields on the page input_fields = page.query_selector_all('input[type="text"], input[type="password"], textarea') # Randomly choose an input field and fill it with a random string random_input = random.choice(input_fields) random_input.fill(''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=10))) def navigate_random_link(page): # Find all links on the page links = page.query_selector_all('a') # Randomly choose a link and navigate to it random_link = random.choice(links) page.goto(random_link.get_attribute('href')) with sync_playwright() as playwright: # Launch the browser and create a new page browser = playwright.chromium.launch() page = browser.new_page() # Navigate to the target web page page.goto('https://example.com') # Perform monkey testing on the page for 10 iterations monkey_test_page(page, 10) # Close the browser browser.close()
赶紧用起来吧,来测试下自己的Web应用。