这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.

2 浏览器 监控项 JavaScript objects

概述

本节描述了使用 Duktape 在 JavaScript 语言中为在 Browser item 脚本中使用而实现的 Zabbix 扩展。 这些扩展补充了 Additional JavaScript objects 页面上描述的 JavaScript objects。

浏览器

Browser object 负责管理 WebDriver 会话,在创建时初始化会话,并在销毁时终止会话。 单个脚本最多可支持四个 Browser objects。

要构造一个 Browser object,请使用 new Browser(options) 语法。 optionsJSON object)参数用于指定浏览器选项,通常为 WebDriver 选项方法的结果(例如,Browser.chromeOptions())。

以下方法在 Browser object 中受支持。

方法 描述
navigate(url) 导航到指定的 URL。

参数:
url - (string) 要导航到的 URL。
getUrl() 返回已打开页面 URL 的string。
getPageSource() 返回已打开页面源的 string。
findElement(strategy, selector) 返回一个包含在打开页面中的 Element object(如果没有元素匹配 strategyselector,则返回 null)。

参数:
strategy - (string, CSS 选择器/链接文本/部分链接文本/标签名/Xpath) 定位策略;
selector - (string) 使用指定定位策略的元素选择器。
findElements(strategy, target) 返回一个包含打开页面中多个元素的 array Element objects(如果没有任何元素匹配定位策略和目标,则返回一个空的 array)。

参数:
strategy - (string,CSS 选择器/链接文本/部分链接文本/标签名称/Xpath)定位策略;
target - (string)使用指定定位策略的元素选择器。
getCookies() 返回 Cookie objects 的 array。
addCookie(cookie) 设置 Cookie。

参数:
cookie - (Cookie object) 要设置的 Cookie。
getScreenshot() 返回一个Base64编码的string,代表浏览器视口图像。
setScreenSize(x,y) 设置浏览器的视口大小。

参数:
x - (string) 高度(以像素为单位);
y - (string) 宽度(以像素为单位)。
setScriptTimeout(timeout) 设置脚本加载超时。

参数:
timeout - (integer) 超时时间(以毫秒为单位)。
setSessionTimeout(timeout) 设置会话(页面加载)超时。

参数:
timeout - (integer) 超时值(以毫秒为单位)。
setElementWaitTimeout(timeout) 设置元素位置策略(隐式)超时时间。

参数:
timeout - (integer) 超时时间,单位为毫秒。
collectPerfEntries(mark) 收集要通过 getResult() 方法检索的性能条目。

参数:
mark - (string,可选)性能快照标记。
getRawPerfEntries() 返回性能条目 objects 的 array。
getResult() 返回一个 Result object,其中包含浏览器会话统计信息(错误信息、性能快照等)。
getError() 返回一个包含浏览器错误的 BrowserError object(如果没有浏览器错误,则返回 null)。
setError(message) 设置一条自定义错误信息,该信息将包含在 Result object 中。

参数:
message - (string) 错误信息。
discardError() 丢弃在 Result object 中返回的错误。
getAlert() 返回一个包含浏览器警报的 Alert object(如果没有浏览器警报,则返回 null)。
chromeOptions() 返回带有预定义 Chrome 浏览器选项的 chromeOptions object。
firefoxOptions() 返回一个带有预定义Firefox浏览器选项的 firefoxOptions object。
safariOptions() 返回一个带有预定义 Safari 浏览器选项的 safariOptions object。
edgeOptions() 返回一个带有预定义 Edge 浏览器选项的 edgeOptions object。
switchFrame(target) 切换到指定的框架。自 version 7.0.4 起受支持。

参数:
target -(浏览器元素或 integer,可选)目标 get 框架。若要通过元素选择框架,请传递该元素。若要通过索引选择框架,请传递数字。如果留空,则将切换到顶级浏览上下文 Top。

所有浏览器方法都可能抛出以下错误:

  • BrowserError - 源自当 Browser 构造函数失败时抛出的 object Error; 包含一个额外的 browser 属性,该属性具有引发此 BrowserErrorBrowser object。
  • WebdriverError - 派生自 BrowserError; 包含与 BrowserError object 相同的属性,这些属性用于指示错误是否是由于 WebDriver 响应中的错误而生成的。

元素

Element object 是由 Browser object 的 findElement()/findElements() 方法返回的,不能直接构造。

Element object 表示网页中的一个元素,并提供了与其交互的方法。

Element object 支持以下方法。

方法 描述
getAttribute(name) 返回元素属性的属性值 string(如果未找到指定属性,则返回 null)。

参数:
name - (string) 属性名称。
getProperty(name) 返回元素属性的属性值 string(如果未找到指定属性,则返回 null)。

参数:
name - (string) 属性名称。
getText() 返回元素文本的文本值 string。
click() 单击元素。
clear() 清除可编辑元素的内容。
sendKeys(keys) 发送按键。

参数:
keys - (string) 要发送的按键。

Cookie object 由 Browser object 的 getCookies() 方法返回,并传递给 addCookie() 方法。

尽管 Cookie object 没有任何方法,但它可以包含以下属性:

属性 类型 描述
name string Cookie 的名称。
value string Cookie 的值。
path string Cookie 有效的路径。
如果在添加 Cookie 时省略,则默认为 "/"
domain string Cookie 可见的域名。
如果在添加 Cookie 时省略,则默认为会话当前浏览上下文的活动文档的 URL 域名。
secure boolean 指示 Cookie 是否安全。
如果在添加 Cookie 时省略,则默认为 false
httpOnly boolean 指示 Cookie 是否为 HTTP-only。
如果在添加 Cookie 时省略,则默认为 false
expiry integer Cookie 的过期时间(自 Unix 时间纪元以来的秒数)。如果在添加 Cookie 时省略,则不得设置。
sameSite string Cookie 的 sameSite 属性,用于控制 Cookie 是否应限制为第一方或同站点上下文。
可以设置为 "Lax""Strict"
如果在添加 Cookie 时省略,则默认为 "None"

告警

Alert object 表示一个网页警告框,由 Browser object 的 getAlert() 方法返回,不能直接构造。

Alert object 包含一个 text 属性,该属性包含警告框的文本内容(如果没有警告框,则返回 null)。

Alert object 支持以下方法:

方法 描述
accept() 接受警告框。
dismiss() 取消警告框。

结果

Result object 包含会话统计信息,并由 Browser object getResult() 方法返回。

通常,Result object 会被字符串化并从脚本返回,然后通过预处理解析为依赖的 监控项值。

虽然 Result object 没有任何方法,但它可以包含以下属性。

属性 类型 描述
duration string 从会话创建到结果检索的会话持续时间。
error object 错误信息。
http_status integer WebDriver 返回的 HTTP 状态(如果没有 WebDriver 错误,则为 0)。
error_code string WebDriver 返回的错误(如果没有 WebDriver 错误,则为一个空的 string)。
message string WebDriver 错误消息(如果没有 WebDriver 错误,则为一个空的 string)。
performance_data object 性能统计信息。
summary object 性能摘要。
navigation object 导航摘要。
resource object 资源摘要。
details array of objects 每次可能导致导航的操作之后的性能统计信息。
mark string (可选) 使用 collectPerfEntries() 方法指定的性能快照标记。
navigation object 导航统计信息。
resource object 此步骤的资源摘要。
user array of objects 标记/测量类型的统计信息的 array。
marks array of objects 已标记的性能快照索引。
name string 性能快照标记名称。
index integer 在 details 中的性能快照索引 array。