-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite_scroll_unittest.py
55 lines (42 loc) · 1.92 KB
/
infinite_scroll_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class TestInfiniteScroll(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('https://the-internet.herokuapp.com')
self.driver.set_window_size(1000, 850)
self.driver.implicitly_wait(10)
def tearDown(self):
self.driver.quit()
def test_infinite_scroll(self):
infinite_scroll_page = self.driver.find_element(By.LINK_TEXT, "Infinite Scroll")
infinite_scroll_page.click()
# make a nested function in order to call it multiple times
# allows the user to scroll as many times as wanted during test
def scroll_down(elem, num):
for _ in range(num):
time.sleep(.01)
elem.send_keys(Keys.PAGE_DOWN)
# wait time between each scroll down - can be modified, counted in seconds
scroll_pause_time = 0.3
body = self.driver.find_element(By.XPATH, "/html/body")
# get the initial scroll value
initial_scroll_value = body.get_attribute("scrollHeight")
prev_height = initial_scroll_value
# range to be able to tell how many scrolls would be performed
for i in range(10):
scroll_down(body, 1)
time.sleep(scroll_pause_time)
current_height = body.get_attribute("scrollHeight")
# Check if scrollable space got larger
if current_height == prev_height:
break
prev_height = current_height
# get a final value and print for comparison reasons
final_scroll_value = body.get_attribute("scrollHeight")
print(f"Initial Value : {initial_scroll_value} and Final Value : {final_scroll_value}")
if __name__ == "__main__":
unittest.main()