Selenium test works in local machine, but fails on Jenkins

こ雲淡風輕ζ 提交于 2021-02-19 06:10:43

问题


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

Here is my test code:

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! :-)

-- EDIT:

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.


回答1:


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:

  • scroll into view of the element before clicking:

    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()
    
  • click via JavaScript (WebDriver click() vs JavaScript click()):

    self.browser.execute_script("arguments[0].click();", open_login_modal_btn)
    


来源:https://stackoverflow.com/questions/34645326/selenium-test-works-in-local-machine-but-fails-on-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!