设为首页 收藏本站
查看: 771|回复: 0

[经验分享] JavaScript(Node.js)+ Selenium自动化测试

[复制链接]

尚未签到

发表于 2017-2-23 11:54:29 | 显示全部楼层 |阅读模式
  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。
  官方网址:https://nodejs.org/en/



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

  chromedriver(.exe)

  Internet Explorer

  IEDriverServer.exe

  Edge

  MicrosoftWebDriver.msi

  Firefox 47+

  geckodriver(.exe)

  PhantomJS

  phantomjs(.exe)

  Opera

  operadriver(.exe)

  Safari

  SafariDriver.safariextz



  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-346216-1-1.html 上篇帖子: 博客园博文备份小记 下篇帖子: 总结一年来的前端学习心得
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表