liuling师姐程序学习笔记(1)

本文介绍了一个名为Socket.AnaysisTask的Java类,该类包含内部类Configure用于加载和保存配置文件。Configure类使用Properties类进行配置信息的读写操作,并利用Android的Environment类获取外部存储目录路径。此外,AnaysisTask类通过实例化ClientAccept类并在新线程中运行,实现了多线程任务。

Socket.AnaysisTask.java:
(1)包含内部类Configure。Configure.java在main包中。
Configure类中包含对输入输出流的操作。见基础资料FileInputStream与FileOutputStream类
Configure中就两个方法:loadConfig()和saveConfig(),分别重载:
public Properties loadConfig(Context context, String file);
public Properties loadConfig(String file);
public void saveConfig(Context context, String file, Properties properties);
public void saveConfig(String file, Properties properties);
两个方法的核心是Properties类(java.util.Properties):Java中Properties类的操作
(2)调用Configure的方法如下:

public String ConfigureFile =Environment.getExternalStorageDirectory().getPath()+"/"+"Configure.properties";
p=configure.loadConfig(ConfigureFile);  

android.os.Environment:android.os.Environment 提供访问环境变量(方法:getExternalStorageDirectory ();返回 : File;解释 : 返回扩展存储区目录(SDCard))。
File类:java File类 简单用法举例
(3)AnaysisTask中的方法:AnaysisTaskIni()
在方法中实例化ClientAccept类,ClientAccept类继承并实现了Runnable接口,从而在一条新的线程中运行。多线程实现方式—实现Runnable接口(还介绍了Timer类和TimerTask类,其中TimerTask类也继承了Runnable接口)

Started by user liuling [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /Users/ios/.jenkins/workspace/android-appium-test [Pipeline] { [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Tool Install) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Scan Android Devices) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] echo 开始扫描连接的 Android 设备... [Pipeline] sh + adb devices [Pipeline] echo adb devices 输出为: List of devices attached 192.168.1.43:5555 device emulator-5554 device [Pipeline] echo 检测到 2 台设备: 192.168.1.43:5555, emulator-5554 [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Start Appium Servers) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] sh + lsof -i :4723 + grep LISTEN node 34850 ios 16u IPv4 0x3597b850efd609a0 0t0 TCP *:4723 (LISTEN) [Pipeline] echo 端口 4723 已被占用,尝试下一个... [Pipeline] sh + lsof -i :4725 + grep LISTEN [Pipeline] echo 找到空闲端口: 4725 [Pipeline] echo 为设备 192.168.1.43:5555 分配 Appium 端口 4725 [Pipeline] sh + echo 51496 + nohup appium -a 0.0.0.0 -p 4725 --allow-cors --session-override + sleep 3 + echo '📄 日志内容(启动阶段):' 📄 日志内容(启动阶段): + cat appium_192.168.1.43:5555.log [Appium] Welcome to Appium v3.1.0 [Appium] Non-default server args: [Appium] { allowCors: true, port: 4725, sessionOverride: true } [Appium] The autodetected Appium home path: /Users/ios/.appium [Appium] Attempting to load driver xcuitest... [Appium] Attempting to load driver uiautomator2... [Appium] Requiring driver at /Users/ios/.appium/node_modules/appium-xcuitest-driver/build/index.js [Appium] XCUITestDriver has been successfully loaded in 0.520s [Appium] Requiring driver at /Users/ios/.appium/node_modules/appium-uiautomator2-driver/build/index.js [Appium] AndroidUiautomator2Driver has been successfully loaded in 0.997s [Appium] You have enabled CORS requests from any host. Be careful not to visit sites which could maliciously try to start Appium sessions on your machine [Appium] Appium REST http interface listener started on http://0.0.0.0:4725 [Appium] You can provide the following URLs in your client code to connect to this server: http://127.0.0.1:4725/ (only accessible from the same host) http://192.168.137.29:4725/ http://192.168.1.142:4725/ [Appium] Available drivers: [Appium] - xcuitest@10.2.1 (automationName 'XCUITest') [Appium] - uiautomator2@6.6.2 (automationName 'UiAutomator2') [Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use. + sleep 10 + echo '📄 日志内容(崩溃阶段,关键!):' 📄 日志内容(崩溃阶段,关键!): + cat appium_192.168.1.43:5555.log [Appium] Welcome to Appium v3.1.0 [Appium] Non-default server args: [Appium] { allowCors: true, port: 4725, sessionOverride: true } [Appium] The autodetected Appium home path: /Users/ios/.appium [Appium] Attempting to load driver xcuitest... [Appium] Attempting to load driver uiautomator2... [Appium] Requiring driver at /Users/ios/.appium/node_modules/appium-xcuitest-driver/build/index.js [Appium] XCUITestDriver has been successfully loaded in 0.520s [Appium] Requiring driver at /Users/ios/.appium/node_modules/appium-uiautomator2-driver/build/index.js [Appium] AndroidUiautomator2Driver has been successfully loaded in 0.997s [Appium] You have enabled CORS requests from any host. Be careful not to visit sites which could maliciously try to start Appium sessions on your machine [Appium] Appium REST http interface listener started on http://0.0.0.0:4725 [Appium] You can provide the following URLs in your client code to connect to this server: http://127.0.0.1:4725/ (only accessible from the same host) http://192.168.137.29:4725/ http://192.168.1.142:4725/ [Appium] Available drivers: [Appium] - xcuitest@10.2.1 (automationName 'XCUITest') [Appium] - uiautomator2@6.6.2 (automationName 'UiAutomator2') [Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use. [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (1/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (2/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (3/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (4/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (5/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (6/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (7/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (8/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (9/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (10/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (11/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (12/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (13/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (14/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (15/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (16/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (17/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (18/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (19/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] sh + curl -s http://0.0.0.0:4725/wd/hub/status + grep '"ready":true' + true [Pipeline] echo aiting for Appium on port 4725... (20/20) [Pipeline] sleep Sleeping for 3 sec [Pipeline] error [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: ppium on port 4725 did not become ready Finished: FAILURE两次抓取其appium端口存在,并且在curl期间也存在,curl验证失败之后,命令行验证,这个端口就不存在了
最新发布
12-27
你提供的完整 Jenkins 日志非常清晰,我们来 **逐层分析问题本质**。 --- ## 🔍 现象总结 | 阶段 | 观察 | |------|------| | ✅ 启动命令执行 | `nohup appium -p 4725 ... &` 成功运行 | | ✅ 日志显示启动成功 | 输出了:`Appium REST http interface listener started on http://0.0.0.0:4725` | | ✅ `curl` 请求期间端口还存在 | 多次尝试连接 `/wd/hub/status` | | ❌ 所有 `curl` 响应都未包含 `"ready":true` | 检查失败 | | ⚠️ 最终验证超时 | 报错:“Appium on port 4725 did not become ready” | | 🚫 最后用 `ps` 查不到进程 | 进程已退出 | --- ## ✅ 关键结论: > 💥 **Appium 启动了 → 监听了端口 → 但没有返回正确的 `/wd/hub/status` 响应 → 被判定为“未就绪” → 实际上可能已经崩溃或无法处理请求** 但更严重的是: > 🟥 **虽然日志里没报错,但它在后续某个时刻静默退出了!** --- ## 🔎 深度分析:为什么 `curl` 返回空?而日志却显示“启动成功”? ### 1. Appium v3.x 的 `/wd/hub/status` 行为变化(重点!) 从 Appium v2.x 升级到 v3.x 后,**`/wd/hub/status` 默认不再返回 `"ready": true`**,尤其是在没有任何会话激活时。 #### 正常响应示例(v3.1.0): ```json { "value": { "message": "Appium server is up and running", "build": { "version": "3.1.0" } }, "status": 0 } ``` 👉 注意:**它没有 `"ready": true` 字段!** 所以你的检查逻辑: ```bash curl -s http://0.0.0.0:4725/wd/hub/status | grep '"ready":true' ``` 永远匹配不到内容 → 判定失败! --- ## ✅ 根本原因:❌ 错误地依赖 `"ready":true` 字段进行健康检查 这是导致你整个流程误判的核心 bug。 即使 Appium 完全正常运行,也不会输出这个字段。 --- ## ✅ 解决方案:修改健康检查逻辑 你应该改为检查 HTTP 状态码是否可达、且返回 JSON 中包含 `"message"` 或 `"version"` ### ✅ 推荐的健康检查脚本: ```groovy def waitForAppiumReady(int port, int retries = 20, int delay = 3) { sh """ for i in {1..${retries}}; do # 使用 -m 设置超时,避免无限等待 response=\$(curl -s -m 5 http://localhost:${port}/wd/hub/status) if [ -n "\$response" ]; then echo "Received response: \$response" # 检查是否包含 build/version 或 known message if echo "\$response" | grep -q 'version\\|"message\\|"build'; then echo "✅ Appium on port ${port} is ready." exit 0 fi else echo "⏳ No response from Appium on port ${port}, waiting... (\$i/${retries})" fi sleep ${delay} done echo "❌ Timeout waiting for Appium on port ${port}" cat appium_${port}.log exit 1 """ } ``` 或者更简单的判断方式(只要能返回非空即可): ```bash curl -f http://localhost:4725/wd/hub/status > /dev/null 2>&1 ``` - `-f`:失败时不返回成功状态码 - 如果能拿到 HTTP 200 就算通过 ✅ 改成这样: ```groovy sh """ for i in {1..20}; do if curl -f -s http://localhost:${port}/wd/hub/status > /dev/null; then echo "✅ Appium is ready on port ${port}" exit 0 fi echo "⏳ Waiting for Appium... (\$i/20)" sleep 3 done echo "❌ Failed to wait for Appium" exit 1 """ ``` --- ## 🤔 那为什么最后 `ps` 找不到进程? 这里有两种可能性: ### ✅ 可能性 1:Jenkins 构建失败后杀死了子进程 Jenkins 默认会在构建失败时终止所有相关进程(尤其是由当前 Job 启动的后台任务)。 你看到“最后不存在”,是因为: > ❌ 构建失败 → Jenkins 主动 kill 掉了 `nohup` 启动的 node 进程 这不是 Appium 自己挂了,是被外部杀了。 ### ✅ 可能性 2:Node.js 内部错误导致崩溃(次要) 虽然日志中没有打印异常,但仍有可能在收到第一个 `/wd/hub/status` 请求时触发内部异常退出。 但这种情况极少见,除非模块损坏。 --- ## ✅ 如何验证 Appium 是否真的“活着”? 加一个调试步骤,在 `waitForAppiumReady` 之后、失败前,查看进程和端口状态: ```groovy sh ''' echo "mPid: \$(cat appium.pid)" ps -ef | grep \$(cat appium.pid) | grep -v grep lsof -i :4725 ''' ``` 如果发现: - PID 存在 - `node` 进程还在 - `LISTEN` 状态仍在 → 说明 Appium 没死,只是健康检查写错了! --- ## ✅ 修复建议:完整正确流程 ```groovy sh """ nohup appium \\ -a 0.0.0.0 \\ -p ${port} \\ --allow-cors \\ --session-override \\ > ${logFile} 2>&1 < /dev/null & echo \$! > appium.pid sleep 5 # 给足时间初始化 # 检查是否存活 + 可响应 for i in {1..20}; do if curl -f -s http://localhost:${port}/wd/hub/status > /dev/null; then echo "✅ Appium is ready on port ${port}" exit 0 fi # 检查进程是否还活着 if ! kill -0 \$(cat appium.pid) > /dev/null 2>&1; then echo "❌ Appium process died!" cat ${logFile} exit 1 fi echo "⏳ Waiting for Appium to be ready... (\$i/20)" sleep 3 done echo "❌ Timeout waiting for Appium" cat ${logFile} exit 1 """ ``` --- ## ✅ 总结 | 问题 | 原因 | 解决方案 | |------|------|----------| | `grep '"ready":true'` 失败 | Appium v3 不再返回该字段 | ✅ 改为检查响应体是否存在或使用 `-f` | | `ps` 找不到进程 | Jenkins 构建失败后杀死子进程 | ✅ 先确认是在失败前还是失败后 | | 认为 Appium 未启动 | 实际已启动但检查逻辑错误 | ✅ 修改健康检查策略 | --- 你现在最需要做的是: > 🔧 **把 `waitForAppiumReady` 中的 `"ready":true` 匹配删除,改用 `curl -f` 或关键字匹配** 然后重新运行,你会发现:**Appium 其实早就 ready 了!** ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值