require 'rubygems'
require 'appium_lib'
require 'date'
require 'logger'
require 'pathname'
require 'thread'
require"fileutils"
capabilities = {
'appium-version'=> '1.4.13.1',
'platformName'=> 'Android',
'platformVersion'=> '4.4.2',
'appActivity:'=>'com.worktile.ui.external.WelcomeActivity',
'app'=>
'D:\WorkspacePython\apk\wt-android-3.12.3.apk',
'deviceName'=>'VPhone'
}
server_url = "http://127.0.0.1:4723/wd/hub"
dr = Appium::Driver.new(caps: capabilities).start_driver
Appium.promote_appium_methods Object
#创建日志路径,打印日志
def loginfo(s = "#{$!.message} #{$@[0]} ")
wait do
url = file()
#判断需写入的日志内容是否为空,如果为空则不需要写入
return if not s
#创建logger实例
logger = Logger.new(url+(Time.now).strftime("%Y-%m-%d")+"_install_detail.log", 'daily') #daily/weekly/monthly.
#赋予logger控制输出级别为DEBUG。DEBUG含义是:既可以在
#控制台看到需写入的日志信息,又写入到了日志文件中
logger.level = Logger::INFO
logger.info(''){s}
logger.close
end
end
#清除文件内上一次创建的内容,该方法调用必须写在所有方法之前
def Logdelete()
wait do
url = file()
io = File.open(url+(Time.now).strftime("%Y-%m-%d")+"_install_detail.log","w")
end
end
#创建文件夹/result/image/
def url()
begin
wait do
FileUtils.makedirs(Dir.pwd+"/result/image/")
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"创建文件夹异常!#{ex.message}"
end
end
#获取当前路径
def file()
begin
wait do
Dir.pwd
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"当前路径异常!#{ex.message}"
end
end
#点击ID
def ClickByID(id)
begin
wait do
find_element(:id,id).click
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{id}异常!#{ex.messge}"
end
end
#截图
def TakeTakesScreenshot()
begin
url()
wait do
dr = screenshot(Dir.pwd+"/result/image/"+ Time.now.strftime("%Y%m%d %H%M%S")+'.jpg')
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{}异常!#{ex.message}"
end
end
#通过用户名点击
def ClickByName(name)
begin
wait do
find_element(:name,name).click
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{name}异常!#{ex.message}"
end
end
#通过name输入name键入文字
def SendByName(name,content)
begin
wait do
find_element(:name,name).send_keys content
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{name}异常!#{ex.message}"
end
#通过id输入id键入文字
def SendByID(id,content)
begin
wait do
find_element(:id,id).send_keys content
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{id}异常!#{ex.message}"
end
#新版本更新窗口弹出,点击稍后更新按钮
def WaitForNew(content,name)
begin
# wait for alert to show
wait do
text content
find(name).click
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{name}异常!#{ex.message}"
end
end
def LongPressClick(name)
begin
wait do
e = find_element(:name, name)
Appium::TouchAction.new.long_press(element: e, x: 0.5, y: 0.5).release(element: e).perform
end
rescue =>ex
raise Exception,"#{name}异常!#{ex.message}"
end
end
#执行下滑动作
def SwipeDown(num)
begin
for i in 0..num
wait do
swipe :start_x => 0.5, :start_y => 0.25, :end_x => 0.5, :end_y => 0.75, :duration => 500
end
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{num}异常!#{ex.message}"
end
#执行上滑动作
def SwipeUp(num)
begin
for i in 0..num
wait do
swipe :start_x => 0.5, :start_y => 0.75, :end_x => 0.5, :end_y => 0.25, :duration => 500
end
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{num}异常!#{ex.message}"
end
#执行左滑动作
def SwipeLeft(num)
begin
for i in 1..num
wait do
swipe :start_x => 0.75, :start_y => 0.5, :end_x => 0.25, :end_y => 0.5, :duration => 500
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{num}异常!#{ex.message}"
end
end
#执行右滑动作
def SwipeRight(num)
begin
for i in 0..num
wait do
swipe :start_x => 0.25, :start_y => 0.5, :end_x => 0.75, :end_y => 0.5, :duration => 500
end
end
end
rescue =>ex
TakeTakesScreenshot()
raise Exception,"#{num}异常!#{ex.message}"
end
#结束当前运行
def Quit()
wait do
driver_quit
end
end
Logdelete()
ClickByID("btn_login")
loginfo("点击登录按钮成功")
SendByID("et_username","yk1234567")
loginfo("输入用户名成功")
SendByID("et_password","yk123456")
loginfo("输入密码成功")
ClickByID("btn_login")
loginfo("点击登录按钮成功")
WaitForNew('新版本更新','稍后更新')
loginfo("点击新版本更新按钮成功")
loginfo("点击稍后更新按钮成功")
SwipeLeft(1)
loginfo("滑屏两次成功")
LongPressClick('appium')
loginfo("长按成功")
TakeTakesScreenshot()
loginfo("截图成功")
Quit()