본문 바로가기
코딩/Mobile 자동화 (new)

구글 스프레드 시트 API 연동 (Python) + OS에 따라 Xpath 가져오기

by salzzak 2024. 5. 8.
728x90

https://velog.io/@junsugi/Google-Sheet-%EC%97%B0%EB%8F%99%ED%95%98%EA%B8%B0-feat.-Google-API

 

Google Sheet 연동하기 (feat. Google API)

구글 스프레드시트 연동하다 빡쳐서 쓰는 글

velog.io

위 블로그 참고하여 프로젝트 생성 + 구글 스프레드 시트 라이브러리 추가 + 서비스 계정 생성 + 키 생성 + 구글 스프레드 시트 공유까지 

 

+ 트러블 슈팅

공유 드라이브 내 스프레드 시트를 만들어서 공유 > 서비스 계정 추가하는 단계가 불가했는데,

개인 드라이브 계정에 구축함으로써 해결

 

worksheet()

@pytest.fixture(scope="module")
def worksheet():
    # 구글 스프레드시트 연동
    scope = ([
        'https://spreadsheets.google.com/feeds',
        'https://www.googleapis.com/auth/drive',
    ])

    # google 개발자 콘솔에서 받아온 키 json 파일
    credentials = Credentials.from_service_account_file('키 파일', scopes=scope)
    gc = gspread.authorize(credentials)

    # 해당 링크 스프레드 시트 불러오기
    spreadsheet_url = '스프레드 시트 링크'
    doc = gc.open_by_url(spreadsheet_url)

    # 'xpath' 시트 불러오기
    worksheet = doc.worksheet('시트명')

    return worksheet

 

get_xpath() 

driver.desired_capabilities.get('platformName') 함수를 이용해 OS에 따른 XPath 값을 가져온다. 

def get_xpath(driver, worksheet, location):
    # '조직도_멤버' 값을 가진 Cell 찾기
    cell_find = worksheet.find(location)
    print("**** Found something at R%sC%s" % (cell_find.row, cell_find.col))

    ios_col = 4
    and_col = 5
    web_col = 6

    # '조직도_멤버' 값을 가진 Cell 값 기준으로 And/iOS xpath 값 받아오기
    ios_xpath = worksheet.cell(cell_find.row, ios_col).value
    and_xpath = worksheet.cell(cell_find.row, and_col).value
    web_xpath = worksheet.cell(cell_find.row, web_col).value

    if driver.desired_capabilities.get('platformName') == 'Android':
        return and_xpath
    elif driver.desired_capabilities.get('platformName') == 'ios':
        return ios_xpath
    else:
        return web_xpath

 

 

test_case_03()

def test_case_03(tools):
    driver_m, driver_w, worksheet = tools
    # Mobile > 주문 접수 + 배차 완료

    print(driver_m.desired_capabilities.get('platformName'))
    print(get_xpath(driver_m, worksheet, "테스트1"))
    print()
    print(driver_w.desired_capabilities.get('platformName'))
    print(get_xpath(driver_w, worksheet, "테스트2"))

 

 

코드 실행 시, driver OS 에 따라 각각 데이터 잘 가져옴 확인할 수 있다.

 

댓글