美文网首页
Appium iOS Python3(二)~脚本的使用

Appium iOS Python3(二)~脚本的使用

作者: blaze冰叔 | 来源:发表于2019-08-23 15:34 被阅读0次

    书接上回,我们说到了使用appium录制脚本


    image.png

    录制时,代码区会根据我们的操作实时生成代码。
    右侧是可选语言
    1 生成可执行脚本
    2 复制脚本
    3 删除喽

    接下来,我们讲讲使用脚本
    首先运行appium,确保server已启动
    本地运行python,我用的是VSCode
    将从appium中复制的脚本贴到VScode中,我们运行一哈看看效果

    遇到的问题
    1 No module named appium
    没有安装Python-Client
    安装一哈
    pip install Appium-Python-Client
    2 name 'TouchAction' is not defined
    from appium.webdriver.common.touch_action import TouchAction

    然后就可以起飞了
    代码如下

    # This sample code uses the Appium python client
    # pip install Appium-Python-Client
    # Then you can paste this into a file and simply run with Python
    
    from appium import webdriver
    from appium.webdriver.common.touch_action import TouchAction
    
    caps = {}
    caps["platformName"] = "iOS"
    caps["platformVersion"] = "12.4"
    caps["deviceName"] = "iPhone"
    caps["automationName"] = "XCUITest"
    caps["app"] = "com.*****"
    caps["udid"] = "ed428*********bd63e4"
    
    driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    
    TouchAction(driver).tap(x=265, y=118).perform()
    TouchAction(driver).tap(x=37, y=46).perform()
    TouchAction(driver).tap(x=118, y=641).perform()
    TouchAction(driver).tap(x=249, y=171).perform()
    driver.back()
    TouchAction(driver).tap(x=171, y=265).perform()
    
    
    # driver.quit()
    

    代码中
    TouchAction(driver).tap(x=265, y=118).perform()
    就属于模拟根据坐标模拟点击动作.
    同样的调用方式我们可以使用的还有

    tap(self, element=None, x=None, y=None, count=1):
    press(self, el=None, x=None, y=None, pressure=None)
    long_press(self, el=None, x=None, y=None, duration=1000)
    wait(self, ms=0)
    move_to(self, el=None, x=None, y=None)
    

    但是使用坐标来操作界面就会遇到一个问题,比如actionSheet,alert,或者其他自定义的视图(效果如模态视图),操作会出现相应异常
    此时,我们就可以使用第二种方式来进行操作
    1 通过appium中的appSource查找你所要操作的元素


    image.png

    然后右侧SelectedElement中可以看到FindBy 和Selector

    btn=driver.find_element_by_xpath("//XCUIElementTypeButton[@name='home dt']")
    btn=driver.find_element_by_accessibility_id('home dt')
    #这俩是等效操作
    btn.click()
    

    相关文章

      网友评论

          本文标题:Appium iOS Python3(二)~脚本的使用

          本文链接:https://www.haomeiwen.com/subject/mehtectx.html