How To Run Selenium PyTests In Parallel (2 Min) In Python

Published: 06 April 2022
on channel: Gokce DB
1,907
27

In this tutorial, you'll learn how to run selenium tests in parallel using Pytests & Python.


Facebook:   / gokcedbsql  

Video Transcript:

Hi guys, this is Abhi from Gokce DB. In this video, you are going to learn how to run SELENIUM TESTS in PARALLEL using PYTEST in PYTHON.

Let’s start by clicking on the PYTHON PACKAGES button and ensure that PYTEST, PYTEST dash XDIST, and PYTEST dash FORKED packages are installed.

Next, let’s look at the TESTS directory structure. I have an AMAZON underscore TESTS directory which contains three files. CONFTEST dot PY file contains the fixture function or driver to initialize the web driver.

I also have two TESTS files. The first test searches for the keyword SUNGLASSES then click on the AMAZON CHOICE product and asserts whether the keyword UV FOUR HUNDRED PROTECTION appears on the product page or not.

In the second test file, instead of searching for SUNGLASSES, I’m searching for SHOES. And the rest of the steps are the same.

I also have a PAGE underscore OBJECT directory which contains LOCATORS and PAGES. LOCATORS dot PY file contains the IDs and XPATHs that are being used on the HOME page and the SEARCH page.

Whereas, the PAGES dot PY file contains the helper method for the AMAZON HOME page and the AMAZON SEARCH page classes. To execute the two tasks sequentially, right-click on the AMAZON underscore TESTS folder then hit RUN PYTEST.

Here, I’m expecting the first test to PASS and the second test to FAIL. Now, if you wanted to run this test in parallel click on the PYTEST dropdown then click on edit configuration.

Type HYPHEN N space TWO in additional arguments, where two is the number of workers. If I re-run the test again, you’ll notice two browser windows popups because both the tests are running in parallel.

Finally, to export the test results click on the EXPORT TESTS RESULTS button. There you have it.

Make sure you like, subscribe, and turn on the notification bell. Until next time.

from tests.page_object.pages.pages import AmazonHomePage, AmazonSearchPage


def test_keyword_in_search_result(driver):
amazon_home_page = AmazonHomePage(driver)
amazon_search_page = AmazonSearchPage(driver)

driver.get(amazon_home_page.get_url())
amazon_home_page.get_search_text_box_by_id().send_keys("sunglasses")
amazon_home_page.get_search_submit_button_by_id().click()
amazon_search_page.get_amazons_choice_product_by_xpath().click()

assert "UV400 PROTECTION" in driver.page_source

from tests.page_object.pages.pages import AmazonHomePage, AmazonSearchPage


def test_keyword_in_search_result(driver):
amazon_home_page = AmazonHomePage(driver)
amazon_search_page = AmazonSearchPage(driver)

driver.get(amazon_home_page.get_url())
amazon_home_page.get_search_text_box_by_id().send_keys("shoes")
amazon_home_page.get_search_submit_button_by_id().click()
amazon_search_page.get_amazons_choice_product_by_xpath().click()

assert "UV400 PROTECTION" in driver.page_source

from tests.page_object.locators.locators import AmazonHomePageLocator, AmazonSearchPageLocator
from selenium.webdriver.common.by import By


class AmazonHomePage:
def __init__(self, driver):
self.driver = driver

@staticmethod
def get_url():
return AmazonHomePageLocator.url

def get_search_text_box_by_id(self):
return self.driver.find_element(By.ID, AmazonHomePageLocator.search_text_box_id)

def get_search_submit_button_by_id(self):
return self.driver.find_element(By.ID, AmazonHomePageLocator.search_submit_button_id)


class AmazonSearchPage:
def __init__(self, driver):
self.driver = driver

def get_amazons_choice_product_by_xpath(self):
return self.driver.find_element(By.XPATH,
AmazonSearchPageLocator.amazons_choice_label_xpath)

class AmazonHomePageLocator:
url = "https://www.amazon.com/"
search_text_box_id = "twotabsearchtextbox"
search_submit_button_id = "nav-search-submit-button"


class AmazonSearchPageLocator:
amazons_choice_label_xpath = "//*[contains(@id,'amazons-choice-label')]"

import pytest
from selenium import webdriver
import os


@pytest.fixture()
def driver():
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
driver = webdriver.Chrome(executable_path=root_dir + '/resources/chromedriver')
Time in seconds driver should wait when searching for an element if it is not
immediately present
driver.implicitly_wait(2)
yield driver
driver.quit()


Watch video How To Run Selenium PyTests In Parallel (2 Min) In Python online, duration hours minute second in high quality that is uploaded to the channel Gokce DB 06 April 2022. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 1,907 times and liked it 27 visitors.