Skip to content

Instantly share code, notes, and snippets.

@hjwp
Last active December 17, 2015 02:59
Show Gist options
  • Save hjwp/5539906 to your computer and use it in GitHub Desktop.
Save hjwp/5539906 to your computer and use it in GitHub Desktop.
Minimal repro for selenium control-key bug
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
class TestCtrlKey(unittest.TestCase):
def tearDown(self):
self.browser.quit()
def test_firefox_send_keys(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
self.assert_ctrlkey_doesnt_break_subsequent_clicks()
def test_chrome_send_keys(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
self.assert_ctrlkey_doesnt_break_subsequent_clicks()
def test_firefox_actionchains(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
self.assert_actionchains_work()
def test_chrome_actionchains(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
self.assert_actionchains_work()
def assert_ctrlkey_doesnt_break_subsequent_clicks(self):
# sanity-check to start with
self.browser.get("http://www.seleniumhq.org/")
self.browser.find_element_by_link_text("About").click()
self.browser.find_element_by_link_text("History")
# but send_keys including a control breaks
self.browser.get("http://www.seleniumhq.org/")
self.browser.find_element_by_id("q").send_keys(Keys.CONTROL, "c")
self.browser.find_element_by_link_text("About").click() # will open in new tab if ctrl persists
self.browser.find_element_by_link_text("History") # will fail if about page is in new tab
def assert_actionchains_work(self):
# actionchains with explicit keydown and keyup are fine
self.browser.get("http://www.seleniumhq.org/")
ActionChains(self.browser).key_down(Keys.CONTROL).perform()
self.browser.find_element_by_id("q").send_keys("c")
ActionChains(self.browser).key_up(Keys.CONTROL).perform()
self.browser.find_element_by_link_text("About").click()
self.browser.find_element_by_link_text("History")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment