Open In App

Cloud-based Automation using Selenium in Python and BrowserStack

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is a library that enables the automation of web browsers. It is most popularly used for testing web applications. At the heart of Selenium lies the Selenium WebDriver. WebDriver uses browser automation APIs to control the browser and run tests. This would mean the browser would work in the same way as when it is used by a real user. You can use these features to automate most of your boring stuff online, for example, applying to multiple jobs on LinkedIn and AngelList.

This article is about using Selenium (Python) to automate a form-filling process, i.e. filling a form multiple times. After that, we step things up a notch, by using BrowserStack to take this automation process to the cloud. BrowserStack is a cloud web and mobile testing platform that lets developers access browsers on-demand for their testing needs.

Cloud-based automation and testing have several benefits. It offers better collaboration, quicker and cost-effective testing, unlimited availability, and some major advantages like customization, parallelization, and support for agile workflows.

Cloud-based automation using Selenium in Python and BrowserStack is a powerful combination that allows you to automate web applications across different operating systems, browsers, and devices. Here are the steps to set up cloud-based automation using Selenium in Python and BrowserStack:

Using BrowserStack for cloud-based automation provides several benefits, including:

  1. Testing across different operating systems, browsers, and devices: BrowserStack allows you to test your web applications across a wide range of platforms, ensuring compatibility with different user configurations.
  2. Scalability: BrowserStack provides a scalable infrastructure that allows you to run tests in parallel, reducing the time it takes to complete your test suite.
  3. Ease of use: BrowserStack provides an intuitive web interface that makes it easy to set up and manage your tests.

Some potential disadvantages of using BrowserStack for cloud-based automation include:

  1. Cost: BrowserStack is a paid service, so you need to factor in the cost of the service when considering cloud-based automation.
  2. Limited customization: BrowserStack provides a limited set of customization options compared to running Selenium locally, which may limit the flexibility of your automation.
  3. Network latency: Running tests in the cloud can introduce network latency, which may affect the performance of your automation.

Regener

For this article, we will be using a sample Google form.

The first and most obvious step would be to install Selenium on your system by using the following command in your terminal:

sudo pip3 install selenium

The next step would be to create a BrowserStack account (If you have one already, then that’s great). BrowserStack’s Automate Mobile Plan for 1 parallel and 1 user for 1 year is free for students through the GitHub Student Developer Pack.

You would then need to head to this page on BrowserStack’s website. Here you can select the browser and OS combination of your preference. After you’ve selected your preference, you would then need to create a new .py file on your system in a code editor. Let’s say, you wish to perform my test on Chrome 84 Browser on Mac OS X Catalina. So you should write down the following code in your .py file:

python3




from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
from selenium.webdriver.common.by import By
 
 
desired_cap = {
 'browser': 'Chrome',
 'browser_version': '84.0 beta',
 'os': 'OS X',
 'os_version': 'Catalina',
 'resolution': '1024x768',
 'name': 'Bstack-[Python] Sample Test'
}
 
driver = webdriver.Remote(
    command_executor ='Your unique BrowserStack Access Key link',
    desired_capabilities = desired_cap)


 
The first four lines of the code specify the imported libraries. The time library will be used later on in the project. The desired cap dictionary specifies the specifications of the OS and Browser to be used. The next statement ( webdriver.Remote() ) connects the program to the BrowserStack’s system. The unique string to be passed to the command_executor parameter would be found on this page when you select your Browser and OS preference. The access key is unique to each BrowserStack user and so is the link. 

After this, you need to add the following python code to your .py file.

python3

The driver.get() statement will open the link provided in the browser. The screenshot of the form is attached below.

The sample form used for this article.

Now your next step would be to select the correct options for each of the questions/fields. There are several ways to do this, but let’s say you use the XPath to select your desired option. For this, you need to hover over to the option of your choice and press the mouse’ right-click over the radio button/checkbox. Then you would need to select the inspect option. When you right-click on the highlighted element inside the new pane on the right, you would be able to copy the XPath, as shown in the picture below.

Copying XPath

This works for multiple-choice, text, and checkbox fields. Now you can paste the XPath in the .py file in the form a string as shown below. You would also need the XPath of the Submit button and Submit another response (resubmit) button. The resubmit button makes automation easier.

python3




radioOption3 ='//*[@id ="mG61Hd"]/div[2]/div / div[2]/div[1]/div / div / div[2]/div[1]/div / span / div / div[3]/label / div / div[1]/div / div[3]'
checkBox2 = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div[1]/div[2]/label/div/div[1]/div[2]'
checkBox3 = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div[1]/div[3]/label/div/div[1]/div[2]'
text = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input'
submit = '//*[@id ="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span'
 
resubmit = '/html / body / div[1]/div[2]/div[1]/div / div[4]/a'


Now, you are left with the final step, implementing the loop for automation. Let’s say you want to fill the form 100 times. So you run a while loop 100 times, incrementing variable i each time. Inside the while loop you use find_element(By.XPATH) and click() attribute to implement your functionality, For the text field, the send_keys method does the job. The time.sleep(1) waits for 1 second after submitting the form to avoid any unwanted error. The following code uses try and except for the same purpose (as a precaution). If there is no error, the tool will go on to submit another response in the same window. If an error is generated, it will close that window, and start with a new one. The driver.quit() statement at the end stops/completes the process of automation. 

python3




i = 0
while i<100:
    driver.find_element(By.XPATH,radioOption3).click()
    driver.find_element(By.XPATH,checkBox2).click()
    driver.find_element(By.XPATH,checkBox3).click()
    text_box = driver.find_element(By.XPATH,text)
    text_box.send_keys("The text I need")
    driver.find_element(By.XPATH,submit).click()
    i = i + 1
    time.sleep(1)
    try:
        driver.find_element(By.XPATH,resubmit).click()
    except:
        driver.close()
        driver = webdriver.Remote(command_executor ='Your unique BrowserStack link', desired_capabilities = desired_cap)
        driver.get('https://forms.gle / xX5NEumdXW4Z7SrC6')
driver.quit()


Now all you need to do is go to your terminal and use the following command to run the program.

python3 filename.py

Now you can move to your BrowserStack Dashboard and see your program fill the form 100 times.

Selenium is a powerful tool which can be used for various purposes. The code is easy to implement and it makes testing and automation a lot easier. BrowserStack provides an easy-to-use powerful interface which makes cloud-based testing a lot easier.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads