无需浏览器的 Selen 测试

发布于 2024-12-07 13:20:04 字数 75 浏览 0 评论 0原文

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

I use Selenium RC for testing. Now to perform a load test, I want to run parallel test cases.
Is there any way to run them without opening a browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

树深时见影 2024-12-14 13:20:04

Chrome 现在有无头模式:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

Chrome now has a headless mode:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
你对谁都笑 2024-12-14 13:20:04

由于 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/')

Since PhantomJS has been deprecated, using headless versions of Firefox would be a viable option.

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/')
浅暮の光 2024-12-14 13:20:04

试试这个代码:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

Try this code:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
拧巴小姐 2024-12-14 13:20:04

在 Centos 上进行设置(以 root 身份进行所有安装)

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

python get-pip.py

安装selenium
如果您的系统上有 pip,您可以简单地安装或升级 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):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)

To set up on Centos (do all installation as root)

Install pip Download https://bootstrap.pypa.io/get-pip.py

python get-pip.py

Installing selenium
If you have pip on your system, you can simply install or upgrade the Python bindings:

pip install -U selenium

Alternately, you can download the source distribution from PyPI (e.g. selenium-2.53.1.tar.gz), unarchive it, and run:

python setup.py install

install the program: pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg

Then modify your script and get this.

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):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)
迟月 2024-12-14 13:20:04

你可以无头运行Selenium,看看这个问题/答案: Is it possible to hide Selenium RC 中的浏览器?

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

You can run Selenium headless, take a look at this question/answer: Is it possible to hide the browser in Selenium RC?

Especially for performance load tests, you should have a look at
Apache JMeter.

终难愈 2024-12-14 13:20:04

始终遵循文档。这是 selenium doc 所说的内容。它提供了一个独立jar

  • 下载独立的 jar。并使用命令运行它

    java -jar selenium-server-standalone.jar
    
  • 现在您将看到一个独立服务器已启动。

  • 现在像下面一样设置你的网络驱动程序,其余部分将保持原样。

    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capability={'browserName':'htmlunit','version':'2',' javascriptEnabled': True})
    
  • 摘要代码如下。

    从 selenium 导入 webdriver
    从 selenium.webdriver.common.desired_capability 导入 DesiredCapability
    从 selenium.webdriver.common.keys 导入密钥
    驱动程序 = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', 
    desired_capability={'浏览器名称': 'htmlunit', '版本': '2', 
    'javascriptEnabled': True})
    driver.get("http://www.python.org")
    在 driver.title 中断言“Python”
    elem = driver.find_element_by_name("q")
    elem.clear()
    elem.send_keys("pycon")
    elem.send_keys(Keys.RETURN)
    断言“未找到结果”。不在 driver.page_source 中
    驱动程序.close()
    

Always follow the Documentation. Here is what selenium doc says. It provide a standalone jar.

  • Download the standalone jar. And run it with command

    java -jar selenium-server-standalone.jar
    
  • Now you will see a stanalone server started.

  • Now set up your webdriver like below and rest part will be as it is.

    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
    
  • Summary code will be like.

    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()
    
屋檐 2024-12-14 13:20:04

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

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

driver = webdriver.PhantomJS()

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

It is possible, but not with the standard firefox driver / chrome / etc.

You would need to install PhantomJS. Just assign your WebDriver to an instance of phantomJS driver:

driver = webdriver.PhantomJS()

If you run your code now, no browser window will be opened.

聊慰 2024-12-14 13:20:04

您可以简单地传递一个“headless”参数来测试 selenium,而无需打开浏览器。

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

该代码片段将为您提供您想要的内容。

You can simply pass an argument "headless" to test selenium without opening the browser.

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

This code snippet will provide you exactly what you want.

病女 2024-12-14 13:20:04

如果您不想打开网络浏览器,可以导入 Options

from selenium import webdriver   # for webdriver
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser

然后在代码中:

option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)

并继续程序的其余部分。

You can import Options if you don't want to open a web browser.

from selenium import webdriver   # for webdriver
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser

Then in the code:

option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)

And continue with the rest of the program.

—━☆沉默づ 2024-12-14 13:20:04

要求:

sudo apt-get install xvfb
pip install selenium
pip install PyVirtualDisplay

从下面的链接下载 chrome 驱动程序二进制文件并粘贴到驱动程序目录中: https://sites.google.com/a/chromium.org/chromedriver/downloads" rel="nofollow noreferrer">https:// /sites.google.com/a/chromium.org/chromedriver/downloads

代码:

from selenium import webdriver
from pyvirtualdisplay import Display

with Display(visible=False, size=(800, 600)):
    browser = webdriver.Chrome('drivers/chromedriver')
    browser.get('https://www.example.com')
    print(browser.page_source)
    browser.quit()

requirements:

sudo apt-get install xvfb
pip install selenium
pip install PyVirtualDisplay

download chrome driver binary from below link and paste into drivers directory: https://sites.google.com/a/chromium.org/chromedriver/downloads

code:

from selenium import webdriver
from pyvirtualdisplay import Display

with Display(visible=False, size=(800, 600)):
    browser = webdriver.Chrome('drivers/chromedriver')
    browser.get('https://www.example.com')
    print(browser.page_source)
    browser.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文