AWS Doc SDK ExamplesWord
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SDK for Python(Boto3)을 사용한 Device Farm 예제
다음 코드 예제에서는 Device Farm과 AWS SDK for Python (Boto3) 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.
주제
시나리오
다음 코드 예제에서는 Device Farm을 사용하여 브라우저 테스트를 실행하고 스크린샷을 찍는 방법을 보여줍니다.
- Python용 SDK(Boto3)
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. PyTest 및 Selenium을 사용하여 지정된 웹 사이트를 찾고, 스크린샷을 찍고, 실제 웹 사이트 콘텐츠를 예상 콘텐츠와 비교합니다.
import datetime import os import subprocess import boto3 import pytest from selenium import webdriver from selenium.webdriver import DesiredCapabilities from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait def get_git_hash(): """ Get the short Git hash of the current commit of the repository """ try: return ( subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]) .decode("utf-8") .strip() ) except: return "norepo" class TestHelloSuite: """ Our test suite. This style of test suite allows us to use setup_method and teardown_method. """ def save_screenshot(self, name): self.driver.save_screenshot(os.path.join(self.screenshot_path, name)) def setup_method(self, method): """ Set up a test. This makes sure that the session for an individual test is ready. The AWS credentials are read from the default ~/.aws/credentials or from the command line by setting the AWS_ACCESS_KEY_ID and AWS_SECRET_KEY environment variables. The project Amazon Resource Name (ARN) is determined by the PROJECT_ARN environment variable. """ devicefarm_client = boto3.client("devicefarm") project_arn = os.environ.get("PROJECT_ARN", None) if project_arn is None: raise ValueError("Must set PROJECT_ARN") # Request a driver hub URL for the Selenium client testgrid_url_response = devicefarm_client.create_test_grid_url( projectArn=project_arn, expiresInSeconds=300 ) # We want a directory to save our files into. We're going to make a directory # in the current directory that holds our results. self.screenshot_path = os.path.join( ".", "results", get_git_hash() + "-" + (datetime.date.today().isoformat()) ) if not os.path.exists(self.screenshot_path): os.makedirs(self.screenshot_path, exist_ok=True) # We want a Firefox instance on Windows desired_cap = DesiredCapabilities.FIREFOX desired_cap["platform"] = "windows" desired_cap["BrowserVersion"] = "latest" # Configure the webdriver with the appropriate remote endpoint. self.driver = webdriver.Remote(testgrid_url_response["url"], desired_cap) # # Auto-Tagging # # In order to get the Session ARN, we need to look up the session by the # Project ARN and session ID (from the driver). testgrid_session_arn_response = devicefarm_client.get_test_grid_session( projectArn=project_arn, sessionId=self.driver.session_id ) # Save the session's ARN so we can tag the session. self.session_arn = testgrid_session_arn_response["testGridSession"]["arn"] # In order to tag it, we're going to use the resourcegroupstaggingapi client to # add a tag to the session ARN that we just got. tag_client = boto3.client("resourcegroupstaggingapi") tag_client.tag_resources( ResourceARNList=[self.session_arn], Tags={"TestSuite": f"testsuite {method.__name__}", "GitId": get_git_hash()}, ) def teardown_method(self, method): """ Clean up resources used by each method. """ # End the Selenium session so we're off the clock. self.driver.quit() @pytest.mark.parametrize( "query,leading", [ pytest.param( "Seattle", "Seattle (/siˈætəl/ (listen) see-AT-əl) is a seaport city on the West Coast of the United States.", ), pytest.param( "Selenium", "Selenium is a chemical element with the symbol Se and atomic number 34.", ), pytest.param( "Amazon Locker", "Amazon Locker is a self-service package delivery service offered by online retailer Amazon.", ), pytest.param( "Kootenai Falls", "Kootenai Falls is a waterfall on the Kootenay River located in Lincoln County, Montana, just off U.S. Route 2.", ), pytest.param( "Dorayaki", "Dorayaki (どら焼き, どらやき, 銅鑼焼き, ドラ焼き) is a type of Japanese confection.", ), pytest.param("Robot Face", "<|°_°|> (also known as Robot Face or Robot)"), ], ) def test_first_paragraph_text(self, query, leading): """ This test looks at the first paragraph of a page on Wikipedia, comparing it to a known leading sentence. If the leading sentence matches, the test passes. A screenshot is taken before the final assertion is made, letting us debug if something isn't right. """ # Open the main page of Wikipedia self.driver.get("https://en.wikipedia.org/wiki/Main_Page") # Find the search box, enter a query, and press enter search_input = self.driver.find_element(By.ID, "searchInput") search_input.click() search_input.send_keys(query) search_input.send_keys(Keys.ENTER) # Wait for the search box to go stale -- This means we've navigated fully. WebDriverWait(self.driver, 5).until( expected_conditions.staleness_of(search_input) ) # Get the leading paragraph of the article. lead = leading.lower() # Find the element... lead_para = self.driver.find_element( By.XPATH, "//div[@class='mw-parser-output']//p[not(@class)]" ) # ... and copy out its text. our_text = lead_para.text.lower() our_text = our_text[: len(lead)] # Take a screenshot and compare the strings. self.save_screenshot(f"leadingpara_{query}.png") assert our_text.startswith(lead) @pytest.mark.parametrize( "query,expected", [ pytest.param("Automation Testing", "Test Automation"), pytest.param("DevOps", "DevOps"), pytest.param("Jackdaws Love My Big Sphinx Of Quartz", "Pangram"), pytest.param("EarthBound", "EarthBound"), pytest.param("Covered Bridges Today", "Covered Bridges Today"), pytest.param("Kurt Godel", "Kurt Gödel"), pytest.param("N//ng language", "Nǁng language"), pytest.param( "Who the Frick Is Jackson Pollock?", "Who the $&% Is Jackson Pollock?" ), ], ) def test_redirect_titles(self, query, expected): """ A test comparing pages we expect to (or not to) redirect on Wikipedia. This test checks to see that the page ("query") redirects (or doesn't) to the "expected" page title. Several of these are common synonyms ("Jackdaws...") while others are because of characters untypable by most keyboards ("Nǁng language") A screenshot is taken just before the final assertion is made to aid in debugging and verification. """ # Open the main page of Wikipedia self.driver.get("https://en.wikipedia.org/wiki/Main_Page") # Find the search box, enter some text into it, and send an enter key. search_input = self.driver.find_element(By.ID, "searchInput") search_input.click() search_input.send_keys(query) search_input.send_keys(Keys.ENTER) # wait until the page has rolled over -- once the search input handle is stale, # the browser has navigated. WebDriverWait(self.driver, 5).until( expected_conditions.staleness_of(search_input) ) # Get the first heading & take a screenshot our_text = self.driver.find_element(By.ID, "firstHeading").text.lower() self.save_screenshot(f"redirect_{query}.png") # did it match? assert our_text == expected.lower()
-
API 세부 정보는 AWS SDK for Python(Boto3) API 참조의 다음 주제를 참조하세요.
-
다음 코드 예제에서는 Device Farm으로 모바일 장치 패키지를 업로드하고 테스트하는 방법을 보여줍니다.
- Python용 SDK(Boto3)
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. 컴파일된 Android 애플리케이션 및 테스트 패키지를 Device Farm에 업로드하고, 테스트를 시작하고, 테스트가 완료될 때까지 기다린 다음 결과를 보고합니다.
import boto3 import os import requests import string import random import datetime import time # Update this dict with your own values before you run the example: config = { # This is our app under test. "appFilePath": "app-debug.apk", "projectArn": "arn:aws:devicefarm:us-west-2:111222333444:project:581f5703-e040-4ac9-b7ae-0ba007bfb8e6", # Since we care about the most popular devices, we'll use a curated pool. "testSpecArn": "arn:aws:devicefarm:us-west-2::upload:20fcf771-eae3-4137-aa76-92e17fb3131b", "poolArn": "arn:aws:devicefarm:us-west-2::devicepool:4a869d91-6f17-491f-9a95-0a601aee2406", "namePrefix": "MyAppTest", # This is our test package. This tutorial won't go into how to make these. "testPackage": "tests.zip", } client = boto3.client("devicefarm") unique = ( config["namePrefix"] + "-" + (datetime.date.today().isoformat()) + ("".join(random.sample(string.ascii_letters, 8))) ) print( f"The unique identifier for this run is '{unique}'. All uploads will be prefixed " f"with this." ) def upload_df_file(filename, type_, mime="application/octet-stream"): upload_response = client.create_upload( projectArn=config["projectArn"], name=unique + "_" + os.path.basename(filename), type=type_, contentType=mime, ) upload_arn = upload_response["upload"]["arn"] # Extract the URL of the upload and use Requests to upload it. upload_url = upload_response["upload"]["url"] with open(filename, "rb") as file_stream: print( f"Uploading {filename} to Device Farm as " f"{upload_response['upload']['name']}... ", end="", ) put_req = requests.put( upload_url, data=file_stream, headers={"content-type": mime} ) print(" done") if not put_req.ok: raise Exception(f"Couldn't upload. Requests says: {put_req.reason}") started = datetime.datetime.now() while True: print( f"Upload of {filename} in state {upload_response['upload']['status']} " f"after " + str(datetime.datetime.now() - started) ) if upload_response["upload"]["status"] == "FAILED": raise Exception( f"The upload failed processing. Device Farm says the reason is: \n" f"{+upload_response['upload']['message']}" ) if upload_response["upload"]["status"] == "SUCCEEDED": break time.sleep(5) upload_response = client.get_upload(arn=upload_arn) print("") return upload_arn our_upload_arn = upload_df_file(config["appFilePath"], "ANDROID_APP") our_test_package_arn = upload_df_file( config["testPackage"], "APPIUM_PYTHON_TEST_PACKAGE" ) print(our_upload_arn, our_test_package_arn) response = client.schedule_run( projectArn=config["projectArn"], appArn=our_upload_arn, devicePoolArn=config["poolArn"], name=unique, test={ "type": "APPIUM_PYTHON", "testSpecArn": config["testSpecArn"], "testPackageArn": our_test_package_arn, }, ) run_arn = response["run"]["arn"] start_time = datetime.datetime.now() print(f"Run {unique} is scheduled as arn {run_arn} ") state = "UNKNOWN" try: while True: response = client.get_run(arn=run_arn) state = response["run"]["status"] if state == "COMPLETED" or state == "ERRORED": break else: print( f" Run {unique} in state {state}, total " f"time {datetime.datetime.now() - start_time}" ) time.sleep(10) except: client.stop_run(arn=run_arn) exit(1) print(f"Tests finished in state {state} after {datetime.datetime.now() - start_time}") # Pull all the logs. jobs_response = client.list_jobs(arn=run_arn) # Save the output somewhere, using the unique value. save_path = os.path.join(os.getcwd(), "results", unique) os.mkdir(save_path) # Save the last run information. for job in jobs_response["jobs"]: job_name = job["name"] os.makedirs(os.path.join(save_path, job_name), exist_ok=True) # Get each suite within the job. suites = client.list_suites(arn=job["arn"])["suites"] for suite in suites: for test in client.list_tests(arn=suite["arn"])["tests"]: # Get the artifacts. for artifact_type in ["FILE", "SCREENSHOT", "LOG"]: artifacts = client.list_artifacts(type=artifact_type, arn=test["arn"])[ "artifacts" ] for artifact in artifacts: # Replace `:` because it has a special meaning in Windows & macOS. path_to = os.path.join( save_path, job_name, suite["name"], test["name"].replace(":", "_"), ) os.makedirs(path_to, exist_ok=True) filename = ( artifact["type"] + "_" + artifact["name"] + "." + artifact["extension"] ) artifact_save_path = os.path.join(path_to, filename) print(f"Downloading {artifact_save_path}") with open(artifact_save_path, "wb") as fn: with requests.get( artifact["url"], allow_redirects=True ) as request: fn.write(request.content) print("Finished")
-
API 세부 정보는 AWS SDK for Python(Boto3) API 참조의 다음 주제를 참조하세요.
-