无浏览器的Selenium测试
我在用Selenium RC进行测试。现在为了进行负载测试,我想同时运行多个测试用例。有没有办法在不打开浏览器的情况下运行它们呢?
10 个回答
14
因为PhantomJS已经不再被支持了,所以使用无头版本的Firefox是一个不错的选择。
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get('https://www.google.com/')
131
Chrome现在有一种无头模式:
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
8
在Centos上设置环境(所有安装都要以root身份进行)
首先安装pip,下载这个链接的文件:https://bootstrap.pypa.io/get-pip.py
python get-pip.py
安装selenium 如果你的系统上已经有pip了,可以直接安装或升级Python的selenium库: pip install -U selenium
另外,你也可以从PyPI下载源代码包(比如selenium-2.53.1.tar.gz),解压后运行:
python setup.py install
安装程序:pyvirtualdisplay
pip install pyvirtualdisplay
yum install Xvfb libXfont Xorg
然后修改你的脚本,添加加粗的部分,放在**和**之间
**from pyvirtualdisplay import Display**
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class SeleniumDemo(unittest.TestCase):
def setUp(self):
**self.display = Display(visible=0, size=(800, 600))
self.display.start()**
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.soastastore.com/"
self.verificationErrors = []
self.accept_next_alert = True
def tearDown(self):`enter code here`
self.driver.quit()
***self.display.stop()***
self.assertEqual([], self.verificationErrors)