30 Days Of Python πŸ‘¨β€πŸ’» - Day 29 - Automation Testing

Introduction 

 
This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here:
Today I explored end-to-end Browser Automation Tests using Python by creating a basic automation testing project.
 
When it comes to implementing browser automation, Selenium is one of the most popular and widely used libraries. The Selenium Python library makes it very easy to use it with Python and run scripts to perform automated tests on websites.
 
Automation tests are essential for testing web applications in a real-world scenario, just as how a user would normally use the application. It is also used to perform cross-browser tests to ensure the application works on all target browsers.
 
I created a basic project to understand and implement the basics of browser automation testing with Selenium.
 

Creating a project

 
I created a new directory python-selenium and added a virtual environment to the project.
 
python -m venv venv
 

Installing Selenium

 
The next step is to install the Selenium package which can be installed in the project environment using the command:
 
pip install selenium
 

Installing the Drivers

 
Selenium uses drivers to interface with the browser. So browser-specific drivers need to be installed so that Selenium is able to create the browser instance for running the automation tests.
 
| Chrome: | https://sites.google.com/a/chromium.org/chromedriver/downloads |
| Edge: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
| Firefox: | https://github.com/mozilla/geckodriver/releases |
| Safari: | https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
I downloaded the Firefox driver for this particular project. The driver needs to be the executable path.
 
(In Windows 10, place the driver file in the path where the Python application is installed, or any other location whose path is there in the environment variables)
 

Creating a Sample Automation Script

 
I created a Python script file automation.py in the root project directory.
 
Here is a simple code for opening a webpage using Selenium:
 
automation.py
  1. from selenium import web-driver  
  2.   
  3. browser = webdriver.Firefox()  
  4. browser.get('https://tabandspace.com')  
Selenium Cheat-sheet: Here is a cheat-sheet for commonly-used Selenium methods:
  1. # import selenium  
  2. from selenium import web-driver  
  3.   
  4. #create a chrome driver  
  5. chromedriver = "C:/tests/chromedriver.exe"  
  6. driver = webdriver.Chrome(executable_path = chromedriver)  
  7.   
  8. #create a firefox driver  
  9. geckodriver = "C:/tests/geckodriver.exe"  
  10. driver = webdriver.Firefox(executable_path = geckodriver)  
  11.   
  12. #create an IE driver  
  13. iedriver = "C:/tests/IEDriverServer.exe"  
  14. driver = webdriver.Firefox(executable_path = iedriver)  
  15.   
  16. #open a website  
  17. the_url = "https://dev.to"  
  18. driver.get(the_url)  
  19.   
  20. #finding an element by id  
  21. the_id = 'register'  
  22. element = driver.find_element_by_id(the_id)  
  23.   
  24. #finding an element by name  
  25. the_name = 'register'  
  26. element = driver.find_element_by_id(the_name)  
  27.   
  28. #findding an element by class name  
  29. the_class_name = 'nav-link'  
  30. element = driver.find_element_by_class_name(the_class_name)  
  31.   
  32. #finding an element by tag name  
  33. the_tag_name = 'a'  
  34. element = driver.find_element_by_tag_name(the_tag_name)  
  35.   
  36. #finding an element by link text ( anchor elements)  
  37. the_link_text = 'Sign Up'  
  38. element = driver.find_element_by_link_text(the_link_text)  
  39.   
  40. #finding an element by partial link text (anchor elements)  
  41. the_partial_link_text = 'Sign'  
  42. element = driver.find_element_by_partial_link_text(the_partial_link_text)  
  43.   
  44. #finding element using css selectors  
  45. the_css_selector = 'a[href="/sign-up"]'  
  46. element = driver.find_element_by_css_selector(the_css_selector)  
  47.   
  48. #finding element using x-path  
  49. the_xpath = '//a[@href = "/sign-up"]'  
  50. element = driver.find_element_by_xpath(the_xpath)  
  51.   
  52. #clicking an element  
  53. the_id = 'register'  
  54. element = driver.find_element_by_id(the_id)  
  55. element.click()  
  56.   
  57. #type inside an element  
  58. the_id = 'email'  
  59. the_email = '[email protected]'  
  60. element = driver.find_element_by_id(the_id)  
  61. element.send_keys(the_email)  
  62.   
  63. #select an option from select elements  
  64. the_id = 'country'  
  65. element = driver.find_element_by_id(the_id)  
  66. select_element = Select(element)  
  67. select_element.select_by_visible_text('India')  
  68.   
  69. #taking screenshots  
  70. the_path = 'C:/tests/screenshots/1.png'  
  71. driver.save_screenshot(the_path)  
  72.   
  73. #uploading a file  
  74. the_file_path = 'C:/tests/files/test.pdf'  
  75. the_id = 'upload_button'  
  76. element = driver.find_element_by_id(the_id)  
  77. element.send_keys(the_file_path)  
  78.   
  79. #execute javascript  
  80. js_code = 'document.getElementById("pop-up").remove()'  
  81. driver = execute_script(js_code)  
  82.   
  83. #switch to iframe  
  84. the_iframe_id = 'payment_section'  
  85. the_element_id = 'card_number'  
  86. the_iframe = driver.find_element_by_id(the_iframe_id)  
  87. driver.switch_to.frame(the_iframe)  
  88. element = driver.find_element_by_id(the_element_id)  
  89. element.send_keys('23232323')  
  90. driver.switch_to.default_content()  
  91.   
  92. #switch to next tab  
  93. global nextTab  
  94. global currentTab  
  95. nextTab = currentTab + 1  
  96. driver.switch_to_window(driver.window_handles[nextTab])  
  97. currentTab = currentTab + 1  
  98.   
  99. #swtich to previous tab  
  100. global previousTab  
  101. global currentTab  
  102. previousTab = currentTab - 1  
  103. driver.switch_to_window(driver.window_handles[previousTab])  
  104. currentTab = currentTab - 1  
  105.   
  106. #close tab  
  107. driver.close()  
  108.   
  109. #close alert  
  110. driver.switch_to.alert.accept()  
  111.   
  112. #refresh  
  113. driver.refresh()  
  114.   
  115. #hover  
  116. the_id = "register"  
  117. the_element = driver.find_element_by_id(the_id)  
  118. hover = ActionChains(driver).move_to_element(the_element)  
  119. hover.perform()  
  120.   
  121. #right click  
  122. the_id = "register"  
  123. the_element = driver.find_element_by_id(the_id)  
  124. right_click = ActionChains(driver).context_click(the_element)  
  125. right_click.perform()  
  126.   
  127. #set window size  
  128. driver.set_window_size(16001200)  
  129.   
  130. #press key  
  131. the_id = 'register'  
  132. element = driver.find_element_by_id(the_id)  
  133. element.send_keys(Keys.RETURN)  
  134.   
  135. #configuring element load timeout  
  136. from selenium.webdriver.support.ui import WebDriverWait  
  137.   
  138. the_id = 'register'  
  139. WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, the_id)))  
  140.   
  141. #emulating mobile device  
  142. google_pixel_3_xl_user_agent = 'Mozilla/5.0 (Linux; Android 9.0; Pixel 3 XL Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36'  
  143. pixel_3_xl_emulation = {  
  144.    "deviceMetrics": {  
  145.       "width"411,  
  146.       "height"731,  
  147.       "pixelRatio"3  
  148.    },  
  149.    "userAgent": google_pixel_3_xl_user_agent  
  150. }  
  151. options = webdriver.ChromeOptions()  
  152. options.add_experimental_option("mobileEmulation", pixel_3_xl_emulation)  
  153. driver = webdriver.Chrome(  
  154.    executable_path = chromedriver,  
  155.    chrome_options = options)  

Configure Automation Testing in a Build Pipeline

 
I found a great article that describes the steps to implement web automation scripts in a build pipeline such as Github Workflow. Selenium Base is a great wrapper on top of selenium that makes it easy to install drivers and add the automation script to a build pipeline.
 
https://dev.to/seleniumbase/running-browser-tests-from-github-workflows-with-seleniumbase-1oic
 
That’s all for today. Tomorrow is the last day of this challenge, where I shall be sharing the various Python resources I have collected during this month while learning Python.
 
Have a great one!


Similar Articles