如何使用 node.js 控制浏览器(ala Selenium)?

发布于 2024-11-04 19:22:36 字数 209 浏览 2 评论 0原文

我听说过苏打 ,但似乎需要您注册,并且分钟数有限制(免费帐户/200 分钟)。

有谁知道是否有其他方法来控制浏览器,或者更具体地说,在网页上调用 JS?

I've heard of soda, but it seems like it requires you to signup and there's a limit on the # of minutes ( free acct / 200 minutes ).

Does anyone know if there's some alternative way to control a browser, or more specifically invoke JS on a web page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简美 2024-11-11 19:22:36

https://github.com/LearnBoost/soda/raw/master/examples /google.js

/**
 * Module dependencies.
 */

var soda = require('../')
  , assert = require('assert');

var browser = soda.createClient({
    host: 'localhost'
  , port: 4444
  , url: 'http://www.google.com'
  , browser: 'firefox'
});

browser.on('command', function(cmd, args){
  console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
});

browser
  .chain
  .session()
  .open('/')
  .type('q', 'Hello World')
  .clickAndWait('btnG')
  .getTitle(function(title){
    assert.ok(~title.indexOf('Hello World'), 'Title did not include the query');
  })
  .clickAndWait('link=Advanced search')
  .waitForPageToLoad(2000)
  .assertText('css=#gen-query', 'Hello World')
  .assertAttribute('as_q@value', 'Hello World')
  .testComplete()
  .end(function(err){
    if (err) throw err;
    console.log('done');
  });

https://github.com/LearnBoost/soda/raw/master/examples/google.js

/**
 * Module dependencies.
 */

var soda = require('../')
  , assert = require('assert');

var browser = soda.createClient({
    host: 'localhost'
  , port: 4444
  , url: 'http://www.google.com'
  , browser: 'firefox'
});

browser.on('command', function(cmd, args){
  console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
});

browser
  .chain
  .session()
  .open('/')
  .type('q', 'Hello World')
  .clickAndWait('btnG')
  .getTitle(function(title){
    assert.ok(~title.indexOf('Hello World'), 'Title did not include the query');
  })
  .clickAndWait('link=Advanced search')
  .waitForPageToLoad(2000)
  .assertText('css=#gen-query', 'Hello World')
  .assertAttribute('as_q@value', 'Hello World')
  .testComplete()
  .end(function(err){
    if (err) throw err;
    console.log('done');
  });
丿*梦醉红颜 2024-11-11 19:22:36

Zombie.js 可能适合你。它是无头的,看起来真的很酷。

Zombie.js might work for you. It is headless and seems really cool.

终难愈 2024-11-11 19:22:36

实际上,现在 JavaScript 的 Selenium 绑定可以与 Node.js 一起使用。

以下是一些入门的基本步骤:

  1. 1 安装 Node.js,您可以在此处找到下载。
  2. 确保你
    拥有最新的 Chrome 驱动程序并将其放入您的路径中。
  3. 使用 npm install selenium-webdriver 将模块添加到您的项目中。
  4. 编写测试,例如:


var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('simple programmer');
driver.findElement(webdriver.By.name('btnG')).click();
driver.quit();</code>

我通过一些屏幕截图介绍了如何执行此操作,以及如何使用 Mocha 作为测试驱动程序 我的博客文章在这里

There are actually now Selenium bindings for JavaScript that work with Node.js.

Here are some basic steps to get started:

  1. 1 Install Node.js, you can find the download here.
  2. Make sure you
    have the latest Chrome driver and put it in your path.
  3. Use npm install selenium-webdriver to get the module added to your project.
  4. Write a test, for example:


var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('simple programmer');
driver.findElement(webdriver.By.name('btnG')).click();
driver.quit();</code>

I cover how to do this with some screenshots and how to use Mocha as a test driver in my blog post here.

未央 2024-11-11 19:22:36

这是一个围绕 selenium 的 webdriver 的 java API 的纯 Node.js 包装器:

https://npmjs.org/package/ webdriver-sync

这是一个示例:

var webdriverModule = require("webdriver-sync");
var driver = new webdriverModule.ChromeDriver;
var By = webdriverModule.By;
var element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
element = driver.findElement(By.name("q"));
assert.equal(element.getAttribute('value'), "Cheese!");

将其保存在 .js 文件中并使用 Node 运行它。

该模块是一个纯粹的包装器,因此睡眠或同步调用之类的事情是完全可能的。这是模块的当前界面:

module.exports={
   ChromeDriver:ChromeDriver,
   FirefoxDriver:FirefoxDriver,
   HtmlUnitDriver:HtmlUnitDriver,
   By:new By(),
   ExpectedConditions:new ExpectedConditions(),
   WebDriverWait:WebDriverWait,
   Credentials:UserAndPassword,
   Cookie:Cookie,
   TimeUnits:TimeUnits,
   /**
    * @param {number} amount in mills to sleep for.
    */
   sleep:function(amount){
      java.callStaticMethodSync(
         "java.lang.Thread",
         "sleep",
         new Long(amount)
      );
   }
};

您可以在此处查看测试完整功能的集成测试:

https://github.com/jsdevel/webdriver-sync/blob/master/test/integrations/SmokeIT.js

Here's a pure node.js wrapper around the java API for selenium's webdriver:

https://npmjs.org/package/webdriver-sync

Here's an example:

var webdriverModule = require("webdriver-sync");
var driver = new webdriverModule.ChromeDriver;
var By = webdriverModule.By;
var element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
element = driver.findElement(By.name("q"));
assert.equal(element.getAttribute('value'), "Cheese!");

Save that in a .js file and run it with node.

The module is a pure wrapper, so things like sleep or synchronous calls are entirely possible. Here's the current interface of the module:

module.exports={
   ChromeDriver:ChromeDriver,
   FirefoxDriver:FirefoxDriver,
   HtmlUnitDriver:HtmlUnitDriver,
   By:new By(),
   ExpectedConditions:new ExpectedConditions(),
   WebDriverWait:WebDriverWait,
   Credentials:UserAndPassword,
   Cookie:Cookie,
   TimeUnits:TimeUnits,
   /**
    * @param {number} amount in mills to sleep for.
    */
   sleep:function(amount){
      java.callStaticMethodSync(
         "java.lang.Thread",
         "sleep",
         new Long(amount)
      );
   }
};

You can see an integration test that tests the full capabilities here:

https://github.com/jsdevel/webdriver-sync/blob/master/test/integrations/SmokeIT.js

屋檐 2024-11-11 19:22:36

wd 是“适用于 webdriver/selenium 2 的 node.js javascript 客户端”

wd is "A node.js javascript client for webdriver/selenium 2"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文