android 如何模拟手工打开屏幕和关闭屏幕
有一个 app 应用,要测试的点是安装应用后关闭掉屏幕,然后再打开屏幕,再向左或向右滑动解锁屏幕
问题是:安装 - 访问 - 操作应用都可以,一串的操作
这个不知道如何关闭屏幕和打开屏幕
先谢谢!
如果你是用 UIAutomator 的话 直接就是
uiDevice.pressKeyCode(26); //KEYCODE_POWER=26;
appium 的话
driver.keyevent(26)
配置 server 的时候,代码增加启动 activity 和等待 activity
desired_caps['appActivity'] = appActivity
desired_caps[ 'appWaitActivity'] = appWaitActivity
#3 楼 @eurekasaber 有加的
现在报错:
AttributeError: 'WebDriver' object has no attribute 'keyevent'
这个 keyevent 事件是要 import 哪个类的?
#5 楼 @zsx10110
from appium import webdriver
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
#success = True
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = 'SM-N9100'
desired_caps['app'] = 'C:\Users\liujuan\Desktop\apk\youqianhua1.apk'
desired_caps['appPackage'] = 'com.youqian.activity'
desired_caps['appActivity'] = 'com.youqian.activity.guide.SplashActivity'
desired_caps[ 'appWaitActivity'] = 'com.youqian.activity.guide.SplashActivity'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(3)
driver.keyevent(26)
driver.quit()
我把好多包都导了,但是也不知道有啥用。还是报 AttributeError: 'WebDriver' object has no attribute 'keyevent'
#9 楼 @sincool 其实这个问题解决方法就是看提示信息了
Original error: com.youqian.activity/.guide.SplashActivity never started. Current: com.youqian.activity/.guide.GuideActivity
所以说你就直接将
desired_caps['appActivity'] = 'com.youqian.activity.guide.SplashActivity'
desired_caps[ 'appWaitActivity'] = 'com.youqian.activity.guide.SplashActivity'
改成
desired_caps['appActivity'] = 'com.youqian.activity.guide.GuideActivity'
desired_caps[ 'appWaitActivity'] = 'com.youqian.activity.guide.GuideActivity'
因为它告诉你说当前的 Activity 是 GuideActivity 虽然我还不太清楚 waitActivity 是什么作用。
#10 楼 @zsx10110 我发现,滋要是下面还有别的任何操作,都会报 selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: com.youqian.activity/.guide.SplashActivity never started. Current: com.youqian.activity/.guide.GuideActivity)
不管是不是 keyevent。。。。。我要下面直接不加任何东西,然后直接只是安装打开就没有任何问题
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = 'SM-N9100'
desired_caps['app'] = 'C:\\Users\\cvtpc\\Downloads\\youqianhua.apk'
desired_caps['appPackage'] = 'com.youqian.activity'
desired_caps['appActivity'] = 'com.youqian.activity.guide.GuideActivity'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(3)
driver.keyevent(26)
driver.quit()
这段代码在我那都是 ok 的 我就改了一个 app 跟一个 appAcitivity
#20 楼 @zsx10110
from appium import webdriver
from time import sleep
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = 'SM-N9100'
desired_caps['app'] = 'C:\Users\liujuan\Desktop\apk\youqianhua_v2011.apk'
desired_caps['appPackage'] = 'com.youqian.activity'
desired_caps['appActivity'] = 'com.youqian.activity.guide.GuideActivity'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(3)
driver.keyevent(26)
driver.quit()
appium 中设置的也是 GuideActivity。然后就报的是:selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: Permission to start activity denied.)
@chenhengjie123 帮忙看看为啥?
@sincool 我帮你呼叫下大神。
我觉得你是一个应用程序中包含了多个 apk,换句话说,你用错 apk 了,问问开发是否还有其他的 apk
逻辑上通过 SurfaceFlinger 判定界面显示状态,未锁屏则发键值 26 锁屏;锁屏壁纸界面则滑屏就是打开,挺好实现的逻辑。
设备端 shell 脚本我是这么写的,逻辑上是这样的,写成 java 脚本也可以。
#锁屏函数$1:0--lock;1--unlock
#屏幕物理分辨率
export wm_size_x=`wm size|busybox awk -F" |x" '{print $(NF-1)}'`
export wm_size_y=`wm size|busybox awk -F" |x" '{print $NF}'`
lock(){
while true;do
case `dumpsys SurfaceFlinger|grep "|....|"|busybox awk 'BEGIN{r="o"}{if($NF=="com.android.systemui.ImageWallpaper")r="l"}END{print NR-1 r}'` in
"1o")
local state=0
;;
"3l")
local state=1
;;
*)
local state=2
;;
esac
case $1 in
0)
if [ $state -ne 0 ];then
input keyevent 26
else
break
fi
sleep 1
;;
1)
if [ $state -eq 0 ];then
#我这是上滑屏解锁,其他的改下参数就好了
input keyevent 26&&sleep 1&&input swipe $((wm_size_x/2)) $((4*wm_size_y/5)) $((wm_size_x/2)) $((wm_size_y/5))
elif [ $state -eq 1 ];then
input swipe $((wm_size_x/2)) $((4*wm_size_y/5)) $((wm_size_x/2)) $((wm_size_y/5))
else
break
fi
sleep 1
;;
esac
done
}
我这个问题得改一下内容了,没看见有修改帖子的,我得重新开个新帖
多谢各位,这个问题已解决,与我的猜测一样。换了个 apk 的 activity 就可以了