简体   繁体   English

如何在共享不同的设置和拆卸方法的同时,在一个脚本下运行多个测试用例?

[英]How to run multiple test cases under one script while sharing different setup and teardown methods?

I'm currently trying to do automate mobile testing and am wondering if there is any way that I can define different setup() methods (for example having different appPackage, appActivity) and run the test cases in the same script. 我目前正在尝试自动执行移动测试,并且想知道是否有任何方法可以定义不同的setup()方法(例如,具有不同的appPackage,appActivity)并在同一脚本中运行测试用例。

For example, I have written the below code that it will automate sending a message (appPackage is com.android.mms and appActivity is com.android.mms.ui.ConversationList). 例如,我编写了以下代码,它将自动发送消息(appPackage为com.android.mms,appActivity为com.android.mms.ui.ConversationList)。 But after selecting the option to add an attachment I need to add a picture which will then be using the appPackage is com.android.documentsui and appActivity is com.android.documentsui.DocumentsActivity), which is the action defined under case3 on my code. 但是在选择添加附件的选项后,我需要添加一张图片,然后将使用appPackage(com.android.documentsui和appActivity(com.android.documentsui.DocumentsActivity)),这是在我的代码中case3下定义的操作。

How can I handle this in the same script? 如何在同一脚本中处理此问题? Thanks in advance. 提前致谢。

import os, sys
import glob
import unittest
from appium import webdriver
from time import sleep

PLATFORM_VERSION = '5.1.1'


class EntranceTests(unittest.TestCase):

def setUp(self):
    print 'commandline args',sys.argv[1]
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1.1'
    desired_caps['deviceName'] = 'android'    
    desired_caps['udid'] = sys.argv[1] 
    desired_caps['appPackage'] = 'com.android.mms'
    desired_caps['appActivity'] = 'com.android.mms.ui.ConversationList'
    url = "http://localhost:{}/wd/hub".format(sys.argv[2])
    self.driver = webdriver.Remote(url, desired_caps)



def case2(self):
    addmsg = self.driver.find_element_by_xpath('//android.widget.ImageButton[contains(@index, "0")]')
    addmsg.click()

    editreceiver = self.driver.find_element_by_id('com.android.mms:id/recipients_editor')
    editreceiver.send_keys("0713414713")

    message = self.driver.find_element_by_id('com.android.mms:id/embedded_text_editor_btnstyle')
    message.send_keys("Test Message")

    attachment = self.driver.find_element_by_id('com.android.mms:id/add_attachment_second')
    attachment.click()

    addpic = self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Pictures")]')
    addpic.click()

    sendmsg = self.driver.find_element_by_id('com.android.mms:id/first_send_button_sms_view')
    sendmsg.click()



def case3(self):
    pic = self.driver.find_element_by_xpath('//android.widget.ImageView[contains(@index, "0")]')
    pic.click()



def testcase2(self):
    self.case2()
    self.case3()


def tearDown(self):
    self.driver.quit()


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(EntranceTests)
unittest.TextTestRunner(verbosity=2).run(suite)

To switch between apps, you use start_activity() . 要在应用之间切换,请使用start_activity() For your case, it will be - 对于您的情况,它将是-

def case3(self):
    pic = self.driver.find_element_by_xpath('//android.widget.ImageView[contains(@index, "0")]')
    pic.click()
    self.driver.start_activity("com.android.documentsui", "com.android.documentsui.DocumentsActivity")
    #do soemthing in the new app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM