selenium js webdriver - 附加到活动会话

Waz*_*ime 4 javascript session selenium

我正在使用带有selenium-webdriver包的Node.js 来运行我的测试.
每次测试开始时,Web驱动程序都会启动一个新会话并打开一个新窗口.
我正在尝试获取会话ID并在以后使用它getSession() (doc referance link)

var webdriver = require('selenium-webdriver');
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;

var server = new SeleniumServer('./seleniumServer/selenium-server-standalone-2.43.1.jar', {
    port: 4444
});
server.start();

var driver = new webdriver.Builder()
        .usingServer(server.address())
        .withCapabilities(webdriver.Capabilities.firefox())
        .build();

console.log(driver.getSession());
Run Code Online (Sandbox Code Playgroud)

但这会导致异常:

getSession();
^
TypeError: Object [object Object] has no method 'getSession'
    at Object.<anonymous> (\testing\demo_1.js:14:3)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我它有什么问题以及如何获得并设置selenium会话ID?
最重要的是,如何使用sessionId附加到打开的浏览器会话?

gm2*_*008 5

如果你的webdriver构建过程成功,driver应该有方法getSession().getSession()的文档可以在这里找到.

但是,返回的getSession()是promise,因此您不会直接从返回值获取会话ID.你需要这样做:

 driver.getSession()
                .then( function(session){
                    var session_id = session.getId();
                });
Run Code Online (Sandbox Code Playgroud)

您很可能需要将会话ID保存在文件中,然后在下次运行程序时,使用此函数附加到此会话ID:

browser = webdriver.WebDriver.attachToSession(...);
Run Code Online (Sandbox Code Playgroud)

,其中的文件可以在这里找到.

但是,问题是上面的函数调用attachToSession()不会通知您它是否成功.我解决它的方法是browser.getTitle()使用返回的WebDriver对象进行调用,并等待它解析/拒绝.我们将知道我们是否已成功附加到会话ID.


设置webdriver:

为了回应user3789620的问题,我在这里放置了设置webdriver的代码:

var webdriver_server = 'http://localhost:9515', // chromedriver.exe serves at this port 
chrome = require('selenium-webdriver/chrome'),
options = new chrome.Options(),
webdriver = require( 'selenium-webdriver'),
Http = require( 'selenium-webdriver/http');
options.setChromeBinaryPath(your_chrome_binary_path);

var browser = new webdriver.Builder()
  .withCapabilities(webdriver.Capabilities.chrome())
  .setChromeOptions(options)
  .usingServer(webdriver_server)
  .build()

if( 'undefined' != typeof saved_session_id && saved_session_id!= ""){
  console.log("Going to attach to existing session  of id: " + saved_session_id);
  client = new Http.HttpClient( webdriver_server );
  executor = new Http.Executor( client);
  browser = webdriver.WebDriver.attachToSession( executor, saved_session_id);
}
Run Code Online (Sandbox Code Playgroud)


Jac*_*rry 5

感谢gm2008,他的回答让我走上了正轨。该attachToSession功能对undefined我来说可能是一个实现更改(我正在使用4.0.0-alpha.1of selenium-webdriver)。但是,我能够通过 TypeScript 中的以下内容完成所需的行为:

import wd, { WebDriver, Session } from 'selenium-webdriver'
import { HttpClient, Executor } from 'selenium-webdriver/http'

// My server URL comes from running selenium-standalone on my machine
const server: string = 'http://localhost:4444/wd/hub'

async function newBrowserSessionId(): Promise<string> {
  const browser: WebDriver = new wd.Builder()
    .withCapabilities(wd.Capabilities.chrome())
    .usingServer(server)
    .build()

  const session: Session = await browser.getSession()

  return session.getId()
}

async function getExistingBrowser(sessionId: string): Promise<WebDriver> {
  const client: HttpClient = new HttpClient(server)
  const executor: Executor = new Executor(client)
  const session: Session = new Session(sessionId, wd.Capabilities.chrome())

  return new WebDriver(session, executor)
}

async function driveExistingBrowser(browser: WebDriver): Promise<void> {
  await browser.get('https://www.google.com/')
}

async function closeExistingBrowser(browser: WebDriver): Promise<void> {
  await browser.close()
}

async function connectAndDrive(): Promise<void> {
  const sessionId: string = await newBrowserSessionId()
  const existingBrowser: WebDriver = await getExistingBrowser(sessionId)

  await driveExistingBrowser(existingBrowser)
  await closeExistingBrowser(existingBrowser)
}

connectAndDrive()
Run Code Online (Sandbox Code Playgroud)

只要您保持会话打开并有办法传递 ID,Session即使启动会话的脚本已完成执行,您也可以附加到现有会话。closeExistingBrowser()只要您准备好执行清理,就可以使用该函数。