POM 구조로 테스트 케이스 작성

Keypoint·2022년 6월 7일
0
post-custom-banner

일단 생성한 파일 구조를 보면 아래와 같다.

Locators/Pages/Tests가 분리됨

(base) jiyoungan@Jiyoungui-MacBookPro sample_project % tree

└── POMProjectDemo
    ├── Locators
    │   ├── __pycache__
    │   │   └── locators.cpython-39.pyc
    │   └── locators.py
    ├── Pages
    │   ├── __pycache__
    │   │   ├── homePage.cpython-39.pyc
    │   │   └── loginPage.cpython-39.pyc
    │   ├── homePage.py
    │   └── loginPage.py
    └── Tests
        ├── __pycache__
        │   └── login.cpython-39.pyc
        ├── chromedriver
        └── login.py

Locators.locators.py

class Locators():

        # login page objects
        username_textbox_id = "txtUsername"
        password_textbox_id = "txtPassword"
        login_button_id = "btnLogin"

        # home page objects
        welcome_link_id = "welcome"
        logout_link_linkText = "Logout"

Pages.homePage.py

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

        self.welcome_link_id = "welcome"
        self.logout_link_linkText = "Logout"

    def click_welcome(self):
        self.driver.find_element_by_id(self.welcome_link_id).click()

    def click_logout(self):
        self.driver.find_element_by_link_text(self.logout_link_linkText).click()

Tests.login.py

from selenium import webdriver

from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
import unittest

# cmd 에서 실행할 수 있게 세팅
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "...", "..."))

# 페이지 단위의 함수를 불러옴
from sample_project.POMProjectDemo.Pages.loginPage import LoginPage
from sample_project.POMProjectDemo.Pages.homePage import HomePage

# 결과를 html로 저장
import HtmlTestRunner


class LoginTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        chrome_options = webdriver.ChromeOptions()
        service = Service(executable_path=ChromeDriverManager().install())
        cls.driver = webdriver.Chrome(service=service, options=chrome_options)
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

	# 테스트 케이스 1
    def test_01_login_valid(self):
        driver = self.driver
        driver.get("https://opensource-demo.orangehrmlive.com/")

        login = LoginPage(driver)
        login.enter_username("Admin")
        login.enter_password("admin123")
        login.click_login()

        homepage = HomePage(driver)
        homepage.click_welcome()
        homepage.click_logout()

        time.sleep(2)
        
	# 테스트 케이스 2
    def test_02_login_invalid(self):
        driver = self.driver
        driver.get("https://opensource-demo.orangehrmlive.com/")

        login = LoginPage(driver)
        login.enter_username("Admin1")
        login.enter_password("admin123")
        login.click_login()
        message = driver.find_element_by_xpath("").text
        self.assertEqual(message, "Invalid credentials")
        time.sleep(2)

	# 테스트 케이스가 완료되고 마지막으로 실행하는 함수
    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")


if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output="/Users/jiyoungan/Documents/Automation/reports"))
profile
QA Engineer
post-custom-banner

1개의 댓글

comment-user-thumbnail
2022년 10월 7일

appium도 동일하게 작성하면 동작하나요?

답글 달기