You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Wallaby execute_script调用自定义JS函数在PhantomJS中失效

问题排查:PhantomJS下Wallaby调用自定义JS函数返回{:error, :obscured}

系统环境

  • OSX v10.13.4
  • Elixir v1.6.5
  • OTP 19
  • Phoenix 1.3.2
  • Wallaby 0.19.2
  • PhantomJS 2.1

问题描述

最近用Wallaby做Phoenix应用测试时碰到个头疼的问题:直接在测试里执行内嵌的JS代码完全正常:

execute_script(session, "localStorage.setItem('test', 'foo'); return localStorage.getItem('test');")

但如果把一模一样的逻辑封装到应用的app.js里,定义成全局函数get_test():

function get_test() {
  localStorage.setItem('test', 'foo');
  return localStorage.getItem('test');
}
window.get_test = get_test

然后在测试套件里调用这个全局函数:

execute_script(session, "return get_test();")

就会返回{:error, :obscured}。

更奇怪的是,相同代码在Selenium环境下运行完全正常,只有切换到PhantomJS时,就找不到这个自定义函数了。我翻了Wallaby的execute_script相关测试用例,里面没有调用自定义全局函数的场景,暂时没找到参考。

不过我做了个验证测试:把自定义函数直接写在execute_script的脚本参数里,测试是能正常通过的:

@function_script """
function get_tester() {
  localStorage.setItem('tester', 'foo');
  return localStorage.getItem('tester');
}
return get_tester();
"""
session 
|> visit("index.html") 
|> execute_script(@function_script, fn(value) -> send self(), {:callback, value} end)
assert_received {:callback, "foo"}

这说明问题肯定出在我的应用配置或者PhantomJS的资源加载环节上,不是Wallaby的execute_script本身的问题。

后续更新

折腾了一阵后终于定位到根因了——是libsodim在PhantomJS环境里没有正确加载,JS代码本身是完全没问题的。现在已经搭建好完整的测试库和工具来处理这个加载问题了。

内容的提问来源于stack exchange,提问作者Spasm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:58:15