JavaScript(Node.js)+Selenium自动化测试
2016-09-0823:14by虫师,294阅读,2评论,收藏,编辑 Seleniumisabrowserautomationlibrary.Mostoftenusedfortestingweb-applications,Seleniummaybeusedforanytaskthatrequiresautomatinginteractionwiththebrowser.
Selenium是一个浏览器自动化测试库,大多时候我们用它来测试web应用,Selenium可以胜任任何在浏览器上自动化测试的任务。
众所周知,Selenium可以支持多种编程语言(Java/ruby/python/C#/Go/JavaScipt),这篇博客就来介绍如何通过JavaScipt语言编写Selenium自动化测试脚本。在此之前,需要把环境搭建起来。
之前有个问题一直弄不明白,JavaScipt脚本不是只打开浏览器才能执行么?如何运行Selenium呢?难道我要打开一个浏览器执行JavaScipt去驱动另一个浏览器执行?直到我昨天看了一点Node.js的资料,才突然明白。
所以,需要先安装Node.js。
官方网址:https://nodejs.org/en/
Installation
Seleniummaybeinstalledvianpmwith
Selenium可以通过npm安装。(npm是随同NodeJS一起安装的包管理工具。)
>npminstallselenium-webdriver NOTE:Mozilla''sgeckodriverisonlyrequiredforFirefox47+.EverythingyouneedforFirefox38-46isincludedwiththispackage.
Selenium官方在推出了3.0,值得庆祝,万年的2.x终于升级到3.0了,当然,3.0的正式版还没推出。其中带来了一些改变。最大的变化之一是,Firefox浏览器的驱动由原来集成在Selenium安装包里,现在改为独立的一个驱动文件了(gekodriver),但是,它只能驱动Firefox47版本以上(目前最新版本是48.0.2)。
经过多年的发展WebDriver已经成为了事实上的标准,现在每种浏览器都有独立的官方驱动文件了。如下表:
Browser
Component
Chrome
chromedriver(.exe)
InternetExplorer
IEDriverServer.exe
Edge
MicrosoftWebDriver.msi
Firefox47+
geckodriver(.exe)
PhantomJS
phantomjs(.exe)
Opera
operadriver(.exe)
Safari
SafariDriver.safariextz
Youwillneedtodownloadadditionalcomponentstoworkwitheachofthemajorbrowsers.ThedriversforChrome,Firefox,PhantomJS,Opera,andMicrosoft''sIEandEdgewebbrowsersareallstandaloneexecutablesthatshouldbeplacedonyoursystemPATH.TheSafariDriverbrowserextensionshouldbeinstalledinyourbrowserbeforeusingSelenium;werecommenddisablingtheextensionwhenusingthebrowserwithoutSeleniumorinstallingtheextensioninaprofileonlyusedfortesting.
然后,把这些驱动下载,并存放到一个目录中,例如:D:/driver/,然后,把这这个目录添加到系统环境变量PATH下面。
Usage
当Selenium-webdriver被npm下载完成,将到在当前目录下多出一个../node_modules/目录。然后,在与这个目录同级的目录下创建第一个Selenium测试脚本baidu.js。
Thesamplebelowandothersareincludedintheexampledirectory.Youmayalsofindthetestsforselenium-webdriverinformative.
复制代码 varwebdriver=require(''selenium-webdriver''), By=webdriver.By, until=webdriver.until;
vardriver=newwebdriver.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执行。
>nodebaidu.js
chromemobileemulation
有时候,需要模拟移动端浏览器测试。例子如下:
复制代码 varwebdriver=require(''selenium-webdriver''), By=webdriver.By, until=webdriver.until, chrome=require(''selenium-webdriver/chrome'');
vardriver=newwebdriver.Builder() .forBrowser(''chrome'') .setChromeOptions(newchrome.Options() .setMobileEmulation({deviceName:''GoogleNexus5''})) .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(); 复制代码
UsingtheBuilderAPI
TheBuilderclassisyourone-stopshopforconfiguringnewWebDriverinstances.Ratherthanclutteryourcodewithbranchesforthevariousbrowsers,thebuilderletsyousetalloptionsinoneflow.WhenyoucallBuilder#build(),alloptionsirrelevanttotheselectedbrowseraredropped:
复制代码 varwebdriver=require(''selenium-webdriver''),
chrome=require(''selenium-webdriver/chrome''),
firefox=require(''selenium-webdriver/firefox'');
vardriver=newwebdriver.Builder()
.forBrowser(''firefox'')
.setChromeOptions(/.../)
.setFirefoxOptions(/.../)
.build(); 复制代码 Whywouldyouwanttoconfigureoptionsirrelevanttothetargetbrowser?TheBuilder''sAPIdefinesyourdefaultconfiguration.YoucanchangethetargetbrowseratruntimethroughtheSELENIUM_BROWSERenvironmentvariable.www.wang027.comForexample,theexample/google_search.jsscriptisconfiguredtorunagainstFirefox.Youcanruntheexampleagainstotherbrowsersjustbychangingtheruntimeenvironment
复制代码 #cdnode_modules/selenium-webdriver
nodeexample/google_search
SELENIUM_BROWSER=chromenodeexample/google_search
SELENIUM_BROWSER=safarinodeexample/google_search 复制代码
TheStandaloneSeleniumServer
ThestandaloneSeleniumServeractsasaproxybetweenyourscriptandthebrowser-specificdrivers.Theservermaybeusedwhenrunninglocally,butit''snotrecommendasitintroducesanextrahopforeachrequestandwillslowthingsdown.Theserverisrequired,however,touseabrowseronaremotehost(mostbrowserdrivers,liketheIEDriverServer,donotacceptremoteconnections).
TousetheSeleniumServer,youwillneedtoinstalltheJDKanddownloadthelatestserverfromSelenium.Oncedownloaded,runtheserverwith
>java-jarselenium-server-standalone-2.45.0.jar YoumayconfigureyourteststorunagainstaremoteserverthroughtheBuilderAPI:
复制代码 varwebdriver=require(''selenium-webdriver''), By=webdriver.By, until=webdriver.until;
vardriver=newwebdriver.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(); 复制代码 OrchangetheBuilder''sconfigurationatruntimewiththeSELENIUM_REMOTE_URLenvironmentvariable:
SELENIUM_REMOTE_URL="http://localhost:4444/wd/hub"nodescript.js |
|