最简单的Web Monkey 测试范例

昨天学习了随机测试。立即搜索了随机测试的小工具。

发现一个真正的傻瓜是的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 random
import time
from 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应用。

最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取【保证100%免费】

在这里插入图片描述

 这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

软件测试技术交流群社:786229024(里面还有工作内推机会,毕竟我们是关系社会。)

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

面试文档获取方式:

猜你喜欢

转载自blog.csdn.net/wx17343624830/article/details/130019157