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