在jenkins中执行python测试时找不到Chrome二进制文件

时间:2017-11-11 01:37:14

标签: python selenium jenkins

我正在使用python和selenium来执行我的应用程序的测试表单。 代码是用eclipse NEON编写的,当我从eclipse执行时它工作正常。 但是,当我使用jenkins运行相同的代码时,它说:找不到chrome二进制文件。 我在jenkins中所做的是执行Windows bact命令。 我写了一个批处理代码,如:

C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe C:\Users\Me\NEON\workspace\Test1\test-package\test.py

我将此代码保存到名为run_test1.bat的文件中 然后我把这行放在jenkins中:

call "C:\Users\Me\NEON\workspace\Test1\test-package\run_test1.bat"

这是jenkins的错误堆栈:

20:22:57 C:\Users\Me\NEON\workspace\Test1\test-package>C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe C:\Users\Me\NEON\workspace\Test1\test-package\test.py 
20:23:03 E
20:23:03 ======================================================================
20:23:03 ERROR: test_x (__main__.Login)
20:23:03 ----------------------------------------------------------------------
20:23:03 Traceback (most recent call last):
20:23:03   File "C:\Users\Me\NEON\workspace\Test1\test-package\test.py", line 33, in setUp
20:23:03     self.driver = webdriver.Chrome("C:\\Selenium-driver\\chromedriver.exe")
20:23:03   File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
20:23:03     desired_capabilities=desired_capabilities)
20:23:03   File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
20:23:03     self.start_session(desired_capabilities, browser_profile)
20:23:03   File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
20:23:03     response = self.execute(Command.NEW_SESSION, capabilities)
20:23:03   File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
20:23:03     self.error_handler.check_response(response)
20:23:03   File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
20:23:03     raise exception_class(message, screen, stacktrace)
20:23:03 selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
20:23:03   (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)
20:23:03 
20:23:03 
20:23:03 ----------------------------------------------------------------------
20:23:03 Ran 1 test in 5.095s
20:23:03 
20:23:03 FAILED (errors=1)
20:23:03 
20:23:03 C:\Users\Me\NEON\workspace\Test1\test-package>exit 1 
20:23:03 Build step 'Execute Windows batch command' marked build as failure
20:23:03 Finished: FAILURE

我的python代码就是这样开始的:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities    
import unittest, time, re
from util import Util
import logging 
from selenium.webdriver.remote.remote_connection import LOGGER
LOGGER.setLevel(logging.ERROR)


class Login(unittest.TestCase):
    def setUp(self):
        '''options = webdriver.ChromeOptions()
        options.binary_location = ("C://Users//Me//AppData//Local//Google//Chrome//Application//")
        chrome_driver_binary = ("C://Selenium-driver//chromedriver.exe")
        self.driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)'''


        self.driver = webdriver.Chrome("C:\\Selenium-driver\\chromedriver.exe")

有人可以帮我解决詹金斯的这个问题。 我正在使用Jenkins ver。 2.89

1 个答案:

答案 0 :(得分:0)

这可能是你的jenkins slave配置中的一个问题。

Jenkins需要作为标准的Windows用户帐户登录才能运行您的脚本。您需要确保用户能够运行您的脚本(不要在个人用户帐户目录下使用chrome二进制文件的路径。为所有用户安装chrome)。

selenium脚本需要一个桌面来启动浏览器,所以不要忘记配置你的VM自动登录到这个用户帐户(桌面将可用于你的jenkins奴隶)。

有关jenkins slave的配置,请参阅此处的文档:Step by step guide to set up master and slave machines on Windows