从 WebDriverJS 确定会话 ID
Determining session ID from WebDriverJS
我正在尝试在浏览器上运行WebDriverJS,但是文档对于如何让它控制主机浏览器有些模糊。 在这里,它说:
启动浏览器以针对另一个浏览器运行WebDriver测试有点多余 (与仅使用节点相比)。相反,在浏览器中使用 WebDriverJS 旨在 自动化实际运行脚本的浏览器。只要知道服务器的> URL 和浏览器的会话 ID,就可以完成此操作。虽然这些值可能是 直接传递给构建器,它们也可以使用 wdurl 和 WDSID 定义 "环境变量",从加载页面的 URL 查询数据中解析:
<!-- Assuming HTML URL is /test.html?wdurl=http://localhost:4444/wd/hub&wdsid=foo1234 -->
<!DOCTYPE html>
<script src="webdriver.js"></script>
<input id="input" type="text"/>
<script>
// Attaches to the server and session controlling this browser.
var driver = new webdriver.Builder().build();
var input = driver.findElement(webdriver.By.tagName('input'));
input.sendKeys('foo bar baz').then(function() {
assertEquals('foo bar baz',
document.getElementById('input').value);
});
</script>
我想从 Node.js 打开我的测试页,然后运行客户端脚本中包含的命令。 但是,我不知道在构建会话时如何提取会话 ID(wdsid 查询参数)。 有人知道吗?
经过大量的实验和阅读WebDriverJS源代码,终于想通了。
var webdriver = require('./assets/webdriver');
var driver = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({
'browserName': 'chrome',
'version': '',
'platform': 'ANY',
'javascriptEnabled': true
}).
build();
var testUrl = 'http://localhost:3000/test',
hubUrl = 'http://localhost:4444/wd/hub',
sessionId;
driver.session_.then(function(sessionData) {
sessionId = sessionData.id;
driver.get(testUrl + '?wdurl=' + hubUrl + '&wdsid=' + sessionId);
});
driver.session_返回一个 Promise 对象,该对象将在实例化时包含会话数据和其他信息。 使用 .then(callback(sessionData)) 将允许您根据需要操作数据。
我的硒版本:4.1.0
通过await
获得sesion_id
const get_session_id = async (SeleniumDriver) =>
{
const res1 = await SeleniumDriver.getSession();
return res1.getId();
}
调用其他函数: await get_session_id(SeleniumDriver);
相关文章:
- PHP会话ID变量与AJAX一起返回
- 在javascript中获取django站点的会话ID
- 核心ajax每次都发送具有不同会话id的请求
- 如何在java脚本中获取会话id
- 在Express中手动设置会话ID
- Socket.io:随时获取客户端会话 ID
- 从 WebDriverJS 确定会话 ID
- IE 窗口.在第二个会话时打开 打开一个具有第一个会话 ID 的窗口
- 使用 Javascript 捕获会话 ID
- 无法从带有 Socket.IO 的 Cookie 获取快速会话 ID
- 如何从浏览器的 cookie 存储中删除会话 ID
- 为什么会话在 JSP 中的会话超时后具有会话 ID
- 如何在 Cookie 中将会话 ID 设置为在会话结束时过期
- 当请求一个JS文件时,为什么需要cookie信息(会话ID)
- 如何可靠地获取Google Analytics生成的会话id
- 将会话ID/密钥作为请求参数传递时存在安全风险
- opencart跟踪会话id
- 带有会话id的流星路由
- 在cookie中保存变量- console.log返回具有会话id的变量
- 传递会话ID变量,如果内联JS函数允许它在其他地方使用