1. About H5 page positioning?
Hybrid application
Generally speaking, Hybrid App refers to an app between web app and native app
One is native interface and code, the other is embedded web page, such as WeChat, Alipay has a browser kernel, and has browser kernel implementation.
The embedded template for displaying web content in Android application is called webview
To automate WebView, developers need to recompile the App and modify the following code:
Webview debugging mode on (Development)
Add the call of setWebContentsDebuggingEnabled to the WebView object. Each WebView should be added. Usually, there is only one WebView
protected void onCreate(Bund1e savedInstancestate){ super.onCreate(savedInstanceState); Webview mywebview=(Webview)findViewById(R.id.jcywebview); #Get the object first myWebView.setWebContentsDebuggingEnabled(true);} #After calling the setWebContentsDebuggingEnabled method of the object
There are two relationships between WebView and app:
- 1. The content of WebView does not depend on the app
Features: opening is just a url. Use the mobile phone mode and directly open the corresponding web page with the chrome browser
appium calls all interface environments context
The context name of the native part is usually NATIVE_APP
The context of WebView is WEBVIEW_XXXX (general application app package name)
How do we check the current context?
appium calls all interface environments context
The context name of the native part is usually NATIVE_APP
The context of WebView is WEBVIEW_XXXX (general application app package name)
How do we check the current context?
driver.contexts #View several context s, including local and WebView
To display the current context:
driver.current_context
example:
print(driver.contexts) # Return is the list ['NATIVE_APP','WEBVIEW_com.example.jcy.wvtest '] print(driver.current_context) # The current context is: NATIVE_APP driver.switch_to.context('WEBVIEW_com.example.jcy.wvtest') # Enter the embedded WebView driver.find_element_by_id('index-kw').send_keys('Hello') # operation driver.find_element_by_id('index-bn').click() driver.switch_to.context('NATIVE_APP') # Return to local app driver.find_element_by_id('com.example.jcy.wvtest:id/navigation_dashboard').click() time.sleep(2) driver.find_element_by_id('com.example.jcy.wvtest:id/navigation_notifications').click()
2. The content of WebView depends on the app (if you really encounter this problem, it's not recommended to continue, which is a waste of time.)
Features: it's not the website address at all. It's embedded in the application. The computer can't open at all
You must surf the Internet scientifically (if you have successfully accessed the Internet)
First open the application, and then open the chrome browser on the computer. Input: C hrome://inspect
Remote Target will appear on the computer, and the connected mobile phone model and webview will be displayed
Click inspect under the web page to see the contents of webview
2. About Toast box positioning?
Introduction to Toast
Toast in Android is a simple message prompt box. When the view is displayed to the user, it is displayed as floating in the application. Different from Dialog
What's more, it will never get focus and can't be clicked.
The idea of Toast class is not to attract people's attention as much as possible. At the same time, it also displays information to users, hoping they can see it. And Toast display time is limited, one
It disappeared in about 3 seconds. Therefore, using traditional element positioning tools, we cannot locate Toast elements.
AppiumToast content acquisition
The current version of Appium supports Toast content recognition, which is mainly based on UiAutomator2. Therefore, the following parameters need to be configured in capability:
desired_caps['automationName']='uiautomator2'
Test scenario
Enter the login interface and enter the wrong user name or password to obtain Toast content:
"Wrong user name or password, you can try 4 more times "
code implementation
get_toast.py
#coding=utf-8 from selenium.webdriver.support.ui import WebDriverWait driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear() driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('xuefei') driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('88888887') driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click() error_message="The user name or password is wrong. You can try it again 4 times" message='//*[@text=\'{}\']'.format(error_message) toast_element=WebDriverWait(driver,5).until(lambdax:x.find_element_by_xpath(message)) print(toast_element.text)
Note: when Toast is in Chinese, the top must be annotated with #coding=utf-8, otherwise character recognition will fail due to encoding and decoding.
3. Multi touch problem?
Multi touch problems are mostly used in map zooming, picture zooming and scene zooming. How to simulate such operations in appium?
Schematic diagram of sliding principle:
MultiAction
Multi action is a multi touch class, which can simulate multi-point operations of users. It mainly includes two methods: add() and perform(). Multi action can be combined with ActionTouch to simulate the operation effect of users' multiple fingers sliding;
from appium.webdriver.common.multi_action import MultiAction from appium.webdriver.common.touch_action import TouchAction
load:
The method add(self, *touch_actions) adds the TouchAction object to the multi action and executes it later.
Parameters:
touch_actions - one or more TouchAction objects that describe the chain of actions to be performed by a finger
Usage:
a1 = TouchAction(driver) a1.press(el1).move_to(el2).release() a2 = TouchAction(driver) a2.press(el2).move_to(el1).release() MultiAction(driver).add(a1, a2)#Combine the above actions
Execution:
perform(self) performs the operations stored in the object.
Continuous operation:
a1 = TouchAction(driver) a1.press(el1).move_to(el2).release() a2 = TouchAction(driver) a2.press(el2).move_to(el1).release() MultiAction(driver).add(a1, a2).perform()
It is somewhat similar to the use of multithreading and multiprocessing in Python
Test scenario
Install and start the app. After entering the map, zoom in and out respectively
code implementation
from appium import webdriver from time import sleep from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction x=driver.get_window_size()['width'] y=driver.get_window_size()['height'] def pinch(): action1=TouchAction(driver) action2=TouchAction(driver) zoom_action=MultiAction(driver) action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release() action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release() print('start pinch...') zoom_action.add(action1,action2) zoom_action.perform() def zoom(): action1=TouchAction(driver) action2=TouchAction(driver) zoom_action=MultiAction(driver) action1.press(x=x*0.4,y=y*0.4).wait(1000).move_to(x=x*0.2,y=y*0.2).wait(1000).release() action2.press(x=x*0.6,y=y*0.6).wait(1000).move_to(x=x*0.8,y=y*0.8).wait(1000).release() print('start zoom...') zoom_action.add(action1,action2) zoom_action.perform() if __name__ == '__main__': for i in range(3): pinch() for i in range(3): zoom()
It doesn't matter that I don't know and can't use the contents mentioned above. Just solve some rare problems. Even if encountered, other schemes can be used to replace or bypass.
appium automation focuses on: element positioning, element positioning, element positioning.