Skip to content

Instantly share code, notes, and snippets.

@TonnyXu
Created May 11, 2017 09:12
Show Gist options
  • Save TonnyXu/c6aa322e308dcd3cb1a0d60be747c17f to your computer and use it in GitHub Desktop.
Save TonnyXu/c6aa322e308dcd3cb1a0d60be747c17f to your computer and use it in GitHub Desktop.
Workable safari webdriver script for selenium
"""
automation based on selenium webdriver with safari native support
"""
from selenium.webdriver.common.by import By
from selenium import webdriver
import unittest, time
class WebKitFeatureStatusTest(unittest.TestCase):
def setUp(self):
# self.driver = webdriver.Safari(executable_path="/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver")
# self.driver = webdriver.Safari()
self.driver = webdriver.Chrome("./chromedriver")
def tearDown(self):
# self.driver.quit()
self.driver.close()
def test_feature_status_page_search(self):
self.driver.get("https://webkit.org/status/")
time.sleep(2)
# Enter "CSS" into the search box.
search_box = self.driver.find_element_by_id("search")
search_box.send_keys("CSS")
value = search_box.get_attribute("value")
self.assertTrue(len(value) > 0, "point 1:" + value)
search_box.submit()
time.sleep(2)
# Count the results.
feature_count = self.shown_feature_count()
self.assertTrue(feature_count > 0, "point 2:" + str(feature_count))
def test_feature_status_page_filters(self):
self.driver.get("https://webkit.org/status/")
time.sleep(2)
# filters = self.driver.find_element(By.CSS_SELECTOR, "ul#status-filters li input[type=checkbox]")
filters = self.driver.find_elements(By.CSS_SELECTOR, "ul#status-filters li input[type=checkbox]")
self.assertTrue(len(filters) is 6, "expecting 6, but get " + str(len(filters)))
# Make sure every filter is turned off.
for checked_filter in filter(lambda f: f.is_selected(), filters):
checked_filter.click()
# Count up the number of items shown when each filter is checked.
unfiltered_count = self.shown_feature_count()
print("unfiltered_count:" + str(unfiltered_count))
running_count = 0
for filt in filters:
filt.click()
self.assertTrue(filt.is_selected(), "should be checked:" + filt.text)
running_count += self.shown_feature_count()
filt.click()
time.sleep(1)
self.assertTrue(running_count <= unfiltered_count, "running:" + str(running_count) + " unfilt:" + str(unfiltered_count))
def shown_feature_count(self):
return self.driver.execute_script("return document.querySelectorAll('li.feature:not(.is-hidden)').length")
if __name__ == '__main__':
unittest.main()
@TonnyXu
Copy link
Author

TonnyXu commented May 11, 2017

Note, time.sleep(2) is a simple solution to wait the page to finish the animation.

@HarishGargIndia
Copy link

HarishGargIndia commented Oct 1, 2017

Hello Dear,

Can you help me..
i have element -
#input type="checkbox" class="select-all" style="vertical-align: top

i want to click on checkbox so i wrote code below... in selenium python2, browser - chrome
driver.find_elements_by_class_name('input.select-all').click()

i am getting error..
AttributeError: 'list' object has no attribute 'click'

@

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment