2011-09-29 33 views
27

Tôi sử dụng Selenium RC để thử nghiệm. Bây giờ để thực hiện kiểm tra tải, tôi muốn chạy các trường hợp thử nghiệm song song. Có cách nào để chạy chúng mà không cần mở trình duyệt không?Thử nghiệm selen mà không cần trình duyệt

+0

thể trùng lặp của [Có thể ẩn các trình duyệt trong Selenium RC?] (Http://stackoverflow.com/questions/1418082/is-it-possible-to-hide-the- browser-in-selenium-rc) –

Trả lời

4

Để thiết lập trên Centos (làm tất cả những cài đặt như root)

Cài đặt pip Tải xuống https://bootstrap.pypa.io/get-pip.py

python get-pip.py 

Cài đặt selen Nếu bạn có pip trên hệ thống của mình, bạn có thể chỉ cần cài đặt hoặc nâng cấp các ràng buộc Python: pip install -U selenium

Cách khác, bạn có thể tải xuống phân phối nguồn từ PyPI (ví dụ: selen-2.53.1.tar.gz), bỏ lưu trữ nó, và chạy:

python setup.py install 

cài đặt chương trình: pyvirtualdisplay

pip install pyvirtualdisplay 

yum install Xvfb libXfont Xorg 

Sau đó, sửa đổi kịch bản của bạn để thêm các dòng chữ in đậm trong ** và * *

**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 

lớp 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) 
6

Bạn có thể chạy Selenium không đầu, hãy nhìn vào câu hỏi này/câu trả lời: Is it possible to hide the browser in Selenium RC?

Đặc biệt đối với các bài kiểm tra tải trọng hiệu suất, bạn nên có một cái nhìn tại Apache JMeter.

+0

Tôi đã thử Apache JMeter để thử nghiệm tải ứng dụng GWT. Nhưng nó không khá thành công. – Mohyt

43

Có. Chỉ cần install PhantomJS.

Sau đó, thay đổi dòng này:

driver = webdriver.Firefox()

tới:

driver = webdriver.PhantomJS()

Phần còn lại của mã của bạn sẽ không cần phải được thay đổi và không có trình duyệt sẽ mở ra.


Đối với mục đích gỡ lỗi, sử dụng driver.save_screenshot('screen.png') tại các bước khác nhau của mã của bạn, hoặc chỉ chuyển về Firefox một lần nữa:

if os.getenv("environment") == "production": 
    driver = webdriver.PhantomJS() 
else: 
    driver = webdriver.Firefox() 
+2

đây phải là câu trả lời –

+0

Câu trả lời đơn giản thường là tốt nhất! Cảm ơn bạn. – hod

0

Luôn tuân theo Tài liệu. Đây là những gì selenium doc nói. Nó cung cấp standalone jar.

  • Tải xuống bình độc lập. Và chạy nó với lệnh

    java -jar selenium-server-standalone.jar 
    
  • Bây giờ bạn sẽ thấy một máy chủ stanalone bắt đầu.

  • Bây giờ, hãy thiết lập trình quản trị web của bạn như dưới đây và phần còn lại sẽ như cũ.

    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True}) 
    
  • Mã tóm tắt sẽ như thế nào.

    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() 
    
Các vấn đề liên quan