Nightmare.js
有一个非常棒的工具叫做Nightmare.js。首先,它是一个高级 Phantom 包装器,但从 v2 开始,它在 Atom 上被重写。 Nightmare 是基于 webkit 的。
Nightmare 可以无头执行,但您可能需要配置服务器才能使其正常工作。
为什么是噩梦?这是来自官方网站的代码示例:
Nightmare.js
yield Nightmare()
.goto('http://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit');
比较:
Phantom.js
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open('http://yahoo.com', function (status) {
page.evaluate(function () {
var el =
document.querySelector('input[title="Search"]');
el.value = 'github nightmare';
}, function (result) {
page.evaluate(function () {
var el = document.querySelector('.searchsubmit');
var event = document.createEvent('MouseEvent');
event.initEvent('click', true, false);
el.dispatchEvent(event);
}, function (result) {
ph.exit();
});
});
});
});
});
因此您必须编写的代码要少得多。
但它仅限 WEBKIT
硒
为了让某些东西在所有浏览器中都能正常工作,请查看Selenium。它supports really many browsers and platforms。
var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();
只是一个小建议 Selenium 测试可能比噩梦测试更“庞大”,而且我在之前的一份工作中的 Selenium 测试中看到了很多“Promise hell”,所以在你开始之前,我给你的建议是使用生成器和co 或其他一些控制流库。