硒测试可在本地计算机上运行,但在詹金斯上失败 [英] Selenium test works in local machine, but fails on Jenkins
问题描述
我的测试在我的机器上工作正常.但是,在詹金斯身上,出现以下错误:
My test works just fine on my machine. However, on Jenkins, I have the following error:
Traceback (most recent call last):
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/django/test/utils.py", line 216, in inner
return test_func(*args, **kwargs)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/twist/tests/interface/test_hello.py", line 42, in test_login
open_login_modal_btn.click()
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/var/lib/jenkins/jobs/twist-press-ricardo-fork/workspace/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpl_3zEO/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
我按如下所示安装了依赖项:
I installed dependencies as it follows:
$ sudo apt-get install xvfb python-pip
$ sudo pip install pyvirtualdisplay
这是我的测试代码:
class MySeleniumTests(LiveServerTestCase):
fixtures = ['user-data.json']
def setUp(self):
self.display = Display(visible=0, size=(800, 600))
self.display.start()
group = Group.objects.get_or_create(name="Test")[0]
user_adm = User.objects.create(username="adm",
is_active=True,
is_staff=True,
is_superuser=True)
user_adm.set_password("123")
user_adm.groups.add(group)
user_adm.save()
self.selenium.get('%s' % (self.live_server_url))
self.browser = self.selenium
def tearDown(self):
self.display.stop()
@classmethod
def setUpClass(cls):
super(MySeleniumTests, cls).setUpClass()
cls.selenium = WebDriver()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(MySeleniumTests, cls).tearDownClass()
@override_settings(DEBUG=True)
def test_login(self):
open_login_modal_btn = self.browser.find_element_by_xpath("//a[@class='btn btn-nav']")
open_login_modal_btn.click()
# Login info
username_html_id = "lm-email"
password_html_id = "lm-password"
login_value = "adm"
password_value = "123"
username = self.browser.find_element_by_id(username_html_id)
password = self.browser.find_element_by_id(password_html_id)
# Sending login info
username.send_keys(login_value)
password.send_keys(password_value)
desired_url = self.live_server_url + "/panel/"
form = self.browser.find_element_by_xpath('//form[@id="login-modal"]')
form.submit()
time.sleep(5)
# Testing if successful login
# Logging in
current_url = self.browser.current_url
self.assertEqual(current_url, desired_url)
是否有任何解决方法的线索?任何有关如何调试/修复的帮助将不胜感激! :-)
Are there any clues on how to fix it? Any help on how to debug/fix it will be appreciated! :-)
-
我的问题已解决,如果您遇到类似的问题,请查看: WebDriver单击()vs JavaScript click()这就是我的问题.
My issue is solved, if you are facing similar problem, take a look at: WebDriver click() vs JavaScript click() It was the case in my problem.
推荐答案
等待使元素可见或可点击通常有助于克服以下问题:
Waiting for element to be visible or clickable usually helps to overcome issues like this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(self.browser, 10)
open_login_modal_btn = wait.until(
EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-nav']"))
)
open_login_modal_btn.click()
如果这没有帮助,请尝试以下操作:
If this does not help here are things to try:
-
滚动到视图点击前的元素:
open_login_modal_btn = self.browser.find_element_by_xpath("//a[@class='btn btn-nav']")
self.browser.execute_script("arguments[0].scrollIntoView();", open_login_modal_btn)
移动到元素并通过
move to element and click via Browser Actions:
actions = ActionChains(self.browser)
actions.move_to_element(open_login_modal_btn).click().perform()
最大化浏览器窗口:
maximize the browser window at the beginning of the test:
self.browser.maximize_window()
通过JavaScript单击 ( WebDriver click()与JavaScript click()):
self.browser.execute_script("arguments[0].click();", open_login_modal_btn)
这篇关于硒测试可在本地计算机上运行,但在詹金斯上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!