JavaScript(Node.js)+ Selenium自动化测试
Selenium is a browser automation library. Most often used for testing web-applications, Selenium may be used for any task that requires automating interaction with the browser.
Selenium是一个浏览器自动化测试库,大多时候我们用它来测试web应用,Selenium 可以胜任任何在浏览器上自动化测试的任务。
众所周知,Selenium可以支持多种编程语言(Java/ruby/python/C#/Go/JavaScipt),这篇博客就来介绍如何通过JavaScipt语言编写Selenium自动化测试脚本。在此之前,需要把环境搭建起来。
之前有个问题一直弄不明白,JavaScipt脚本不是只打开浏览器才能执行么?如何运行Selenium呢?难道我要打开一个浏览器执行JavaScipt去驱动另一个浏览器执行?直到我昨天看了一点Node.js的资料,才突然明白。
所以,需要先安装Node.js。
Installation
Selenium may be installed via npm with
Selenium可以通过npm安装。(npm是随同NodeJS一起安装的包管理工具。)
>npm install selenium-webdriver
NOTE: Mozilla's geckodriver is only required for Firefox 47+. Everything you need for Firefox 38-46 is included with this package.
Selenium官方在推出了3.0,值得庆祝,万年的2.x终于升级到3.0了,当然,3.0的正式版还没推出。其中带来了一些改变。最大的变化之一是,Firefox浏览器的驱动由原来集成在Selenium安装包里,现在改为独立的一个驱动文件了(gekodriver),但是,它只能驱动Firefox47版本以上(目前最新版本是48.0.2)。
经过多年的发展WebDriver已经成为了事实上的标准,现在每种浏览器都有独立的官方驱动文件了。如下表:
|
Browser |
Component |
|
Chrome |
|
|
Internet Explorer |
|
|
Edge |
|
|
Firefox 47+ |
|
|
PhantomJS |
|
|
Opera |
|
|
Safari |
You will need to download additional components to work with each of the major browsers. The drivers for Chrome, Firefox, PhantomJS, Opera, and Microsoft's IE and Edge web browsers are all standalone executables that should be placed on your system PATH. The SafariDriverbrowser extension should be installed in your browser before using Selenium; we recommend disabling the extension when using the browser without Selenium or installing the extension in a profile only used for testing.
然后,把这些驱动下载,并存放到一个目录中,例如:D:/driver/ ,然后,把这这个目录添加到系统环境变量PATH下面。
Usage
当Selenium-webdriver 被npm下载完成,将到在当前目录下多出一个../node_modules/目录。然后,在与这个目录同级的目录下创建第一个Selenium测试脚本baidu.js。
The sample below and others are included in the example directory. You may also find the tests for selenium-webdriver informative.
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
driver.get('https://www.baidu.com');
driver.findElement(By.id('kw')).sendKeys('webdriver');
driver.findElement(By.id('su')).click();
driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
driver.quit();
执行姿势,打开cmd执行。
>node baidu.js
chrome mobile emulation
有时候,需要模拟移动端浏览器测试。例子如下:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until,
chrome = require('selenium-webdriver/chrome');
var driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options()
.setMobileEmulation({deviceName: 'Google Nexus 5'}))
.build();
driver.get('https://m.baidu.com');
driver.findElement(By.name('word')).sendKeys('webdriver');
driver.findElement(By.name('word')).submit();
driver.wait(until.titleIs('webdriver - 百度'), 2000);
driver.quit();
Using the Builder API
The Builder class is your one-stop shop for configuring new WebDriver instances. Rather than clutter your code with branches for the various browsers, the builder lets you set all options in one flow. When you call Builder#build(), all options irrelevant to the selected browser are dropped:
var webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome'),
firefox = require('selenium-webdriver/firefox');
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setChromeOptions(/* ... */)
.setFirefoxOptions(/* ... */)
.build();
Why would you want to configure options irrelevant to the target browser? The Builder's API defines your defaultconfiguration. You can change the target browser at runtime through the SELENIUM_BROWSER environment variable. For example, the example/google_search.js script is configured to run against Firefox. You can run the example against other browsers just by changing the runtime environment
# cd node_modules/selenium-webdriver node example/google_search SELENIUM_BROWSER=chrome node example/google_search SELENIUM_BROWSER=safari node example/google_search
The Standalone Selenium Server
The standalone Selenium Server acts as a proxy between your script and the browser-specific drivers. The server may be used when running locally, but it's not recommend as it introduces an extra hop for each request and will slow things down. The server is required, however, to use a browser on a remote host (most browser drivers, like the IEDriverServer, do not accept remote connections).
To use the Selenium Server, you will need to install the JDK and download the latest server from Selenium. Once downloaded, run the server with
>java -jar selenium-server-standalone-2.45.0.jar
You may configure your tests to run against a remote server through the Builder API:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub') //注意这里
.build();
driver.get('https://www.baidu.com');
driver.findElement(By.id('kw')).sendKeys('webdriver');
driver.findElement(By.id('su')).click();
driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
driver.quit();
Or change the Builder's configuration at runtime with the SELENIUM_REMOTE_URL environment variable:
SELENIUM_REMOTE_URL="http://localhost:4444/wd/hub" node script.js
Documentation
API documentation is available online from the Selenium project. Additional resources include
- the #selenium channel on freenode IRC
- the selenium-users@googlegroups.com list
- SeleniumHQ documentation
===============
官方原文:http://seleniumhq.github.io/selenium/docs/api/javascript/index.html
JavaScript(Node.js)+ Selenium自动化测试的更多相关文章
- javascript – Node.js请求CERT_HAS_EXPIRED
javascript – Node.js请求CERT_HAS_EXPIRED 转 http://www.voidcn.com/article/p-ssctwovd-bsy.html 原文 htt ...
- e2e 自动化集成测试 环境搭建 Node.js Selenium WebDriverIO Mocha Node-Inspector
Node.js已经出来了许多年载,至今才开始接触.周未在家闲来无事,一时心血来潮,Google了大量的文章,经过实验,终于可以把整个环境给搭起来, 废话不多话,请看步骤. 特别注意, 本文章是针对Wi ...
- 获取本机IP地址[JavaScript / Node.js]
--web客户端JavaScript <body onload="checkCookie()"></body> function getYourIP(){ ...
- e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (一) 京东 商品搜索
之前有发布一篇文章“e2e 自动化集成测试 环境搭建 Node.js Selenium WebDriverIO Mocha Node-Inspector”, 主要是讲了,如何搭建环境, 其中开发环境使 ...
- 用node-inspector调试Node.js(转自NOANYLOVE'S BLOG)
原文地址:http://www.noanylove.com/2011/12/node-the-inspector-debugging-node-js/ 用node-inspector调试Node.js ...
- [转]Getting Start With Node.JS Tools For Visual Studio
本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...
- .NET到Node.js
从.NET到Node.js谈前后端分离实践(by vczero) 一.最初的[无分离]实践 11年末的时候,用winForm开发程序,拖拖控件,点点按钮,连接数据库,做一些基本的管理系统:Java ...
- AngularJS + Node.js + MongoDB开发
AngularJS + Node.js + MongoDB开发的基于位置的通讯录(by vczero) 一.闲扯 有一天班长说了,同学们希望我开发一个可以共享位置的通讯录,于是自己简单设计了下功能.包 ...
- [js高手之路]Node.js模板引擎教程-jade速学与实战2-流程控制,转义与非转义
一.转义与非转义 jade模板文件代码: doctype html html head meta(charset='utf-8') title jade学习-by ghostwu body h3 转义 ...
- windows系统下安装 node.js (node.js安装及环境配置)
node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...
随机推荐
- SQL 存储过程 传入数组参数
今天在做统计数据的时候,传入数组导致数据不显示.解决方式和大家分享一下: --参数@CompanyName='北京,天津,上海' DECLARE @PointerPrev int DECLAR ...
- CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
安装篇: 一.安装Apache yum install httpd #根据提示,输入Y安装即可成功安装 /etc/init.d/httpd start#启动Apache 备注:Apache启动之后会提 ...
- 使用Aspose.Cells读取Excel
最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...
- 自定义cell右侧 多按钮
#import "ViewController.h" @interface ViewController () <UITableViewDataSource, UITable ...
- 多线程导出大规模excel文件
文章有点水,和前几篇没有太大区别,但是单线程处理大文件导出会非常耗时间,用到多线程才能更加合理的利用资源.大文件也可能会超出excel工作表范围.这里也有相应处理 参考:用DataGridView导入 ...
- .NET跨平台:在Linux Ubuntu上编译coreclr/corefx/dnx(20150617)
编译时间:北京2015年6月17日上午 操作系统:Ubuntu 14.04.2 LTS Mono版本:Mono JIT compiler version 4.3.0 (master/3445ac5 T ...
- MSBuild的简单介绍与使用
MSBuild 是 Microsoft 和 Visual Studio的生成系统.它不仅仅是一个构造工具,应该称之为拥有相当强大扩展能力的自动化平台.MSBuild平台的主要涉及到三部分:执行引擎.构 ...
- ABP理论学习之开篇介绍
返回总目录 为了和2016年春节赛跑,完成该系列博客,我牺牲了今天中午的时间来完成该系列的第一篇----开篇介绍.开篇介绍嘛,读过大学教材的同学都知道,这玩意总是那么无聊,跟考试没关系,干脆直接跳过, ...
- 从天猫和支付宝身上学习opcity与rgba
不知道什么时候,一个双层透明的对话框悄然流行了起来. 我们在各大网站上都能见到类似这样的对话框: 这样的聚焦更明显,用户体验更好,也显得更加的高大上. 先说点题外话,这种布局如何用CSS+DIV去实现 ...
- ASP.NET MVC 路由(四)
ASP.NET MVC路由(四) 前言 在前面的篇幅中我们讲解路由系统在MVC中的运行过程以及粗略的原理,想必看过前面篇幅的朋友应该对路由有个概念性的了解了,本篇来讲解区域,在读完本篇后不会肯定的让你 ...