Sample Scripts


This section outlines the sample scripts for the Playwright and Selenium frameworks.

Sample Playwright Script: Validating and Measuring Page Transaction

#Packages that need to be imported

from playwright.sync_api import sync_playwright
import BMCHelixPlaywright
from BMCHelixPlaywright import BrowserWrapper
from STMTimer import STMTimer

# Setup Chrome options and custom WebDriver
customTimer = STMTimer()

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, executable_path=<path_to_chrome_executable>)
    bmcBrowser = BrowserWrapper(browser)

    context = bmcBrowser.new_context()
    page = context.new_page()
 
   #PAGE LEVEL
   # Step 1: Navigate to Wikipedia homepage
    page.verify_title('Wikipedia webpage')  # Expected title
    page.verify_data('The Free Encyclopedia PAGE')  # Expected data
    page.goto("https://www.wikipedia.org/")

   # Step 2: Validate homepage content
    heading = page.locator("*[@id='www-wikipedia-org']/main/div[1]/h1/strong").inner_text()
   if heading == "The Free Encyclopedia":
        bmcBrowser.set_accuracy_error(
           "Homepage: Expected heading 'The Free Encyclopedia' found")  # The set_accuracy_error and set_availability_error is for Driver level
        bmcBrowser.set_availability_error("Homepage: Heading element missing or page failed to load")

   # TRANSACTION LEVEL

   # Transaction 1: Navigate to English Wikipedia
    customTimer.start("ENGLISH")
    page.locator('*[@id="js-link-box-en"]/strong').click()

   # Step 3: Validate English Wikipedia page content
    editors_text = page.locator("*[@id='articlecount']/ul/li[1]").inner_text()
   if editors_text != "111,223 active editors":
        customTimer.set_accuracy_error("ENGLISH",
           "English Wikipedia: '111,223 active editors' text not found")  # The set_accuracy_error and set_availability_error is for Transaction level
        customTimer.set_availability_error("ENGLISH",
           "English Wikipedia: Editor count element missing or page failed to load")

   # Step 4: Click on 'View source'
    page.locator("*[@id='ca-viewsource']/a").click()

   # Step 5: Validate 'Why is the page protected?' section
    protected_text = page.locator("*[@id='Why_is_the_page_protected?']").inner_text()
   if protected_text == "Why is the page protected?":  # If this condition fails then annotation is raised
        customTimer.set_accuracy_error("ENGLISH", "English Wikipedia: 'Why is the page protected?' section found")
        customTimer.set_availability_error("ENGLISH", "English Wikipedia: Section element missing or page failed to load")

   # Transaction 2: Navigate to Administrator abilities
    customTimer.start("ADMINISTRATION")
    page.locator('#mw-protectedpagetext > div > table > tbody > tr > td.mbox-text > div > div > a:nth-child(3)').click()

   # Step 6: Validate Administrator abilities section
    admin_text = page.locator("*[@id=\"Administrators'_abilities\"]").inner_text()
   if admin_text != "Not Administrators' abilities":  # If this condition fails then annotation is raised
        customTimer.set_accuracy_error("ADMINISTRATION",
           "Administration section: Expected 'Not Administrators' abilities' text not found")
        customTimer.set_availability_error("ADMINISTRATION",
           "Administration section: Element missing or page failed to load")

   # Stop timers
    customTimer.stop("ADMINISTRATION")
    customTimer.stop("ENGLISH")

   # Close browser
    bmcBrowser.close()

Sample Selenium Script: Validating and Measuring Page Transactions

#Packages that need to be imported
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options modules or packages required to be imported
from STMWebDriver import STMWebDriver
from STMTimer import STMTimer
import STMChromeDriver

from STMWebDriver import STMWebDriver
from STMTimer import STMTimer
import STMChromeDriver

# Setup Chrome options and custom WebDriver
options = Options()
driver = STMWebDriver.Chrome(options=options)
customTimer = STMTimer()

 PAGE LEVEL

# Step 1: Navigate to Wikipedia homepage
driver.verify_title('Wikipedia webpage')  # Expected title
driver.verify_data('The Free Encyclopedia PAGE')  # Expected data
driver.get('https://www.wikipedia.org/')

# Step 2: Validate homepage content( )
if driver.find_element(By.XPATH, '*[@id="www-wikipedia-org"]/main/div[1]/h1/strong').text == "The Free Encyclopedia":
    driver.set_accuracy_error("Homepage: Expected heading 'The Free Encyclopedia' found") # The set_accuracy_error and set_availability_error is for Driver level
    driver.set_availability_error("Homepage: Heading element missing or page failed to load")

 TRANSACTION LEVEL

# Transaction 1: Navigate to English Wikipedia
customTimer.start('ENGLISH')  # Start timer for English page
driver.find_element(By.XPATH, '/html/body/main/nav[1]/div[1]/a/strong').click()

# Step 3: Validate English Wikipedia page content
if driver.find_element(By.XPATH, '*[@id="articlecount"]/ul/li[1]').text != "111,223 active editors":
    customTimer.set_accuracy_error("ENGLISH", "English Wikipedia: '111,223 active editors' text not found ") # The set_accuracy_error and set_availability_error is for Transaction level
    customTimer.set_availability_error("ENGLISH", "English Wikipedia: Editor count element missing or page failed to load")

# Step 4: Click on 'View source'
driver.find_element(By.XPATH, '*[@id="ca-viewsource"]/a').click()

# Step 5: Validate 'Why is the page protected?' section
if driver.find_element(By.XPATH, '*[@id="Why_is_the_page_protected?"]').text == "Why is the page protected?": # If this condition fails then annotation is raised
    customTimer.set_accuracy_error("ENGLISH", "English Wikipedia: 'Why is the page protected?' section found")
    customTimer.set_availability_error("ENGLISH", "English Wikipedia: Section element missing or page failed to load")

# Transaction 2: Navigate to Administrator abilities
customTimer.start('ADMINISTRATION')  # Start timer for ADMINISTRATION section
driver.find_element(By.XPATH, '*[@id="mw-protectedpagetext"]/tbody/tr/td/table/tbody/tr/td[2]/div/div/a[3]').click()

# Step 6: Validate Administrator abilities section
if driver.find_element(By.XPATH, '*[@id="Administrators\'_abilities"]').text != "Not Administrators' abilities": # If this condition fails then annotation is raised
    customTimer.set_accuracy_error("ADMINISTRATION", "Administration section: Expected 'Not Administrators' abilities' text not found")
    customTimer.set_availability_error("ADMINISTRATION", "Administration section: Element missing or page failed to load")

# Stop timers
customTimer.stop('ADMINISTRATION')
customTimer.stop('ENGLISH')

# Close browser
driver.quit() 

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

BMC PATROL for Synthetic Monitoring 25.4