Original Address https://www.cnblogs.com/sundalian/p/5629500.html
1. Positioning element Application Element
1.1 Locating elements by ID
The ID in Android is usually Resrouce-id:
The code can write this:
webelement element = Driver.findelement (By.id ("com.tencent.mm:id/do"));
Or:
Driver.findelementbyid ("Com.tencent.mm:id/do")
The following example shows an ID positioned to an element and clicks:
@Testpublic void DoClick () { //find elements by id webelement el = driver.findelement (By.id ("com.tencent.mm:id/do")); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
1.2 Locating elements by name
The general Text property is considered to be name
@Testpublic void DoClick () { //Find elements by name webelement el = driver.findelement (By.name ("nickname")); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
1.3 Positioning elements via classname
ClassName refers to the class attribute
@Testpublic void DoClick () { //finds elements through ClassName webelement el = driver.findelement (By.classname (" Android.widget.TextView ")); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
1.4 Positioning with XPath
Appium is relatively inefficient for XPath location execution, which means that it is slower to execute when it encounters XPath's positioning code. In case of necessity, try not to use this positioning method.
Code:
@Testpublic void DoClick () { //finds elements through XPath webelement el = driver.findelement (By.xpath ("/// android.widget.button[@index = ' 1 '])); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
1.5 positioning elements via Accessibilityid
The elements are positioned in the same way as Name,id.
Positioning method:
Findelement (By.accessibilityid ("Sharebutton"))
The code is as follows:
@Testpublic void DoClick () { //Find elements by Accessibilityid webelement el = Driver.findelementbyaccessibilityid (" Sharebutton "); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
1.6 Positioning elements via Androiduiautomator
Androiduiautomator is a powerful element positioning method, it is through the Android Uiautomator class library to find elements, positioning method:
Findelement (By.androiduiautomator (String uiauto));
You can choose Id,nameclassname,description as the incoming string
Webelement El =driver.findelementbyandroiduiautomator ("New Uiselector (). ResourceId (\" com.tencent.mm:id/do\ ")");
The code is as follows:
@Testpublic void DoClick () { //Find elements by Androiduiautomator webelement el = Driver.findelementbyandroiduiautomator ("New Uiselector (). Description (\" sharebutton\ ")"); El.click (); Wait for 3s observation to show try { thread.sleep; } catch (Interruptedexception e) { e.orintstacktrace ();} }
2. Handling Pure Web App element targeting
2.1 Using Chrome-devtools
This is Google provides a mobile Web development debugging tool, through which we can debug the phone page. Through debugging we can see the source of WAP page, so that the positioning of elements. So how does this tool work? First you have to have a PC with a Chrome browser on it, and an Android phone with a Chrome mobile browser installed on it. Connect your phone to your PC via USB cable and turn on the USB debug mode (because each machine is different, so the way to turn on USB mode is no longer explained in detail), then enter the ADB devices in CMD to see if the device is linked successfully:
Next, open the Chrome browser on your PC
Then type in the Address bar: chrome://inspect/#devices
Then open the Chrome browser on your Android phone and enter the Baidu home address:
When you open the Baidu home page, we return to the PC version of the Chrome browser above the chrome://inspect/#devices页面下观察:
Next click on "Inspect", you will find a new window pops up,
Through this figure you can see the mobile phone screen on the page has been shown on the PC, you can see the Mobile WAP page source code and UI interface. It is particularly important to use Devtools: If you click the Inspect Open Devtools window blank, and the refresh is not valid, it is most likely because of the wall.
At the end of the Hosts file, append the following:
64.233.162.83 chrome-devtools-frontend.appspot.com
After saving, enter in cmd: Ipconfig/flushdns
After you refresh DNS, the Open Devtools window will not be blank.
At the top of the Devtools window is a "Select an element on the page to inspect it" button, as shown in:
Baidu Button Element Information
Locate by ID:
Webelement SearchButton = driver.findelement (By.id ("index-bn"));
Baidu Search Keyword code:
public androiddriver<webelement> driver; @BeforeClasspublic void Starttest () throws Malformedurlexception { Desiredcapabilities capabilities = new Desiredcapabilities () capabilities.setcapability ("PlatformName", "Android"); Capabilities.setcapability ("DeviceName", "Meizu MX3"), Capabilities.setcapability ("Platformversion", "5.0.1"); Capabilities.setcapability ("Browsername", "Chrome");d river = new Androiddriver<webelement> (The New URL ("HTTP/ 127.0.0.1:4723/wd/hub "), capabilities);d River.manage (). Timeouts (). implicitlywait (6, timeunit.seconds);} @AfterClasspublic void Afterclass () {driver.quit ();} @Testpublic void Addcontact () {//Open Baidu Home Driver.get ("https://www.baidu.com");//input box element webelement InputBox = Driver.findelement (By.id ("index-kw"));//input Java keyword inputbox.sendkeys ("java");//Baidu button Webelement SearchButton = Driver.findelement (By.id ("index-bn"));//Click Baidu button Earchbutton.click (); try {thread.sleep (n);} catch ( Interruptedexception e) {e.printstacktrace ();}}
2.1chrome Toggle Device Mode
Open the PC Chrome browser and click F12
Click Toggle Device Mode,f5 Refresh to achieve the effect of mode 1, and is simpler than mode 1.
You can switch the device model and screen size.
3. Positioning mixed Application elements
The hybrid application is a native App+webview, which can be easily understood as a native app shell, with all the HTML pages inside. In the process of positioning such an app, you need to locate the button or link on the native app, then click on the button or link, then go through the method provided by Appium, enter the WebView page, and navigate through the positioning tools and methods described earlier. If your Android version is less than 4.4, then you need to use Selendroid mode as the test engine and you need to set the capability when the test is initialized. If your Android version is greater than or equal to 4.4, then appium as the test engine and then chromedriver to handle the webview. If you are using the Appium test engine, the debug WebView needs to meet the Android version 4.4+ already on, and need to configure the appropriate code within your app, Call the static method setwebcontentsdebuggingenabled in the WebView class, as follows:
if (Build.VERSION.SDK_INT >=build.version_codes. KITKAT) {webview.setwebcontentsdebuggingenabled (true);}
Operation to enter WebView:
Use driver.getcontexthandles here (); To get the app's handles, the native app will have a handle,webview that will also have a handleset<string> contexts = Driver.getcontexthandles (); for (String cotext:contexts) { System.out.println (context);//Prints out native_ App (native app handle) and WEBVIEW_com.example.testapp (WebView handle)}//into WebView driver.context ((String) Contexts.toarray () [1]);//Operation WebView Content
Use driver.getcontexthandles here (); Get all handles of the app, native app will have a Native_app Handle,webview will also have a webview_xxx_xxx_xx handle , after determining the handle of the WebView, use: Driver.context (handle string) to enter the WebView page
APPIUM Android Targeting mode