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?
问题:
回答1:
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 to add the bold lines within ** and **
**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)
回答2:
Yes. Just install PhantomJS.
Then, change this line:
driver = webdriver.Firefox()
to:
driver = webdriver.PhantomJS()
The rest of your code won't need to be changed and no browser will open.
For debugging purposes, use driver.save_screenshot('screen.png')
at different steps of your code or just switch back to Firefox again:
if os.getenv("environment") == "production":
driver = webdriver.PhantomJS()
else:
driver = webdriver.Firefox()
回答3:
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.
回答4:
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()