如何使用UFT/QTP控制Chrome缩放且不新建实例修改测试页面级别
Hey there! Let's tackle your two UFT/QTP Chrome zoom questions one by one—here are practical, tested solutions that work without needing to spin up new browser instances:
Chrome relies on native keyboard shortcuts for zoom controls, and UFT can simulate these easily. This method mirrors the exact behavior you'd get manually pressing Ctrl++ or Ctrl+-:
Using Keyboard Shortcuts (Native Browser Zoom)
This affects the entire browser window's zoom level, just like manual interaction:
' First, ensure the Chrome browser window is active Browser("title:=.*").Activate ' Zoom In (Ctrl + Plus sign) Browser("title:=.*").Page("url:=.*").WebElement("tagname:=html").SendKeys "^+" ' Zoom Out (Ctrl + Minus sign) Browser("title:=.*").Page("url:=.*").WebElement("tagname:=html").SendKeys "^-" ' Reset zoom to 100% (Ctrl + 0) Browser("title:=.*").Page("url:=.*").WebElement("tagname:=html").SendKeys "^0"
Pro Tips:
- Replace the
title:=.*andurl:=.*with your actual browser/page identifiers for better reliability. - Targeting the
<html>element ensures the keystrokes are sent to the entire page, not just a single input field.
Alternative: JavaScript-Based Page Zoom
If you need to set an exact zoom percentage (instead of incremental steps), you can run a JavaScript snippet directly on the page. Note this modifies the page's CSS zoom, not the browser's native zoom setting:
' Set zoom to 120% Browser("title:=.*").Page("url:=.*").RunScript "document.body.style.zoom = '120%'" ' Set zoom to 80% Browser("title:=.*").Page("url:=.*").RunScript "document.body.style.zoom = '80%'"
The best part? Both methods above work directly on your existing, test-running Chrome instance—no need to launch a new browser window. Here's how to make it seamless:
- Keep your existing Browser/Page objects: You don't need to call
Browser("Chrome").Launchagain. Just reference the same browser instance you're already using for your test. - Use the keyboard shortcut method for native zoom: This will adjust the zoom level of the active tab/window without disrupting your test flow.
- For exact percentages: Use the JavaScript snippet on your current Page object to set the zoom instantly.
Example Workflow:
' Assume you already have an active Chrome session from your test Set currentBrowser = Browser("title:=My Test Page") Set currentPage = currentBrowser.Page("url:=https://my-test-site.com") ' Adjust zoom to 110% incrementally currentPage.WebElement("tagname:=html").SendKeys "^+" ' Zoom in once (usually ~10% step) ' Or set exact zoom via JS currentPage.RunScript "document.body.style.zoom = '110%'"
内容的提问来源于stack exchange,提问作者deepthi cherukuri

