没有浏览器的Selenium测试

Moh*_*hyt 33 python selenium load-testing selenium-rc selenium-webdriver

我使用Selenium RC进行测试.现在要执行负载测试,我想运行并行测试用例.有没有办法在不打开浏览器的情况下运行它们?

Sté*_*ert 54

是.只需安装PhantomJS即可.

然后,更改此行:

driver = webdriver.Firefox()

至:

driver = webdriver.PhantomJS()

其余代码不需要更改,也不会打开任何浏览器.


出于调试目的,请driver.save_screenshot('screen.png')在代码的不同步骤中使用,或者再次切换回Firefox:

if os.getenv("environment") == "production":
    driver = webdriver.PhantomJS()
else:
    driver = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

  • 这应该是答案 (5认同)
  • 最简单的答案通常是最好的!谢谢。 (2认同)
  • UserWarning:PhantomJS的硒支持已被弃用,请使用无头版本的Chrome或Firefox来代替警告。warn('PhantomJS的硒支持已被弃用,请使用无头' (2认同)

cod*_*der 7

试试这个代码:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
Run Code Online (Sandbox Code Playgroud)


Dag*_*Dag 6

你可以运行Selenium无头,看看这个问题/答案:是否有可能在Selenium RC中隐藏浏览器?

特别是对于性能负载测试,您应该看看 Apache JMeter.


小智 5

在Centos上设置(以root用户身份完成所有安装)

安装pip下载https://bootstrap.pypa.io/get-pip.py

python get-pip.py
Run Code Online (Sandbox Code Playgroud)

安装selenium如果你的系统有点,你可以简单地安装或升级Python绑定:pip install -U selenium

或者,您可以从PyPI下载源代码分发(例如selenium-2.53.1.tar.gz),取消归档,然后运行:

python setup.py install
Run Code Online (Sandbox Code Playgroud)

安装程序:pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg
Run Code Online (Sandbox Code Playgroud)

然后修改脚本以在**和**中添加粗线

**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)
Run Code Online (Sandbox Code Playgroud)

class SeleniumDemo(unittest.TestCase):

python get-pip.py
Run Code Online (Sandbox Code Playgroud)


Hun*_*ter 5

由于 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/')
Run Code Online (Sandbox Code Playgroud)