Testrigor Com Blog...
Testrigor Com Blog...
Sheet
Artem Golubev
April 18, 2023
-Resources-
Driver Initialization
To start, you need to import the required libraries and initialize the WebDriver fo
specific browser you are targeting.
from selenium import webdriver
Locating Elements
By ID: driver.find_element_by_id("username")
By Name: driver.find_element_by_name("password")
By Class driver.find_element_by_class_name("submit-btn")
Name:
Selector: input[type='submit']")
By XPath: driver.find_element_by_xpath("//input[@type='submit']")
Pytest
1. Test functions: Define test functions by prefixing their names with “test_”. Py
will automatically discover and run these functions as test cases.
def test_example_function():
# Test code here
2. Fixtures are used to set up and tear down resources, like test data or object
can be created using the @pytest.fixture decorator and are passed as func
arguments to
the test functions that need them.
import pytest
@pytest.fixture
def example_fixture():
# Set up code here
yield
# Tear down code here
def test_example_function(example_fixture):
# Test code here
Unittest
1. Test classes: Create test classes by inheriting from unittest.TestCase .
import unittest
class ExampleTestCase(unittest.TestCase):
2. Test methods: Define test methods within the test classes by prefixing their
with
“test_”.
class ExampleTestCase(unittest.TestCase):
def test_example_method(self):
# Test code here
3. setUp and tearDown: These methods are used to set up and tear down reso
each
test method. Override them in your test class as needed.
class ExampleTestCase(unittest.TestCase):
def setUp(self):
# Set up code here
def tearDown(self):
# Tear down code here
def test_example_method(self):
# Test code here
4. setUpClass and tearDownClass: These class methods are used to set up and
down
resources for the entire test class. Override them in your test class and use t
@classmethod decorator.
class ExampleTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Set up code here
@classmethod
def tearDownClass(cls):
# Tear down code here
def test_example_method(self):
# Test code here
Upload a file:
file_input = driver.find_element_by_id("file-upload")
file_input.send_keys("path/to/your/file.txt")
import csv
with open("path/to/your/file.csv", "r") as csvfile:
csv_reader = csv.reader(csvfile)
for row in csv_reader:
print(row)
Read data from an Excel file:
Selenium Navigators
Navigate to a URL:
driver.get("https://www.example.com")
driver.refresh()
driver.forward()
driver.back()
driver.current_window_handle
driver.switch_to.window("window_handle")
driver.switch_to.window(driver.window_handles[-1])
driver.close()
driver.switch_to.frame("frame_id")
frame_element = driver.find_element_by_id("frame_id")
driver.switch_to.frame(frame_element)
driver.switch_to.default_content()
Selenium Operations
Lauch a Webpage:
driver.get("https://www.example.com")
Click a button:
button = driver.find_element_by_id("button_id")
button.click()
alert = driver.switch_to.alert
alert.accept()
print(driver.title)
Implicit wait:
Explicit wait:
Sleep:
import time
time.sleep(5) # Pause the script for 5 seconds
field = driver.find_element_by_id("field_id")
driver.execute_script("arguments[0].setAttribute('disabled', true)", field)
field = driver.find_element_by_id("field_id")
driver.execute_script("arguments[0].removeAttribute('disabled')", field)
Selenium Grid
Start a node:
Server:
http://localhost:4444/ui/index.html
Selenium test:
A testRigor specialist will walk you through our platform with a custom
demo.
Request a Demo
Related Articles
-Resources- Software Testing
Hari Mahesh
February 22, 2024
Authorize.net Testing
You need a payment gateway in several scenarios, especially when you are
running a business that requires secure, efficient, and ...
Anushree Chatterjee
February 22, 2024
Hari Mahesh
February 16, 2024
Automate tests with plain English using Generative AI. Reduce QA overhead,
increase coverage, efficiency and scalability.
Get Our Newsletter
We will send monthly testRigor updates on new features, upcoming events and links to
recordings.
Email*
Email*
Join
Privacy policy