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) Location strategy;
selector - (string) 使用指定定位策略的元素选择器。
findElements(strategy, target) 返回打开的页面中多个元素的Element objects的array(如果没有元素匹配定位策略和目标则返回空array)。

参数:
strategy - (string, CSS选择器/链接文本/部分链接文本/标签名/Xpath) Location strategy;
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, 可选) 目标框架。要通过元素选择框架,传递该元素。要通过索引选择框架,传递数字。如果留空,将切换到Top级浏览上下文。

所有Browser方法都可能抛出以下错误:

  • BrowserError - 从Error object派生,如果Browser构造函数失败则抛出;包含一个额外的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。