Basic Testing with Selenium

Basic Testing with Selenium

2021, Jan 10    

Using selenium can be fun. In this example, you’ll load a webpage and interact with different types of UI elements (sliders, checkbox, etc.,)

from selenium import webdriver
from selenium.webdriver import ActionChains

from time import sleep

# create webdriver object 
DRIVER_PATH = 'Z:/chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

# load browswer
driver.get("https://www.w3schools.com/howto/howto_js_rangeslider.asp")

# to maximize the browser window
# driver.maximize_window()

# allow time for website to load visually
sleep(2.75)

# imitate user scrolling down page
driver.execute_script("window.scrollTo(0,25)")
sleep(.05)
driver.execute_script("window.scrollTo(0,30)")
sleep(.15)
driver.execute_script("window.scrollTo(0,60)")
driver.execute_script("window.scrollTo(0,85)")
sleep(.05)
driver.execute_script("window.scrollTo(0,120)")
sleep(.15)
driver.execute_script("window.scrollTo(0,135)")
driver.execute_script("window.scrollTo(0,165)")
sleep(1)
driver.execute_script("window.scrollTo(0,220)")
sleep(.05)
driver.execute_script("window.scrollTo(0,245)")
sleep(.15)
driver.execute_script("window.scrollTo(0,270)")
driver.execute_script("window.scrollTo(0,350)")
sleep(.05)
driver.execute_script("window.scrollTo(0, 375)") 

# identifying the source elements
source1 = driver.find_element_by_id("id1");
source2 = driver.find_element_by_id("id2");
source3 = driver.find_element_by_id("id3");

# action chain object creation
action = ActionChains(driver)
action.pause(1.5)
action.drag_and_drop_by_offset(source2, 100, 0) 
action.pause(1)
action.drag_and_drop_by_offset(source1, -25, 0) 
action.pause(1)
action.drag_and_drop_by_offset(source3, 33, 0)

# perform the operation 
action.perform()