没有浏览器的Selenium测试

问题描述 投票:33回答:6

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

python selenium selenium-webdriver selenium-rc load-testing
6个回答
5
投票

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

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

python get-pip.py

安装selenium如果你的系统有点,你可以简单地安装或升级Python绑定: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)

53
投票

是。只是install 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()

6
投票

你可以运行Selenium无头,看看这个问题/答案:Is it possible to hide the browser in Selenium RC?

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


0
投票

始终遵循文档。这是selenium doc所说的。它提供了standalone jar

  • 下载独立jar。并使用命令运行它 java -jar selenium-server-standalone.jar
  • 现在您将看到一个独立服务器已启动。
  • 现在设置你的webdriver如下,休息部分将是原样。 driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
  • 摘要代码将是。 from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.keys import Keys driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True}) driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.clear() elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." not in driver.page_source driver.close()

0
投票

这是可能的,但不是标准的firefox驱动程序/ chrome /等。

您需要安装PhantomJS。只需将WebDriver分配给phantomJS驱动程序的实例:

driver = webdriver.PhantomJS()

如果您现在运行代码,则不会打开任何浏览器窗口。


0
投票

试试这个

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
© www.soinside.com 2019 - 2024. All rights reserved.