2

コマンドでSeleniumサーバーハブを起動します

java -jar selenium-server-standalone-2.33.0.jar -role hub

コマンドによるSeleniumサーバーノード

java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=htmlunit

次に、コードを実行しようとしています:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
server =  'http://localhost:4444/wd/hub'
dc = DesiredCapabilities.HTMLUNIT
browser = webdriver.Remote(server, dc)
browser.get('http://localhost:8000')

この後は大丈夫です。しかし、Jenkins テストを開始しようとすると:

from django.test import TestCase, LiveServerTestCase
from selenium.webdriver.common import proxy
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.webdriver import WebDriver

class SeleniumTest(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        p = proxy.Proxy({
        'proxyType': proxy.ProxyType().MANUAL,
        'httpProxy': '127.0.0.1:4444',
        })

        capabilities = DesiredCapabilities().HTMLUNIT
        cls.selenium = WebDriver(desired_capabilities=capabilities, proxy=p)
        super(SeleniumTest, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super(SeleniumTest, cls).tearDownClass()

    def test_javascript_basket(self):
        self.selenium.get('http://localhost:8000')

トレースバックに含まれる次のエラーが表示されます。

WebDriverException: メッセージ: u'\n\n\nエラー: 要求された URL を取得できませんでした\n\n\n\n

エラー

\n

要求されたURLを取得できませんでした

\n\n
\n\n\n

URL の取得中に次のエラーが発生しました: a href="http://localhost:4444/wd/hub/session" localhost:4444/wd/hub/session ap\n\n\n

127.0.0.1 への接続に失敗しました。

\n
\n\nシステムが返しました: (111) 接続が拒否されました

\n\n

リモート ホストまたはネットワークがダウンしている可能性があります。もう一度リクエストしてください。

\n\n

キャッシュ管理者はウェブマスターです。

\n\n
\n\n\n
\n\n

2013 年 6 月 10 日月曜日 04:36:42 GMT によって生成されたローカルホスト (squid/3.1.6)

\n\n\n'

どうしたの?Jenkins テストから Selenium サーバーに接続できないのはなぜですか?

python==2.7.3
Django==1.5
django-jenkins==0.14.0
selenium==2.33.0

更新: HTMLUNIT の代わりに Firefox WebDriver を使用している場合、Firefox は行の後に開きます

cls.selenium = WebDriver(desired_capabilities=capabilities, proxy=p)

、しかし後で上記の例外が発生します。

解決済みsetUpClass()メソッド に追加するだけです。

import os
. . .
    def setUpClass(cls):
        os.environ['NO_PROXY'] = '127.0.0.1'
4

1 に答える 1

3

私はこの方法で問題を解決しました ( HTMLUNITの代わりにphantom-jsを使用しました。コードの唯一の安定したバージョンであるためです)。

from django.test import LiveServerTestCase
from selenium import webdriver
from os import environ


class SeleniumTestCase(LiveServerTestCase):
    __test__ = False    

    @classmethod
    def setUpClass(cls):
        environ['NO_PROXY'] = '127.0.0.1'  # The key point

        cls.selenium = webdriver.PhantomJS(service_args=['--proxy-type=none'])    
        super(SeleniumTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.close()
        cls.selenium.quit()
        super(SeleniumTestCase, cls).tearDownClass()


class TestFoo(SeleniumTestCase):    
    def setUp(self):
        # do something before every test method runs
        pass
    def test_foo(self):
        # test
        pass
于 2013-10-02T10:06:06.320 に答える