网页自动化测试开发中记录pytest

4 篇文章 0 订阅
4 篇文章 0 订阅
1切换cmd文件目录C:\Users\14600>D:
D:\>cd D:\worksoftware
D:\worksoftware>
2单个py文件打包成.exe文件1.pyinstaller -F -c (项目主文件)test_01shouye.py
该路径下存在文件名,主项目文件 test_01shouye.py
2.执行spec文件:
pyinstaller -F -c docx2xml.spec
该exe文件已将所有的py文件封装完了,可随处使用
3生成依赖文件requirements.txtpip install pipreqs
pipreqs ./ --encoding=utf8  --force
4清空输入框并且输入内容element.clear()
element.send_keys(text)
5定位显示鼠标悬浮元素from selenium.webdriver.common.action_chains import ActionChains
move = driver.find_element_by_css_selector(
                'div.queryschema-control.ivu-col.ivu-col-span-16 > div > div > div > input')
ActionChains(driver).move_to_element(move).perform()
time.sleep(2)
6上传文件选择的窗让 python 实现在窗口中选择文件的功能
 文件上传窗口处理
通过 pywinauto 选择上传文件
# 点击上传图片按钮
    shoppingmainlibraryico = smpUI.wd.find_element(By.XPATH,
          '//*[@id="__nuxt"]/div/form/div[6]/div/div[2]/div/div/span/button').click()

    # 使用pywinauto来选择文件
    app = pywinauto.Desktop()
    # 选择文件上传的窗口
    dlg = app["打开"]
    # 选择文件地址输入框,点击激活
    dlg["Toolbar3"].click()
    # 键盘输入上传文件的路径
    send_keys("D:\worksoftware\pytest\pytest-auto-zdwy\Files\image")
    # 键盘输入回车,打开该路径
    send_keys("{VK_RETURN}")
    # 选中文件名输入框,输入文件名
    dlg["文件名(&N):Edit"].type_keys("smallp.png")
    # 点击打开
    dlg["打开(&O)"].click()
    time.sleep(3)
    # 随便选一个输入框回车
    smpUI.wd.find_element(By.XPATH, '//*[@id="brief"]/div/div/span/input').send_keys(Keys.ENTER)
7进入iframe界面iframeInstantDeathshopping = smpUI.wd.find_element('xpath', '//*[@id="layui-layer-iframe1"]')
    smpUI.wd.switch_to.frame(iframeInstantDeathshopping)
8退出iframe界面# 定位到iframe
    iframe1 = smpUI.wd.find_element(By.TAG_NAME,'iframe')
    # 句柄切换进iframe
    smpUI.wd.switch_to.frame(iframe1)

# 从 iframe2 切换到上一级 iframe1
    # smpUI.wd.switch_to.parent_frame()


    # smpUI.wd.switch_to.default_content()
9读取Excel文件中的测试数据批量执行def test_excelInstantDeath():
    # 读取Excel文件
    df = pd.read_excel('D:\\worksoftware\\pytest\\pytest-auto-zdwy\\InstantDeathtestcasedate.xlsx')
# 初始化WebDriver
    smpUI.login('zhouzhenzhen', '111111')
    time.sleep(2)
    # 打开网页
    # 点击左侧系统导航栏
    systemInstantDeath = smpUI.wd.find_element(By.XPATH,
                                                  '//*[@id="LAY-system-side-menu"]/li[5]/dl/dd[1]/a')
    smpUI.wd.execute_script("arguments[0].click();", systemInstantDeath)
    time.sleep(2)

    # 进入iframe
    iframeInstantDeath = smpUI.wd.find_element('xpath', '//*[@id="LAY_app_body"]/div[2]/iframe')
    smpUI.wd.switch_to.frame(iframeInstantDeath)

    # 循环处理每行数据
    for index, row in df.iterrows():

        # 输入活动名称
        InstantDeathsift = smpUI.wd.find_element(By.CSS_SELECTOR, 'body > div > form > div > div:nth-child(1) > div > input')
        InstantDeathsift.clear()
        InstantDeathsift.send_keys(row['活动名称'])
        time.sleep(2)

        time.sleep(2)
        if row['状态'] == '进行中':
            # 点击状态选择框
            InstantDeathsiftstate = smpUI.wd.find_element(By.XPATH,
                                                          '/html/body/div/form/div/div[2]/div/div/div/i').click()
            # 选择进行中
            InstantDeathsiftstatejxz = smpUI.wd.find_element(By.CSS_SELECTOR, 'body > div > form > div > div:nth-child(2) > div > div > dl > dd:nth-child(4)').click()
        else:
            # 点击状态选择框
            InstantDeathsiftstate = smpUI.wd.find_element(By.XPATH,
                                                          '/html/body/div/form/div/div[2]/div/div/div/i').click()
            # 选择已结束
            InstantDeathsiftstateyjs = smpUI.wd.find_element(By.CSS_SELECTOR, 'body > div > form > div > div:nth-child(2) > div > div > dl > dd:nth-child(5)').click()
        # 输入活动时间
        InstantDeathsifttimes = smpUI.wd.find_element(By.CSS_SELECTOR, '#date')
        InstantDeathsifttimes.clear()
        InstantDeathsifttimes.send_keys(row['活动时间'])
        time.sleep(2)
        # 点击筛选按钮
        InstantDeathsiftanniu = smpUI.wd.find_element(By.CSS_SELECTOR, 'body > div > form > div > div:nth-child(4) > button:nth-child(1)').click()
        InstantDeathsiftanniu = smpUI.wd.find_element(By.CSS_SELECTOR,
                                                      'body > div > form > div > div:nth-child(4) > button:nth-child(1)').send_keys(Keys.ENTER)
        # 在每次登录完成后,等待页面加载,可以添加一些额外的操作
        smpUI.wd.implicitly_wait(5)  # 等待5秒钟,确保页面加载完成
10远程调试Chrome 浏览器远程调试:启动一个 Chrome 浏览器实例,并在端口上启用了远程调试。远程调试允许外部工具(如 Selenium WebDriver)以编程方式与浏览器实例进行交互。
此命令将 Chrome 配置为通过端口上的 WebDriver(例如 Selenium)进行远程控制,使用特定的用户数据目录进行会话隔离和管理。
C:\Program Files\Google\Chrome\Application>chrome.exe --remote-debugging-port=9222 --user-data-dir=“D:\selenium_chrome” 
11Selenium如何接管已经运行的Chrome浏览器# 接管已经运行的Chrome浏览器
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 以下代码是使用 Python 接管已经打开的浏览器
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
browser = webdriver.Chrome(options=options)
browser.get('https://www.bilibili.com/')
print(browser.title) # 哔哩哔哩 (゜-゜)つロ 干杯~-bilibili
12打开窗口并实现窗口切换#!/usr/bin/env python
# -*- coding: utf-8 -*-
# python + selenium + chrome 如何打开新窗口,并实现窗口切换
# Selenium一直使用同一个浏览器,不重复开浏览器
# 控制已打开的浏览器,切换窗口,在该浏览器打开新的窗口
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.chrome.options import Options
import pyautogui
# 以下代码是使用 Python 接管已经打开的浏览器
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
browser = webdriver.Chrome(options=options)
# browser.get('https://www.bilibili.com/')
browser.get("https://www.taobao.com/")
print(browser.title) # 哔哩哔哩 (゜-゜)つロ 干杯~-bilibili
# browser = webdriver.Chrome()
# browser.set_window_size(900, 900)   # 根据桌面分辨率来定
#
# # 打开淘宝页面
# browser.get("https://www.taobao.com/")
# 获取淘宝页面的 title和handle信息
taobaoWindowHandle = browser.current_window_handle
taobaoTitle = browser.title
print(f"1. taobaoTitle = {taobaoTitle}, taobaoWindowHandle = {taobaoWindowHandle}")
time.sleep(2)
# 打开路由器配置页面 http://172.16.0.1
js = 'window.open("https://cart.taobao.com/cart.htm?spm=a21bo.jianhua/a.1997525049.1.5af92a89mOqGdX&from=mini&ad_id=&am_id=&cm_id=&pm_id=1501036000a02c5c3739");'
# js = 'window.open("http://192.168.1.1");'
browser.execute_script(js)
time.sleep(2)
# 如果没有switch做窗口切换的话,即使有打开新的窗口,浏览器还是会focus在原来的窗口
print(f"2. routing Configure Page had been opened, currentTitle = {browser.title}, currentHandle = {browser.current_window_handle}")
# 拿到所有的窗口
allHandles = browser.window_handles
print(f"3. allHandles = {allHandles}")
# 第一种思路:按照handle,从淘宝窗口切换到路由器配置窗口
# routeWindowHandle = 0
# routeTitle = 'None'
# for handle in allHandles:
#     if handle != browser.current_window_handle:
#         #(无法直接通过输入url访问目标系统) driver.get('https://www.baidu.com') 你这不是进入百度了吗。 那此时的driver就是百度句柄。
#         # 当你再次 driver.get('https://xxx') 产生了2个窗口 那就需要driver.switch_to.window(handles[0])切回百度
#         browser.switch_to.window(handle)
#         routeWindowHandle = browser.current_window_handle
#         routeTitle = browser.title
#         break
# print(f"4. switch to routeWindow, routeWindowHandle = {routeWindowHandle}, routeTitle = {routeTitle}, currentHandle = {browser.current_window_handle}, currentTitle = {browser.title}")

# 第二种思路:当然还有一种方法是根据窗口的title进行切换
# 从路由器配置窗口,切换回淘宝窗口
for handle in allHandles:
    if browser.title.find("百度") == -1:
        browser.switch_to.window(handle)
print(f"5. switch to taobaoWindow, taobaoWindowHandle = {taobaoWindowHandle}, taobaoTitle = {taobaoTitle}, currentHandle = {browser.current_window_handle}, currentTitle = {browser.title}")
time.sleep(5)
# # 第三种思路:按照windowHandle切换,切换到taobao窗口
# browser.switch_to.window(taobaoWindowHandle)
# print(f"6. switch back to taobao window, currentHandle = {browser.current_window_handle}, taobaoWindowHandle = {taobaoWindowHandle}, routeWindowHandle = {routeWindowHandle}")
# time.sleep(10)
# browser.quit()
# browser.execute_script("window.open('https://www.baidu.com/', '_blank');")
# 实现在该浏览器打开新的标签页
# 模拟按 Ctrl + T 在活动窗口(您的浏览器)中打开新选项卡。
pyautogui.hotkey('ctrl', 't')
# Wait briefly for the new tab to open (adjust as needed)
time.sleep(1)
# Example: Navigate to a website in the new tab (you can use pyautogui to type the URL if needed)
pyautogui.typewrite('https://www.bilibili.com/')
pyautogui.press('enter')
13实现在打开的浏览器上打开新的标签页# 实现在该浏览器打开新的标签页
pyautogui.hotkey('ctrl', 't')
# 模拟按 Ctrl + T 在活动窗口(您的浏览器)中打开新选项卡。
time.sleep(1)
pyautogui.typewrite('https://www.bilibili.com/')
pyautogui.press('enter')
14不加载图片,加快访问速度
设置为开发者模式,防止被各大网站识别出来使用了Selenium
 options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) #不加载图片,加快访问速度
 options.add_experimental_option("excludeSwitches",['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
# # prefs = { "profile.managed_default_content_settings.images": 2 } # 设置无图模式
# # options.add_experimental_option("prefs", prefs) # 加载无图模式
15等待购买按钮出现后点击#等待购买按钮出现
                    wait = WebDriverWait(browser, 10)  # 超时时长为10s
                    linkbuy=wait.until(EC.presence_of_element_located((By.ID,'J_LinkBuy')))
                    linkbuy.click()
16刷新页面browser.refresh()  # 刷新页面
17打包成exe文件后报error: unrecognized arguments:--alluredir resultpip3 install six==1.14.0  #six 1.16.0不兼容
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梖梖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值