코딩/Mobile 자동화 (new)

appium 자동화 2024 - 5) 소스 코드 구조화

salzzak 2024. 2. 22. 19:54
728x90

이전에 짰던 하나의 소스코드를 구조화

 

import pytest

from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver import WebElement
from selenium.webdriver.support.wait import WebDriverWait


# 1. 기다렸다가 클릭하는 함수 wait_Element / wait_Elements
def wait_Element(driver, xpath):
    location = xpath

    try:
        element:WebElement = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((By.XPATH, xpath))
        )
        return element

    except TimeoutException:
        print("Timeout!!!!!!!!!!!!")
        return False

def wait_Elements(driver, xpath):
    location = xpath
    wait_Element(driver, location)
    return driver.find_elements(By.XPATH, location)


# 2. appium 세팅
@pytest.fixture(scope="module")
def driver():
    capabilities = dict(
        platformName='Android',
        automationName='uiautomator2'
    )

    appium_server_url = 'http://localhost:4723'

    driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))

    driver.press_keycode(3)

    yield driver
    driver.quit()



# 3. 테스트케이스
def test_case_01(driver):
    el = wait_Element(driver, '//android.widget.TextView[@content-desc="시계"]')
    el.click()
    assert 1;

def test_case_02(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="세계시각"]')
    el.click()
    assert 1;

def test_case_03(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="스톱워치"]')
    el.click()
    assert 1;

def test_case_04(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="타이머"]')
    el.click()
    assert 1;

 

#1 은 자주 쓰이는 함수로 Basic/common.py 로 분류

#2 는 자동화 세팅 영역으로 Basic/appium_setting.py 로 분류

#3 는 TC 영역으로 TestCase/test_case_01.py 로 분류

 

ㄴ Python Package 로 Basic + TestCase 생성

 

ㄴ 각 폴더 아래에 Basic/common.py , Basic/appium_setting.py , TestCase/test_case_01.py 생성

 

각각 파일에 #1, #2, #3 을 넣어준다.

 

Basic/common.py

from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver import WebElement
from selenium.webdriver.support.wait import WebDriverWait

# 기다렸다가 클릭하는 함수 wait_Element / wait_Elements
def wait_Element(driver, xpath):
    location = xpath

    try:
        element:WebElement = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((By.XPATH, xpath))
        )
        return element

    except TimeoutException:
        print("Timeout!!!!!!!!!!!!")
        return False

def wait_Elements(driver, xpath):
    location = xpath
    wait_Element(driver, location)
    return driver.find_elements(By.XPATH, location)

 

 

Basic/appium_setting.py

import pytest

from appium import webdriver
from appium.options.android import UiAutomator2Options

# appium 세팅
@pytest.fixture(scope="module")
def driver():
    capabilities = dict(
        platformName='Android',
        automationName='uiautomator2'
    )

    appium_server_url = 'http://localhost:4723'

    driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))

    driver.press_keycode(3)

    yield driver
    driver.quit()

 

 

TestCase/test_case_01.py

# 테스트케이스
def test_case_01(driver):
    el = wait_Element(driver, '//android.widget.TextView[@content-desc="시계"]')
    el.click()
    assert 1;

def test_case_02(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="세계시각"]')
    el.click()
    assert 1;

def test_case_03(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="스톱워치"]')
    el.click()
    assert 1;

def test_case_04(driver):
    el = wait_Element(driver, '//android.widget.TextView[@resource-id="com.sec.android.app.clockpackage:id/title" and @text="타이머"]')
    el.click()
    assert 1;

 

 

TestCase/test_case_01.py 에서 Basic 패키지를 참조하기 위해,

 

Basic/__init__.py

from Basic.appium_setting import *
from Basic.common import *

 

파일에 위 내용을 입력해준다.

 

from Basic import *

 

이후 TestCase/test_case_01.py 상단에 입력해주자.

 

cd TestCase
pytest test_case_01.py

TestCase 로 이동 후 pytest 명령어 실행 시,

단일소스코드와 동일하게 동작됨 확인할 수있다.