介绍
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
软件清单
- docker
- selenium/hub
- selenium/node-firefox
- selenium/node-firefox-debug
- selenium/node-chrome
- selenium/node-chrome-debug
- python模块:selenium-3.6.0
部署
安装docker
rancher推荐安装脚本
http://rancher.com/docs/rancher/v1.6/en/hosts/#supported-docker-versions
# 安装过程中提示输入管理员密码
curl https://releases.rancher.com/install-docker/17.12.sh | sh
# 完成后可将当前账户加入docker组中,免去输入sudo
pull镜像
# hub
docker pull selenium/hub
# node with vncserver; default password: secret
docker pull selenium/node-firefox-debug
docker pull selenium/node-chrome-debug
启动
# hub
docker run -d -p 4444:4444 --name selenium-hub selenium/hub
# node the same host with hub
docker run -d -P -p 5901:5900 --link selenium-hub selenium/node-firefox-debug
docker run -d -P -p 5902:5900 --link selenium-hub selenium/node-chrome-debug
# node the diff host with hub
docker run -d -P -p 5901:5900 -e HUB_HOST=<hub_host> -e HUB_PORT=<hub_port> selenium-hub selenium/node-firefox-debug
docker run -d -P -p 5901:5900 -e HUB_HOST=<hub_host> -e HUB_PORT=<hub_port> selenium-hub selenium/node-chrome-debug
# setup --shm-size
docker run --shm-size=2g -d -P -p 5901:5900 -e HUB_HOST=<hub_host> -e HUB_PORT=<hub_port> selenium-hub selenium/node-firefox-debug
docker run --shm-size=2g -d -P -p 5901:5900 -e HUB_HOST=<hub_host> -e HUB_PORT=<hub_port> selenium-hub selenium/node-chrome-debug
测试
python模块
pip install selenium==3.6.0
测试示例
chrome
import time
from selenium import webdriver
def main():
chrome_options = webdriver.ChromeOptions()
desired_capabilities = chrome_options.to_capabilities()
driver = webdriver.Remote(
command_executor="http://127.0.0.1:4444/wd/hub",
desired_capabilities=desired_capabilities
)
# driver.set_page_load_timeout(300)
driver.set_window_position(1280, 900)
try:
print('begin')
driver.get('https://www.baidu.com')
print driver.page_source
time.sleep(60)
print('finish')
except Exception as e:
print(e)
finally:
driver.close()
driver.quit()
if __name__ == '__main__':
main()
firefox
import time
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
def main():
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.update_preferences()
desired_capabilities = DesiredCapabilities.FIREFOX
driver = webdriver.Remote(
command_executor="http://127.0.0.1:4444/wd/hub",
desired_capabilities=desired_capabilities,
browser_profile=firefox_profile,
)
# driver.set_page_load_timeout(300)
driver.set_window_position(1280, 900)
try:
print('begin')
driver.get('https://www.baidu.com')
print driver.page_source
time.sleep(60)
print('finish')
except Exception as e:
print(e)
finally:
driver.close()
driver.quit()
if __name__ == '__main__':
main()