Toast Profile
- Toast in Android is a simple message box. When the view displayed to the user, as displayed in floating applications. And a pop-up box is not the same, it never gets focus, can not be clicked.
- Toast class ideology is as unobtrusive as possible, and also displays information to the user, want them to see. Toast displays and the limited time, usually about three seconds disappeared. Therefore, the use of traditional elements positioning tool, we are unable to locate Toast elements.
Source link: https: //www.jianshu.com/p/211a8f6ff064
Gets requirements:
1.Java client 5.0+
2. Use UIAutomator automation engine
3.Android system version 5.0+
method of obtaining
By.xpath ( "// * [contains (@ text, 'toast information part')]")
Example 58 city apk.
First get the package name and class name
package: name='com.wuba'
launchable-activity: name='com.wuba.activity.launch.LaunchActivity'
Follows
1. not logged manually click start
package com.test.firstAppium;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
Import io.appium.java_client.android.AndroidDriver;
ToastTest class {public
// declare a global variable
public static AndroidDriver <WebElement> androidDriver;
// declare a global variable
public static AndroidDriver <WebElement> androidDriver;
@BeforeTest
public void Setup () throws MalformedURLException {
// 1. Create a configuration object
DesiredCapabilities desiredCapabilities = new new DesiredCapabilities ();
// 2. Add configuration
// deviceName: you can find the device we tested
desiredCapabilities.setCapability ( "deviceName", " 127.0.0.1:62001 ");
// PlatformName: test platform for IOS or Android
desiredCapabilities.setCapability (" PlatformName "," Android ");
// appPackage: find the test APP: the aapt dump badging .....
desiredCapabilities.setCapability ( "appPackage", "com.wuba");
// automationName: uiautomator2 to address input box can not enter data
// automation engine
//desiredCapabilities.setCapability("automationName "," uiautomator2 ");
// do not clean up application data, the default is to clean up
desiredCapabilities.setCapability ( "NoReset", to true);
// appActivity: Entrance test app
desiredCapabilities.setCapability ( "appActivity", "com.wuba.activity.launch.LaunchActivity");
// create drive
// passing two parameters The first parameter: Appium address, the second parameter: configuration object
androidDriver new new androidDriver = <WebElement> (new new ( "http://127.0.0.1:4723/wd/hub") the URL, desiredCapabilities);
// implicit wait
.. androidDriver.manage () timeouts () implicitlyWait (30, TimeUnit.SECONDS);
public void Setup () throws MalformedURLException {
// 1. Create a configuration object
DesiredCapabilities desiredCapabilities = new new DesiredCapabilities ();
// 2. Add configuration
// deviceName: you can find the device we tested
desiredCapabilities.setCapability ( "deviceName", " 127.0.0.1:62001 ");
// PlatformName: test platform for IOS or Android
desiredCapabilities.setCapability (" PlatformName "," Android ");
// appPackage: find the test APP: the aapt dump badging .....
desiredCapabilities.setCapability ( "appPackage", "com.wuba");
// automationName: uiautomator2 to address input box can not enter data
// automation engine
//desiredCapabilities.setCapability("automationName "," uiautomator2 ");
// do not clean up application data, the default is to clean up
desiredCapabilities.setCapability ( "NoReset", to true);
// appActivity: Entrance test app
desiredCapabilities.setCapability ( "appActivity", "com.wuba.activity.launch.LaunchActivity");
// create drive
// passing two parameters The first parameter: Appium address, the second parameter: configuration object
androidDriver new new androidDriver = <WebElement> (new new ( "http://127.0.0.1:4723/wd/hub") the URL, desiredCapabilities);
// implicit wait
.. androidDriver.manage () timeouts () implicitlyWait (30, TimeUnit.SECONDS);
}
@Test
public void getToast() {
//androidDriver.findElementsByAndroidUIAutomator("new UiSelector().text(\"未登录\")").click();
androidDriver.findElementById("com.wuba:id/header_login_button").click();
//androidDriver.findElementsByAndroidUIAutomator("new UiSelector().text(\"账号密码登录\")").click();
androidDriver.findElementById("com.wuba:id/accountLogin").click();
androidDriver.findElementById("com.wuba:id/login_username").sendKeys("123333334");
androidDriver.findElementById("com.wuba:id/login_password").sendKeys("14444423");
androidDriver.findElementById("com.wuba:id/login_login_button").click();
information part')]")// toast Obtaining By.xpath ( "// * [contains (
WebElement toastElement =androidDriver.findElementByXPath("//*[contains(@text,'用户名与密码')]");
System.out.println(toastElement.getText());
}
@AfterTest
public void teardown() throws InterruptedException {
Thread.sleep(3000);
androidDriver.quit();
@Test
public void getToast() {
//androidDriver.findElementsByAndroidUIAutomator("new UiSelector().text(\"未登录\")").click();
androidDriver.findElementById("com.wuba:id/header_login_button").click();
//androidDriver.findElementsByAndroidUIAutomator("new UiSelector().text(\"账号密码登录\")").click();
androidDriver.findElementById("com.wuba:id/accountLogin").click();
androidDriver.findElementById("com.wuba:id/login_username").sendKeys("123333334");
androidDriver.findElementById("com.wuba:id/login_password").sendKeys("14444423");
androidDriver.findElementById("com.wuba:id/login_login_button").click();
information part')]")// toast Obtaining By.xpath ( "// * [contains (
WebElement toastElement =androidDriver.findElementByXPath("//*[contains(@text,'用户名与密码')]");
System.out.println(toastElement.getText());
}
@AfterTest
public void teardown() throws InterruptedException {
Thread.sleep(3000);
androidDriver.quit();
}
}