from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class CodeReviewPage:
def __init__(self, driver):
self.driver = driver
self.wait = WebDriverWait(driver, 10)
def open_review(self, review_url):
self.driver.get(review_url)
def is_coding_standards_checked(self):
checkbox = self.wait.until(EC.presence_of_element_located((By.ID, 'coding-standards-checkbox')))
return checkbox.is_selected()
def are_comments_clear(self):
comments = self.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'code-comment')))
return all(comment.text.strip() != '' for comment in comments)
def are_unit_tests_passing(self):
status = self.wait.until(EC.presence_of_element_located((By.ID, 'unit-test-status')))
return status.text.lower() == 'passing'
def no_defect_patterns(self):
defect_warnings = self.driver.find_elements(By.CLASS_NAME, 'defect-warning')
return len(defect_warnings) == 0
def is_approve_enabled(self):
approve_btn = self.wait.until(EC.presence_of_element_located((By.ID, 'approve-button')))
return approve_btn.is_enabled()
def test_defect_prevention_in_code_review():
driver = webdriver.Chrome()
review_page = CodeReviewPage(driver)
try:
review_url = 'https://example.com/code-review/123'
review_page.open_review(review_url)
assert review_page.is_coding_standards_checked(), 'Coding standards checklist is not completed.'
assert review_page.are_comments_clear(), 'Code comments are missing or unclear.'
assert review_page.are_unit_tests_passing(), 'Unit tests are not passing.'
assert review_page.no_defect_patterns(), 'Defect patterns found in code.'
assert review_page.is_approve_enabled(), 'Approve button is not enabled after checks.'
finally:
driver.quit()
if __name__ == '__main__':
test_defect_prevention_in_code_review()This script uses Selenium WebDriver with Python to automate the verification of defect prevention strategies in a code review process.
The CodeReviewPage class models the code review page using the Page Object Model pattern. It encapsulates all interactions and element locators, improving maintainability.
Explicit waits ensure the script waits for elements to load before interacting, avoiding flaky tests.
The test function test_defect_prevention_in_code_review opens the review URL, then asserts that:
- The coding standards checklist is completed.
- Code comments are present and clear.
- Unit tests are passing.
- No defect patterns are detected.
- The approve button is enabled only after all checks pass.
Assertions have clear messages to help identify failures.
The driver quits at the end to close the browser cleanly.