如何让我的自动代码与Appium连接

时间:2018-03-22 21:45:13

标签: android intellij-idea appium appium-android

我正在使用Linux / Ubuntu并尝试创建一个自动测试用例,用于记录用户并注销。但是我刚刚设置了环境,我知道Appium没有GUI,必须通过终端运行。但我的问题是我怎么知道我的代码中的功能与Appium连接,并且正在运行我的代码?我创建了一个“等待”,因为我需要在打开应用程序时拿起一个按钮。我已将我正在使用的当前版本包含在计算机上,我的代码包含在自动化测试中,以及终端日志中。如果你能解决这个问题我会非常感激。

Current Versions in use:
Ubuntu LTS 16.04
Selenium Standalone 3.11.0
Java_Client 2.2.0
IntelliJIdea: IntelliJ IDEA 2017.3.5 (Community Edition)
              Build #IC-173.4674.33, built on March 5, 2018
              JRE: 1.8.0_152-release-1024-b15 amd64
              JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
              Linux 4.8.0-56-generic

Code:
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;  
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.lang.Object;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class TestSonic {

String begin = 
"com.myapk.myapk.debug:id/onboarding_inner_orbit";


AndroidDriver driver;

@Before
public void setup() throws MalformedURLException {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Android Emulator");
    File file = new File("/home/sonic/Documents/Automation", "Myapk.apk");
    capabilities.setCapability("app", "/home/sonic/Documents/Automation/Myapk.apk");
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    capabilities.setCapability("clearSystemFiles", "True");

}

@Test
public void LogIn() {
    Wait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.id(begin)));

    driver.findElement(By.id(begin)).click();
    //WebElement starting = driver.findElementByAndroidUIAutomator(String.valueOf(By.id(begin)));
    //starting.click();

}

@After
public void teardown() {
    driver.quit();
}

}


   Appium Terminal:



[Appium] Welcome to Appium v1.7.2 (REV 7ad6d98cedde01809e32d56ab8ced064f6f28175)
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> POST /wd/hub/session {"desiredCapabilities":{"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"Android Emulator","platformName":"Android"},"capabilities":{"desiredCapabilities":{"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"Android Emulator","platformName":"Android"},"firstMatch":[{"platformName":"android"}]}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"Android Emulator","platformName":"Android"},null,{"desiredCapabilities":{"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"Android Emulator","platformName":"Android"},"firstMatch":[{"platformName":"android"}]}]
[debug] [BaseDriver] Event 'newSessionRequested' logged at 1521754646829 (16:37:26 GMT-0500 (CDT))
[Appium] Merged W3C capabilities {"desiredCapabilities":{"app":"/home/sonic/Docu... into desiredCapabilities object {"app":"/home/sonic/Documents/Automation/Sonic....
[Appium] Creating new AndroidDriver (v1.37.0) session
[Appium] Capabilities:
[Appium]   app: /home/sonic/Documents/Automation/Sonic.apk
[Appium]   deviceName: Android Emulator
[Appium]   platformName: android
[BaseDriver] Session created with session id: fe9c1ce5-c2b1-41ec-bb50-7c47b095b30e
[debug] [AndroidDriver] Getting Java version
[AndroidDriver] Java version is: 1.8.0_161
[ADB] Checking whether adb is present
[ADB] Using adb from /home/sonic/Android/Sdk/platform-tools/adb
[AndroidDriver] Retrieving device list
[debug] [ADB] Trying to find a connected android device
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[AndroidDriver] Using device: emulator-5554
[ADB] Checking whether adb is present
[debug] [ADB] Setting device id to emulator-5554
[BaseDriver] Using local app '/home/sonic/Documents/Automation/Sonic.apk'
[debug] [AndroidDriver] Checking whether app is actually present
[AndroidDriver] Starting Android session
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","wait-for-device"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","echo","ping"]
[AndroidDriver] setDeviceLanguageCountry requires language or country.
[AndroidDriver] Got language: 'null' and country: 'null'
[debug] [Logcat] Starting logcat capture
[debug] [AndroidDriver] Pushing settings apk to device...
[debug] [ADB] Getting install status for io.appium.settings
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","list","packages","io.appium.settings"]
[debug] [ADB] App is installed
[debug] [ADB] Getting package info for io.appium.settings
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","dumpsys","package","io.appium.settings"]
[ADB] Checking whether aapt is present
[ADB] Using aapt from /home/sonic/Android/Sdk/build-tools/27.0.3/aapt
[debug] [ADB] The installed 'io.appium.settings' package does not require upgrade (5 >= 5)
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","ro.build.version.sdk"]
[debug] [ADB] Current device property 'ro.build.version.sdk': 27
[debug] [ADB] Device API level: 27
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","dumpsys","package","io.appium.settings"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","dump","io.appium.settings"]
[debug] [ADB] Got the following command chunks to execute: pm,grant,io.appium.settings,android.permission.WRITE_SETTINGS,;,pm,grant,io.appium.settings,android.permission.ACCESS_MOCK_LOCATION,;
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","grant","io.appium.settings","android.permission.WRITE_SETTINGS",";","pm","grant","io.appium.settings","android.permission.ACCESS_MOCK_LOCATION",";"]
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","grant","io.appium.settings","android.permission.WRITE_SETTINGS",";","pm","grant","io.appium.settings","android.permission.ACCESS_MOCK_LOCATION",";"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","ps"]
[debug] [ADB] Device API level: 27
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","appops","set","io.appium.settings","android:mock_location","allow"]
[debug] [AndroidDriver] Pushing unlock helper app to device...
[debug] [ADB] Getting install status for io.appium.unlock
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","list","packages","io.appium.unlock"]
[debug] [ADB] App is installed
[debug] [ADB] Getting package info for io.appium.unlock
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","dumpsys","package","io.appium.unlock"]
[ADB] Checking whether aapt is present
[debug] [ADB] The installed 'io.appium.unlock' package does not require upgrade (2 >= 2)
[ADB] Getting device platform version
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","ro.build.version.release"]
[debug] [ADB] Current device property 'ro.build.version.release': 8.1.0
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","wm","size"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","ro.product.model"]
[debug] [ADB] Current device property 'ro.product.model': Android SDK built for x86
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","ro.product.manufacturer"]
[debug] [ADB] Current device property 'ro.product.manufacturer': Google
[debug] [AndroidDriver] Parsing package and activity from app manifest
[ADB] Checking whether aapt is present
[ADB] Extracting package and launch activity from manifest
[debug] [ADB] badging package: com.sonic.sonicdrivein.debug
[debug] [ADB] badging act: com.sonic.sonicdrivein.ui.screen.splash.SplashActivity
[debug] [AndroidDriver] Parsed package and activity are: com.sonic.sonicdrivein.debug/com.sonic.sonicdrivein.ui.screen.splash.SplashActivity
[AndroidDriver] Remote apk path is /data/local/tmp/25a696eb8f803cd943564cda2e9d929a.apk
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","ls","/data/local/tmp/25a696eb8f803cd943564cda2e9d929a.apk"]
[debug] [AndroidDriver] Checking if app is installed
[debug] [ADB] Getting install status for com.sonic.sonicdrivein.debug
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","list","packages","com.sonic.sonicdrivein.debug"]
[debug] [ADB] App is installed
[AndroidDriver] Apk is already on remote and installed, resetting
[debug] [AndroidDriver] Running fast reset (stop and clear)
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","am","force-stop","com.sonic.sonicdrivein.debug"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","pm","clear","com.sonic.sonicdrivein.debug"]
[debug] [AndroidDriver] Extracting strings from apk /home/sonic/Documents/Automation/Sonic.apk null /tmp/com.sonic.sonicdrivein.debug
[debug] [ADB] Extracting strings for language: default
[debug] [ADB] Device API level: 27
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","persist.sys.locale"]
[debug] [ADB] Current device property 'persist.sys.locale': 
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","getprop","ro.product.locale"]
[debug] [ADB] Current device property 'ro.product.locale': en-US
[debug] [ADB] No strings.xml for language 'en', getting default strings.xml
[debug] [ADB] Reading strings from converted strings.json
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","push","/tmp/com.sonic.sonicdrivein.debug/strings.json","/data/local/tmp"]
[debug] [AndroidBootstrap] Watching for bootstrap disconnect
[debug] [ADB] Forwarding system: 4724 to device: 4724
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","forward","tcp:4724","tcp:4724"]
[debug] [UiAutomator] Starting UiAutomator
[debug] [UiAutomator] Moving to state 'starting'
[debug] [UiAutomator] Parsing uiautomator jar
[debug] [UiAutomator] Found jar name: 'AppiumBootstrap.jar'
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","push","/home/sonic/.nvm/versions/node/v9.7.1/lib/node_modules/appium/node_modules/appium-android-bootstrap/bootstrap/bin/AppiumBootstrap.jar","/data/local/tmp/"]
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","ps"]
[ADB] No uiautomator process found to kill, continuing...
[debug] [UiAutomator] Starting UIAutomator
[debug] [ADB] Creating ADB subprocess with args: ["-P",5037,"-s","emulator-5554","shell","uiautomator","runtest","AppiumBootstrap.jar","-c","io.appium.android.bootstrap.Bootstrap","-e","pkg","com.sonic.sonicdrivein.debug","-e","disableAndroidWatchers",false,"-e","acceptSslCerts",false]
[debug] [UiAutomator] Moving to state 'online'
[AndroidBootstrap] Android bootstrap socket is now connected
[debug] [ADB] Getting connected devices...
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] json loading complete.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Registered crash watchers.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Client connected
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","dumpsys","window"]
[AndroidDriver] Screen already unlocked, doing nothing
[debug] [ADB] Device API level: 27
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","am","start","-W","-n","com.sonic.sonicdrivein.debug/com.sonic.sonicdrivein.ui.screen.splash.SplashActivity","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[debug] [ADB] Waiting up to 20000ms for activity matching pkg: 'com.sonic.sonicdrivein.debug' and activity: 'com.sonic.sonicdrivein.ui.screen.splash.SplashActivity' to be focused
[debug] [ADB] Possible activities, to be checked: 'com.sonic.sonicdrivein.ui.screen.splash.SplashActivity'
[debug] [ADB] Getting focused package and activity
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/home/sonic/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","emulator-5554","shell","dumpsys","window","windows"]
[debug] [ADB] Found package: 'com.sonic.sonicdrivein.debug' and fully qualified activity name : 'com.sonic.sonicdrivein.ui.screen.splash.SplashActivity'
[Appium] New AndroidDriver session created successfully, session fe9c1ce5-c2b1-41ec-bb50-7c47b095b30e added to master session list
[debug] [BaseDriver] Event 'newSessionStarted' logged at 1521754654676 (16:37:34 GMT-0500 (CDT))
[debug] [MJSONWP] Responding to client with driver.createSession() result: {"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"Android Emulator","platformName":"android"},"app":"/home/sonic/Documents/Automation/Sonic.apk","deviceName":"emulator-5554","platformName":"android","deviceUDID":"emulator-5554","platformVersion":"8.1.0","deviceScreenSize":"1080x1920","deviceModel":"Android SDK built for x86","deviceManufacturer":"Google","appPackage":"com.myapk.myapk.debug","appWaitPackage":"com.myapk.myapk.debug","appActivity":"com.myapk.myapk.ui.screen.splash.SplashActivity","appWaitActivity":"com.myapk.myapk.ui.screen.splash.SplashActivity"}
[HTTP] <-- POST /wd/hub/session 200 7857 ms - 888 

1 个答案:

答案 0 :(得分:0)

添加Thread.sleep(60000);点击后。如果单击元素通过代码与手动与应用程序交互时的效果相同,则Appium已连接并正常工作。

如果点击不起作用,可能有很多可能的原因。尝试单击其他元素,看看是否具有所需的结果。