def test_infinite_scroll(live_server, driver): """Test infinite scroll. 'live_server' is a fixture function that starts a live server. 'driver' is a fixture fuction that provides access to a headless Chrome web browser via the Selenium library. Fixtures are implemented in conftest.py and reused by many tests. Docs: https://docs.pytest.org/en/latest/fixture.html """ # Delete all likes, comments and posts connection = sqlite3.connect("var/insta485.sqlite3") connection.execute("PRAGMA foreign_keys = ON") connection.execute("DELETE FROM likes") connection.execute("DELETE FROM comments") connection.execute("DELETE FROM posts") connection.commit() connection.close() # Create exactly 11 posts with "fox.jpg". We're making requests directly # to the server-side dynamic pages server here, not using Selenium. login_url = urljoin(live_server.url(), "/accounts/login") session = requests.Session() response = session.get(live_server.url()) assert response.status_code == 200 response = session.post(login_url, data={ "username": "******", "password": "******" }) assert response.status_code == 200 pic_path = utils.TEST_DIR / "testdata/fox.jpg" post_url = urljoin(live_server.url(), "/u/awdeorio/") for _ in range(11): with pic_path.open("rb") as pic: response = session.post(post_url, files={"file": pic}, data={"create_post": "upload new post"}) assert response.status_code == 200 # Log in by reusing test from test_index test_index.test_login(live_server, driver) # Verify 10 most recent posts are on the page (postids 2-11 inclusive) for post_id in range(2, 11 + 1): assert driver.find_elements_by_xpath( "//a[@href='/p/{}/']".format(post_id)) # Scroll to the bottom of the page utils.scroll_to_bottom_of_page(driver) # Verify there are 11 posts now assert driver.find_elements_by_xpath("//a[@href='/p/1/']")
def test_infinite_scroll(driver): """Test infinite scroll.""" # Delete all likes, comments and posts connection = sqlite3.connect("var/insta485.sqlite3") connection.execute("PRAGMA foreign_keys = ON") connection.execute("DELETE FROM likes") connection.execute("DELETE FROM comments") connection.execute("DELETE FROM posts") connection.commit() connection.close() # Create exactly 11 posts with "fox.jpg". We're making requests directly # to the server-side dynamic pages server here, not using Selenium. login_url = urljoin(driver.live_server.url(), "/accounts/login") session = requests.Session() response = session.get(driver.live_server.url()) assert response.status_code == 200 response = session.post( login_url, data={"username": "******", "password": "******"} ) assert response.status_code == 200 pic_filename = os.path.join(utils.TEST_DIR, "testdata/fox.jpg") for _ in range(11): with open(pic_filename, "rb") as pic: post_url = urljoin(driver.live_server.url(), "/u/awdeorio/") response = session.post( post_url, files={"file": pic}, data={"create_post": "upload new post"} ) assert response.status_code == 200 # Log in by reusing test from test_index test_index.test_login(driver) # Verify 10 most recent posts are on the page (postids 2-11 inclusive) for post_id in range(2, 11 + 1): assert driver.find_elements_by_xpath( "//a[@href='/p/{}/']".format(post_id) ) # Scroll to the bottom of the page utils.scroll_to_bottom_of_page(driver) # Verify there are 11 posts now assert driver.find_elements_by_xpath("//a[@href='/p/1/']")
def main(): cfg = get_config() browser = load_browser(cfg) #browser.maximize_window() browser.get('https://www.okcupid.com/match') usernames = set() for age in range(18, 30): update_age(browser, age) time.sleep(pause) scroll_to_bottom_of_page(browser) new_usernames = get_all_usernames(browser) import pdb; pdb.set_trace() usernames = usernames | new_usernames scroll_to_top_of_page(browser) print("Done!")
def test_infinite_scroll_many(driver): """Test many infinite scrolls.""" # Delete all likes, comments and posts connection = sqlite3.connect("var/insta485.sqlite3") connection.execute("PRAGMA foreign_keys = ON") connection.execute("DELETE FROM likes") connection.execute("DELETE FROM comments") connection.execute("DELETE FROM posts") connection.commit() connection.close() # Create exactly 30 posts with "fox.jpg". We're making requests directly # to the server-side dynamic pages server here, not using Selenium. login_url = urljoin(driver.live_server.url(), "/accounts/login") session = requests.Session() response = session.get(driver.live_server.url()) assert response.status_code == 200 response = session.post( login_url, data={"username": "******", "password": "******"} ) assert response.status_code == 200 pic_filename = os.path.join(utils.TEST_DIR, "testdata/fox.jpg") for _ in range(30): with open(pic_filename, "rb") as pic: post_url = urljoin(driver.live_server.url(), "/u/awdeorio/") response = session.post( post_url, files={"file": pic}, data={"create_post": "upload new post"} ) assert response.status_code == 200 # Log in by reusing test from test_index test_index.test_login(driver) # Verify 10 newest posts are included. Note: a side effect of this code is # to tell Selenium to wait until all 10 posts have been loaded. Our # subsequent check for the number of posts depends on all 10 being loaded. for post_id in range(21, 30 + 1): assert driver.find_elements_by_xpath( "//a[@href='/p/{}/']".format(post_id) ) posts = driver.find_elements_by_xpath("//a[contains(@href, '/p/')]") assert len(posts) == 10 # Scroll to the bottom of the page utils.scroll_to_bottom_of_page(driver) # Verify 20 posts for post_id in range(11, 30 + 1): assert driver.find_elements_by_xpath( "//a[@href='/p/{}/']".format(post_id) ) posts = driver.find_elements_by_xpath("//a[contains(@href, '/p/')]") assert len(posts) == 20 # Scroll to the bottom of the page utils.scroll_to_bottom_of_page(driver) # Verify 30 posts for post_id in range(1, 30 + 1): assert driver.find_elements_by_xpath( "//a[@href='/p/{}/']".format(post_id) ) posts = driver.find_elements_by_xpath("//a[contains(@href, '/p/')]") assert len(posts) == 30 # Scroll to the bottom of the page a couple times, no errors utils.scroll_to_bottom_of_page(driver) utils.scroll_to_bottom_of_page(driver) utils.scroll_to_bottom_of_page(driver)