def generateTestCases(section): """ @summary 为类添加case @param section conf文件中的section """ conf = read_conf(section) setattr(MyTest, 'udid', conf[0]) setattr(MyTest, 'port', conf[1]) for args in range(1, 200): setattr(MyTest, 'test{}'.format(args), MyTest.getTestFunc(args)) add_log(conf[0], "填入属性test{}".format(args))
def port_list(self, driver): """ @summary 检验启动appium端口是否被占用,并写入list """ self.driver_port_list[0] = self.driver_port_list[1] + 1 while check_port(self.driver_port_list[0], driver) is False: self.driver_port_list[0] = self.driver_port_list[0] + 1 self.driver_port_list[1] = self.driver_port_list[0] + 1 while check_port(self.driver_port_list[1], driver) is False: self.driver_port_list[1] = self.driver_port_list[1] + 1 self.driver_port_list[2] += 1 while check_port(self.driver_port_list[2], driver) is False: self.driver_port_list[2] += 1 add_log(driver, "port list is {}".format(self.driver_port_list))
def check_port(port, driver, host='0.0.0.0'): """ @summary 检测端口是否被占用 @param port 检验的端口 @param host 默认参数,默认本机ip @return Flase 端口被占用 @return True 端口可以正常使用 """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((host, int(port))) s.shutdown(2) add_log(driver, "port {} is uesd !".format(port)) return False except: add_log(driver, "port {} is available!".format(port)) return True
def write_conf(self): """ @summmary 为每个生成driver配置appium启动参数:udid,port,bootstrap-port,selendroid-port """ with open('devices', 'r') as f: lines = f.readlines() for line in lines: line = line.rstrip() add_log(line, line) self.driver_number += 1 section = "device{}".format(self.driver_number) self.cf.add_section(section) self.port_list(list) self.cf.set(section, "udid", str(line)) self.cf.set(section, "port", str(self.driver_port_list[0])) self.cf.set(section, "bootstrap_port", str(self.driver_port_list[1])) self.cf.set(section, "selendroid_port", str(self.driver_port_list[2])) with open("drivers.conf", "w+") as f: self.cf.write(f)
def test(udid, lesson_id, driver): """ @summary 自动化case单元 @param lesson_id 课程表页课程id """ try: driver.find_element_by_accessibility_id('学习').click() driver.find_element_by_id( "com.xueersi.monkeyabc.app:id/menuRL").click() course_id = lesson_id % 5 + 1 driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout'\ '/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.'\ 'widget.RelativeLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/'\ 'android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[{}]'.\ format(course_id)).click() add_log(udid, "course_id is {}".format(course_id)) session_list = [3, 5, 6, 7] session_id = random.choice(session_list) driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout'\ '/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.'\ 'widget.RelativeLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[{}]'\ .format(session_id)).click() add_log(udid, "session_id is {}".format(session_id)) sleep(200) except Exception as e: traceback.format_exc() add_log(udid, traceback.format_exc())
def setUp(self): """ 初始化信息 """ start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) add_log(self.udid, "===============================================") add_log(self.udid, "case start time is " + start_time) desired_caps = {} desired_caps["platformName"] = "Android" desired_caps["platformVersion"] = "*" desired_caps["deviceName"] = "*" desired_caps['udid'] = self.udid desired_caps["automationName"] = "Appium" desired_caps["appPackage"] = "com.xueersi.monkeyabc.app" desired_caps[ "appActivity"] = "com.xueersi.yummy.app.business.splash.SplashActivity" desired_caps["noReset"] = "True" desired_caps["newCommandTimeout"] = "2400" add_log( self.udid, "http://127.0.0.1:{}/wd/hub || udid is {}".format( self.port, self.udid)) self.driver = webdriver.Remote( "http://127.0.0.1:{}/wd/hub".format(self.port), desired_caps) #self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps) self.driver.implicitly_wait(5)
def run_case(thread_name, section): add_log(read_conf(section)[0], "automation star") #event.wait() add_log( read_conf(section)[0], "{} run case Thread is {} \n".format(section, thread_name)) generateTestCases(section) unittest.main() add_log(read_conf(section)[0], "automation end")
def tearDown(self): end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) add_log(self.udid, "case quite time is " + end_time) add_log(self.udid, '===============================================') self.driver.quit()
def start_appium(thread_name, section): conf = read_conf(section) port = conf[1] bootstrap_port = conf[2] selendroid_port = conf[3] add_log(conf[0], conf) try: cmd = "node /Applications/Appium.app/Contents/Resources/app/node_modules"\ "/appium/build/lib/main.js --port {} --bootstrap-port {} --selendroid-port {}"\ .format(port, bootstrap_port, selendroid_port) add_log(conf[0], "cmd: " + cmd) return_code = subprocess.call(cmd, shell=True) add_log(conf[0], 'return_code:{}'.format(return_code)) if return_code != 0: add_log( conf[0], "start appium cmd not exce, port is {}, thread is {}".format( port, thread_name)) return False else: add_log( conf[0], "start appium, port is {}, thread is {}".format( port, thread_name)) #event.set(True) return True except: add_log( conf[0], "start appium cmd not exce, port is {}, thread is {}".format( port, thread_name)) return False