306 lines
14 KiB
Python
306 lines
14 KiB
Python
import time
|
||
import os
|
||
import json
|
||
import random
|
||
from tests.base.base_page import BasePage
|
||
from tests.logger import get_logger
|
||
|
||
logger = get_logger("ImageGenerationPage")
|
||
|
||
class ImageGenerationPage(BasePage):
|
||
"""图像生成页面 POM"""
|
||
|
||
MENU_TEXT = "图像生成"
|
||
|
||
def __init__(self, page):
|
||
self.page = page
|
||
|
||
def navigate_to(self):
|
||
"""进入图像生成页面"""
|
||
logger.info("正在切换到图像生成页面...")
|
||
self.smart_click(self.MENU_TEXT)
|
||
try:
|
||
self.page.wait_for_selector("text=开启新对话", timeout=10000)
|
||
except:
|
||
pass
|
||
time.sleep(2)
|
||
|
||
def start_new_conversation(self):
|
||
"""点击开启新对话"""
|
||
logger.info("正在点击 [开启新对话] 按钮...")
|
||
try:
|
||
btn = self.page.locator("button", has_text="开启新对话").first
|
||
btn.click()
|
||
time.sleep(1)
|
||
except Exception as e:
|
||
logger.warning(f"无法定位开启新对话按钮: {e}")
|
||
self.page.evaluate("""() => {
|
||
const btns = Array.from(document.querySelectorAll('button'));
|
||
const btn = btns.find(b => b.innerText.includes('开启新对话'));
|
||
if (btn) btn.click();
|
||
}""")
|
||
time.sleep(1)
|
||
|
||
def input_random_prompt(self, config_path):
|
||
"""从配置文件读取 variants 并随机输入到文本框"""
|
||
logger.info(f"正在从 {config_path} 获取随机提示词...")
|
||
try:
|
||
with open(config_path, 'r', encoding='utf-8') as f:
|
||
data = json.load(f)
|
||
variants = data.get("variants", [])
|
||
if not variants:
|
||
raise ValueError("variants 列表为空")
|
||
prompt_text = random.choice(variants)
|
||
except Exception as e:
|
||
logger.error(f"读取提示词配置失败: {e}")
|
||
prompt_text = "默认一张高品质摄影人像"
|
||
|
||
logger.info("正在将提示词填入输入框...")
|
||
try:
|
||
textarea = self.page.locator("textarea[placeholder*='描述你想生成的图片']").first
|
||
textarea.fill(prompt_text)
|
||
except Exception as e:
|
||
logger.warning(f"通过 locator 填入失败,尝试 JS 填入: {e}")
|
||
self.page.evaluate("""(text) => {
|
||
const tas = Array.from(document.querySelectorAll('textarea'));
|
||
const ta = tas.find(t => t.placeholder && t.placeholder.includes('描述'));
|
||
if (ta) {
|
||
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;
|
||
nativeInputValueSetter.call(ta, text);
|
||
ta.dispatchEvent(new Event('input', { bubbles: true }));
|
||
ta.dispatchEvent(new Event('change', { bubbles: true }));
|
||
}
|
||
}""", prompt_text)
|
||
time.sleep(1)
|
||
return prompt_text
|
||
|
||
def start_generation(self):
|
||
"""点击执行(发送)按钮"""
|
||
logger.info("正在点击执行(发送)按钮...")
|
||
try:
|
||
# 图标带有 .pi-send
|
||
btn = self.page.locator("button:has(.pi-send)").first
|
||
btn.click()
|
||
except Exception as e:
|
||
logger.warning(f"通过 locator 点击发送按钮失败,尝试 JS 点击: {e}")
|
||
self.page.evaluate("""() => {
|
||
const icon = document.querySelector('.pi-send');
|
||
if (icon && icon.closest('button')) {
|
||
icon.closest('button').click();
|
||
} else if (icon) {
|
||
icon.click();
|
||
}
|
||
}""")
|
||
time.sleep(2)
|
||
|
||
def wait_for_image_generated(self, timeout=300):
|
||
"""
|
||
等待图片生成完成。
|
||
检测策略(任意一条命中即视为成功):
|
||
1. 页面出现"已生成"文字(最可靠,不受懒加载影响)
|
||
2. 页面出现尺寸合格的 img 元素(兜底)
|
||
"""
|
||
logger.info(f"⏳ 等待图片生成 (最长等待 {timeout} 秒)...")
|
||
start_time = time.time()
|
||
last_log = start_time
|
||
|
||
while time.time() - start_time < timeout:
|
||
elapsed = int(time.time() - start_time)
|
||
|
||
if time.time() - last_log >= 30:
|
||
logger.info(f" ⏱️ 已等待 {elapsed}s,继续监听图片生成...")
|
||
last_log = time.time()
|
||
|
||
try:
|
||
result = self.page.evaluate("""() => {
|
||
// 策略1:页面文字出现"已生成"(如「图片已生成 (1张)」)
|
||
const bodyText = document.body.innerText || '';
|
||
if (bodyText.includes('已生成')) return { found: true, by: 'text' };
|
||
|
||
// 策略2:找到尺寸合格的 img(兼容 blob / base64 / CDN URL)
|
||
const imgs = Array.from(document.querySelectorAll('img'));
|
||
const genImgs = imgs.filter(img => {
|
||
const src = img.src || '';
|
||
// 跳过明显的 UI 图标
|
||
if (src.includes('avatar') || src.includes('logo') ||
|
||
src.includes('favicon')) return false;
|
||
// 有尺寸的大图
|
||
if (img.naturalWidth > 100 && img.naturalHeight > 100) return true;
|
||
// blob / base64 也算
|
||
if (src.startsWith('blob:') || src.includes('base64')) return true;
|
||
return false;
|
||
});
|
||
if (genImgs.length > 0) return { found: true, by: 'img' };
|
||
|
||
return { found: false };
|
||
}""")
|
||
|
||
if result and result.get("found"):
|
||
logger.info(f"✨ 耗时 {elapsed}s,检测到图片生成完成!(by={result.get('by')})")
|
||
return True
|
||
except Exception:
|
||
pass
|
||
|
||
time.sleep(3)
|
||
|
||
logger.error(f"❌ 等待结果超时 ({timeout}s),未能检测到生成的图片")
|
||
return False
|
||
|
||
|
||
def click_quote_on_image(self):
|
||
"""
|
||
定位最新生成的图片,悬停触发工具栏,点击「设为参考图」按钮。
|
||
修复:不再依赖 <main> 标签,改为 JS 筛选最大/最新的图片。
|
||
"""
|
||
logger.info("🖱️ 悬停到生成图片并点击 [设为参考图] 按钮...")
|
||
try:
|
||
# Step 1: JS 找到最新生成图片的位置(排除小图标/头像)
|
||
img_rect = self.page.evaluate("""() => {
|
||
const imgs = Array.from(document.querySelectorAll('img'));
|
||
// 筛选"大图"(生成图),按出现顺序取最后一个
|
||
const genImgs = imgs.filter(img => {
|
||
const src = img.src || '';
|
||
if (src.includes('avatar') || src.includes('logo') ||
|
||
src.includes('favicon')) return false;
|
||
return (img.naturalWidth > 100 && img.naturalHeight > 100) ||
|
||
src.startsWith('blob:') || src.includes('base64');
|
||
});
|
||
if (genImgs.length === 0) return null;
|
||
const last = genImgs[genImgs.length - 1];
|
||
const r = last.getBoundingClientRect();
|
||
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
||
}""")
|
||
|
||
if img_rect:
|
||
# Step 2: 用 mouse.move 精确悬停到图片中心,触发浮动工具栏
|
||
self.page.mouse.move(img_rect["x"], img_rect["y"])
|
||
time.sleep(1.0) # 等待工具栏动画浮出
|
||
|
||
# Step 3: 点击「设为参考图」按钮(三层定位)
|
||
# 层1:title 属性
|
||
quote_btn = self.page.locator('button[title="设为参考图"]').first
|
||
if quote_btn.is_visible():
|
||
quote_btn.click()
|
||
logger.info("✅ 设为参考图按钮点击成功(title 定位)")
|
||
else:
|
||
# 层2:.pi-bookmark 图标
|
||
quote_btn2 = self.page.locator("button:has(.pi-bookmark)").first
|
||
if quote_btn2.is_visible():
|
||
quote_btn2.click()
|
||
logger.info("✅ 设为参考图按钮点击成功(icon 定位)")
|
||
else:
|
||
# 层3:JS 直接 click
|
||
self.page.evaluate("""() => {
|
||
let btn = document.querySelector('button[title="设为参考图"]');
|
||
if (!btn) {
|
||
const icon = document.querySelector('.pi-bookmark');
|
||
if (icon) btn = icon.closest('button');
|
||
}
|
||
if (btn) btn.click();
|
||
}""")
|
||
logger.info("✅ JS 兜底点击设为参考图成功")
|
||
else:
|
||
# 找不到生成图片时,直接用 JS 强触发(可能图片已渲染但坐标获取异常)
|
||
logger.warning("⚠️ 未找到合格图片坐标,直接 JS 点击设为参考图")
|
||
self.page.evaluate("""() => {
|
||
let btn = document.querySelector('button[title="设为参考图"]');
|
||
if (!btn) {
|
||
const icon = document.querySelector('.pi-bookmark');
|
||
if (icon) btn = icon.closest('button');
|
||
}
|
||
if (btn) btn.click();
|
||
}""")
|
||
time.sleep(1)
|
||
except Exception as e:
|
||
logger.warning(f"⚠️ 点击设为参考图按钮失败: {e}")
|
||
|
||
|
||
|
||
def stop_generation(self):
|
||
"""点击 [停止生成] 按钮(橙色 danger 按钮)"""
|
||
logger.info("⏹ 正在点击 [停止生成] 按钮...")
|
||
try:
|
||
# 停止按钮带 danger 类或包含"停止生成"文字
|
||
stop_btn = self.page.locator("button.p-button-danger, button[class*='danger']", has_text="停止生成").first
|
||
if stop_btn.is_visible():
|
||
stop_btn.click()
|
||
logger.info("✅ 停止生成按钮点击成功")
|
||
return
|
||
except Exception:
|
||
pass
|
||
|
||
# JS 兜底
|
||
clicked = self.page.evaluate("""() => {
|
||
const btns = Array.from(document.querySelectorAll('button'));
|
||
const stopBtn = btns.find(b => b.innerText.includes('停止生成'));
|
||
if (stopBtn) { stopBtn.click(); return true; }
|
||
return false;
|
||
}""")
|
||
if clicked:
|
||
logger.info("✅ JS 兜底点击停止生成成功")
|
||
else:
|
||
logger.warning("⚠️ 未找到停止生成按钮")
|
||
time.sleep(1)
|
||
|
||
def wait_for_generation_resumable(self, timeout=300):
|
||
"""
|
||
等待「正在中断...」状态消失、执行(发送)按钮重新可点击。
|
||
检测策略(任意一条通过即视为恢复):
|
||
1. 「正在中断...」消失 AND .pi-send 按钮可用
|
||
2. 「正在中断...」消失 AND 找到可点击的发送/执行按钮(文字匹配)
|
||
3. 「正在中断...」消失 AND textarea 可输入(最宽松兜底)
|
||
- 返回 True:已恢复可执行状态
|
||
- 返回 False:超时(timeout=300s)
|
||
"""
|
||
logger.info(f"⏳ 等待中断完成、执行按钮恢复可用 (最长 {timeout}s)...")
|
||
start_time = time.time()
|
||
|
||
while time.time() - start_time < timeout:
|
||
elapsed = int(time.time() - start_time)
|
||
|
||
try:
|
||
is_resumable = self.page.evaluate("""() => {
|
||
const bodyText = document.body.innerText || '';
|
||
|
||
// 「正在中断...」还在 → 还没恢复
|
||
if (bodyText.includes('正在中断')) return false;
|
||
|
||
// 策略1: .pi-send 图标按钮可用
|
||
const sendIcon = document.querySelector('.pi-send');
|
||
if (sendIcon) {
|
||
const btn = sendIcon.closest('button');
|
||
if (btn && !btn.disabled && btn.offsetParent !== null) return true;
|
||
}
|
||
|
||
// 策略2: 文字为「发送」或「执行」的可用按钮
|
||
const btns = Array.from(document.querySelectorAll('button'));
|
||
const actionBtn = btns.find(b => {
|
||
if (b.disabled || b.offsetParent === null) return false;
|
||
const text = (b.innerText || '').trim();
|
||
const label = (b.getAttribute('aria-label') || '').toLowerCase();
|
||
return text === '发送' || text === '执行' ||
|
||
label.includes('send') || label.includes('submit');
|
||
});
|
||
if (actionBtn) return true;
|
||
|
||
// 策略3: textarea 可输入(最宽松,只要中断消失就认为可继续)
|
||
const ta = document.querySelector('textarea');
|
||
if (ta && !ta.disabled && ta.offsetParent !== null) return true;
|
||
|
||
return false;
|
||
}""")
|
||
except Exception:
|
||
is_resumable = False
|
||
|
||
if is_resumable:
|
||
logger.info(f"✅ {elapsed}s 后中断完成,执行按钮已恢复可用")
|
||
return True
|
||
|
||
if elapsed % 30 == 0 and elapsed > 0:
|
||
logger.info(f" ⏱️ 已等待 {elapsed}s,继续监听中断状态...")
|
||
time.sleep(3)
|
||
|
||
logger.error(f"❌ 等待执行按钮恢复超时 ({timeout}s)")
|
||
return False
|
||
|