don't stop believing

Appium Unity 앱 Client 작성 2 (with OpenCV) 본문

Testing Automation/Appium

Appium Unity 앱 Client 작성 2 (with OpenCV)

Tongchun 2018. 8. 17. 09:45

Appium Unity 앱 Client 작성 1에 이어서 진행합니다.

http://dejavuqa.tistory.com/229


이제 게임 로그인 후 옵션 버튼을 클릭해 로그아웃 하는 것 까지 진행해 보겠습니다.


게임을 먼저 플레이 하면서 아래와 같이 스샷을 찍었고 찾으려는 이미지도 오려놨습니다.


Accet을 다운받은 후 나오는 로그인 화면입니다.

스크립트 작성 할때 Accet 다운받는 시간을 충분히 줘야 합니다.


'카카오 계정으로 로그인'버튼을 클릭하면 이메일과 비밀번호를 넣는 화면이 나옵니다.

이 화면은 Native 화면입니다. element로 찾을 수 있습니다.


계정 로그인을 하면 화면을 터치한 후 게임에 접속하게 됩니다.


하루에 한번씩 받는 접속 보상입니다.

스크립트를 작성하려고 보니 이건 넘어가 버리네요. 스크립트 작성시 우선 제외했습니다.


오른쪽 상단의 톱니바퀴 모양의 옵션 버튼을 클릭해 로그아웃을 할 겁니다.


'로그아웃' 버튼을 클릭해 로그아웃 합니다.


'확인'버튼을 눌러 줍니다.


위 스크린샷에서 찾으려는 이미지를 아래와 같이 오려놓고 searchimages라는 폴더에 넣어 놨습니다.



이제 아래와 같이 스크립트를 작성합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'''
Kakao Game SDK Test App
Device: V10 (LGF600Kb1134738)
'''
import unittest
import os
from appium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
# opencv import.
import cv2
import numpy as np
import time, datetime
class Matching():
def detectimage(self, screenshotPath, detectImagePath):
sourceimage = cv2.imread(screenshotPath, 0)
template = cv2.imread(detectImagePath, 0)
w, h = template.shape[::-1]
method = eval('cv2.TM_CCOEFF')
res = cv2.matchTemplate(sourceimage, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print('\nmax_val: %d' % max_val)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
center = (top_left[0] + int(w/2), top_left[1] + int(h/2))
color = (0, 0, 255)
cv2.rectangle(sourceimage, top_left, bottom_right, color, thickness=8)
detectshotPath = screenshotPath[:-4] + '-detect.png'
cv2.imwrite(detectshotPath, sourceimage)
return center
class GrandchaseLoginOutTest(unittest.TestCase):
def makeTS(self):
return str(int(datetime.datetime.now().timestamp()))
def strDatetime(self):
return str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
def setUp(self):
# Kakao Game SDK Test App
app = os.path.join(os.path.dirname(__file__), 'D:/Test_Appium/Grand', 'resigned_com.kakaogames.grdchase_1.8.4_qa.apk')
app = os.path.abspath(app)
# Set up appium
# Appium 4001 .
# desired_capabilities (V10) .
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4001/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'Android',
'platformVersion': '8.0.0',
'deviceName': 'LG V30',
'automationName': 'Appium',
'newCommandTimeout': 500,
'appPackage': 'com.kakaogames.grdchase',
'appActivity': 'com.kog.grandchase.Main',
'udid': 'LGMV300L664ed5e8',
'noReset': True
})
def test_search_field(self):
matching = Matching()
# .
test_folder_name = self.strDatetime()
currentPath = '%s/' % os.getcwd()
test_Directory = currentPath + test_folder_name + '/'
if not os.path.exists(test_Directory):
os.makedirs(test_Directory)
driver = self.driver
wait = WebDriverWait(driver, 20)
# Asset .
sleep(50)
# ' ' tap .
screenshotPath = test_Directory + '%s-screenshot.png' % self.makeTS()
detectImagePath = currentPath + 'searchimages/kakaologin.png'
driver.save_screenshot(screenshotPath)
center = matching.detectimage(screenshotPath, detectImagePath)
driver.tap([center])
# 5 .
sleep(5)
try:
# .
account = wait.until(EC.element_to_be_clickable((By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View[1]/android.view.View[1]/android.widget.EditText')))
account.click()
account.send_keys('dejavuwing@gmail.com')
# .
password = wait.until(EC.element_to_be_clickable((By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View[1]/android.view.View[3]/android.widget.EditText')))
password.click()
password.send_keys('1234ngle')
# ().
signin = wait.until(EC.element_to_be_clickable((By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View[2]')))
signin.click()
# 10 .
sleep(10)
except:
print('you have been login.')
# 5 .
sleep(5)
# ' ' ..
screenshotPath = test_Directory + '%s-screenshot.png' % self.makeTS()
detectImagePath = currentPath + 'searchimages/touchTostart.png'
driver.save_screenshot(screenshotPath)
center = matching.detectimage(screenshotPath, detectImagePath)
driver.tap([center])
# 5 .
sleep(5)
# '' tap .
screenshotPath = test_Directory + '%s-screenshot.png' % self.makeTS()
detectImagePath = currentPath + 'searchimages/option-button.png'
driver.save_screenshot(screenshotPath)
center = matching.detectimage(screenshotPath, detectImagePath)
driver.tap([center])
# 5 .
sleep(5)
# '' tap .
screenshotPath = test_Directory + '%s-screenshot.png' % self.makeTS()
detectImagePath = currentPath + 'searchimages/logout.png'
driver.save_screenshot(screenshotPath)
center = matching.detectimage(screenshotPath, detectImagePath)
driver.tap([center])
# 5 .
sleep(5)
# ' ' tap .
screenshotPath = test_Directory + '%s-screenshot.png' % self.makeTS()
detectImagePath = currentPath + 'searchimages/logout-conform.png'
driver.save_screenshot(screenshotPath)
center = matching.detectimage(screenshotPath, detectImagePath)
driver.tap([center])
# 30 .
sleep(30)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(GrandchaseLoginOutTest)
unittest.TextTestRunner(verbosity=2).run(suite)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


작성한 Appium Client를 실행하면 아래와 같이 스크린샵과 이미지를 찾았는지 확인하는 이미지를 볼 수 있습니다.

아래 화면은 스크립트를 어려번 수행한 후 마지막에 실행된 스크린샷들 입니다.




카카오 로그인 후 '터치하여 시작하기'를 찾아 클릭합니다.

'터치하여 시작하기' 이미지가 보였다 안보였다 하네요. 스샷도 희미하게 찍혀 이미지 인식이 잘 되지는 않았습니다.




게임 접속 후 오른쪽 상단 옵션 버튼을 찾아 로그아웃 합니다.








여기까지 Appium + OpenCV를 이용한 Unity 앱 자동화였습니다.

Comments