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.javascript
Selenium是一個瀏覽器自動化測試庫,大多時候咱們用它來測試web應用,Selenium 能夠勝任任何在瀏覽器上自動化測試的任務。html
衆所周知,Selenium能夠支持多種編程語言(Java/ruby/python/C#/Go/JavaScipt),這篇博客就來介紹如何經過JavaScipt語言編寫Selenium自動化測試腳本。在此以前,須要把環境搭建起來。java
以前有個問題一直弄不明白,JavaScipt腳本不是隻打開瀏覽器才能執行麼?如何運行Selenium呢?難道我要打開一個瀏覽器執行JavaScipt去驅動另外一個瀏覽器執行?直到我昨天看了一點Node.js的資料,才忽然明白。node
因此,須要先安裝Node.js。python
官方網址:https://nodejs.org/en/git
Selenium may be installed via npm withgithub
Selenium能夠經過npm安裝。(npm是隨同NodeJS一塊兒安裝的包管理工具。)web
>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. chrome
Selenium官方在推出了3.0,值得慶祝,萬年的2.x終於升級到3.0了,固然,3.0的正式版還沒推出。其中帶來了一些改變。最大的變化之一是,Firefox瀏覽器的驅動由原來集成在Selenium安裝包裏,如今改成獨立的一個驅動文件了(gekodriver),可是,它只能驅動Firefox47版本以上(目前最新版本是48.0.2)。npm
通過多年的發展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下面。
當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
有時候,須要模擬移動端瀏覽器測試。例子以下:
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();
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 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
===============
官方原文:http://seleniumhq.github.io/selenium/docs/api/javascript/index.html