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. browser = webdriver.Firefox()
  3. 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. #create a chrome driver
  4. chromedriver = "C:/tests/chromedriver.exe"
  5. driver = webdriver.Chrome(executable_path = chromedriver)
  6. #create a firefox driver
  7. geckodriver = "C:/tests/geckodriver.exe"
  8. driver = webdriver.Firefox(executable_path = geckodriver)
  9. #create an IE driver
  10. iedriver = "C:/tests/IEDriverServer.exe"
  11. driver = webdriver.Firefox(executable_path = iedriver)
  12. #open a website
  13. the_url = "https://dev.to"
  14. driver.get(the_url)
  15. #finding an element by id
  16. the_id = 'register'
  17. element = driver.find_element_by_id(the_id)
  18. #finding an element by name
  19. the_name = 'register'
  20. element = driver.find_element_by_id(the_name)
  21. #findding an element by class name
  22. the_class_name = 'nav-link'
  23. element = driver.find_element_by_class_name(the_class_name)
  24. #finding an element by tag name
  25. the_tag_name = 'a'
  26. element = driver.find_element_by_tag_name(the_tag_name)
  27. #finding an element by link text ( anchor elements)
  28. the_link_text = 'Sign Up'
  29. element = driver.find_element_by_link_text(the_link_text)
  30. #finding an element by partial link text (anchor elements)
  31. the_partial_link_text = 'Sign'
  32. element = driver.find_element_by_partial_link_text(the_partial_link_text)
  33. #finding element using css selectors
  34. the_css_selector = 'a[href="/sign-up"]'
  35. element = driver.find_element_by_css_selector(the_css_selector)
  36. #finding element using x-path
  37. the_xpath = '//a[@href = "/sign-up"]'
  38. element = driver.find_element_by_xpath(the_xpath)
  39. #clicking an element
  40. the_id = 'register'
  41. element = driver.find_element_by_id(the_id)
  42. element.click()
  43. #type inside an element
  44. the_id = 'email'
  45. the_email = '[email protected]'
  46. element = driver.find_element_by_id(the_id)
  47. element.send_keys(the_email)
  48. #select an option from select elements
  49. the_id = 'country'
  50. element = driver.find_element_by_id(the_id)
  51. select_element = Select(element)
  52. select_element.select_by_visible_text('India')
  53. #taking screenshots
  54. the_path = 'C:/tests/screenshots/1.png'
  55. driver.save_screenshot(the_path)
  56. #uploading a file
  57. the_file_path = 'C:/tests/files/test.pdf'
  58. the_id = 'upload_button'
  59. element = driver.find_element_by_id(the_id)
  60. element.send_keys(the_file_path)
  61. #execute javascript
  62. js_code = 'document.getElementById("pop-up").remove()'
  63. driver = execute_script(js_code)
  64. #switch to iframe
  65. the_iframe_id = 'payment_section'
  66. the_element_id = 'card_number'
  67. the_iframe = driver.find_element_by_id(the_iframe_id)
  68. driver.switch_to.frame(the_iframe)
  69. element = driver.find_element_by_id(the_element_id)
  70. element.send_keys('23232323')
  71. driver.switch_to.default_content()
  72. #switch to next tab
  73. global nextTab
  74. global currentTab
  75. nextTab = currentTab + 1
  76. driver.switch_to_window(driver.window_handles[nextTab])
  77. currentTab = currentTab + 1
  78. #swtich to previous tab
  79. global previousTab
  80. global currentTab
  81. previousTab = currentTab - 1
  82. driver.switch_to_window(driver.window_handles[previousTab])
  83. currentTab = currentTab - 1
  84. #close tab
  85. driver.close()
  86. #close alert
  87. driver.switch_to.alert.accept()
  88. #refresh
  89. driver.refresh()
  90. #hover
  91. the_id = "register"
  92. the_element = driver.find_element_by_id(the_id)
  93. hover = ActionChains(driver).move_to_element(the_element)
  94. hover.perform()
  95. #right click
  96. the_id = "register"
  97. the_element = driver.find_element_by_id(the_id)
  98. right_click = ActionChains(driver).context_click(the_element)
  99. right_click.perform()
  100. #set window size
  101. driver.set_window_size(1600, 1200)
  102. #press key
  103. the_id = 'register'
  104. element = driver.find_element_by_id(the_id)
  105. element.send_keys(Keys.RETURN)
  106. #configuring element load timeout
  107. from selenium.webdriver.support.ui import WebDriverWait
  108. the_id = 'register'
  109. WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, the_id)))
  110. #emulating mobile device
  111. 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'
  112. pixel_3_xl_emulation = {
  113. "deviceMetrics": {
  114. "width": 411,
  115. "height": 731,
  116. "pixelRatio": 3
  117. },
  118. "userAgent": google_pixel_3_xl_user_agent
  119. }
  120. options = webdriver.ChromeOptions()
  121. options.add_experimental_option("mobileEmulation", pixel_3_xl_emulation)
  122. driver = webdriver.Chrome(
  123. executable_path = chromedriver,
  124. 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!