def load_params(): root_path = get_path.get_root_Path() with open( os.path.join(root_path, "homework", "my_yaml", "case", "add_member_case.yml")) as f: params = yaml.safe_load(f) return params
def test_index_import(self, file_name, check_data): root = get_path.get_root_Path() file_path = os.path.join(root, file_name) mems = self.index.import_contact().upload(file_path).get_member() names = [mem[0] for mem in mems] check_names = [data[0] for data in check_data] assert set(check_names).issubset(set(names))
def get_config(self): config = configparser.ConfigParser() # 类实例化 # 定义文件路径 root_path = get_root_Path() path = os.path.join(root_path, "homework_selenium","iSelenium.ini") config.read(path) driver_path = config["driver"]["chrome_driver"] return driver_path
class WeWorkApi(BaseApi): root_path = get_path.get_root_Path() def access_token(self, secret): ##通讯录管理-"K_s1vBSMulzcOe_r3P1CB7JvqZ_QWUUpQ6kwbJaia_U" url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" corpid = "ww0c8f13621be7beef" data = { "method": "get", "url": url, "params": { "corpid": corpid, "corpsecret": secret } } token = None # response = requests.get(url=url, params=data) response = self.send_request(**data) file = os.path.join(self.root_path, "contact_tocken.txt") if response.status_code == 200: result = json.loads(response.content) if result["errcode"] == 0: token = result["access_token"] result["expires_date"] = time.time() + result["expires_in"] with open(file, "w", encoding="utf-8") as f: json.dump(result, f) else: raise ValueError("access token error!") else: with open(file, "w", encoding="utf-8") as f: json.dump("", f) return token def get_token(self, secret): token = None ##从文件里获取token is_exist = os.path.exists( os.path.join(self.root_path, "contact_tocken.txt")) if is_exist: file_size = os.path.getsize( os.path.join(self.root_path, "contact_tocken.txt")) if file_size > 0: with open(os.path.join(self.root_path, "contact_tocken.txt"), "r", encoding="utf-8") as f: token_data = json.load(f) ##判断token是否过期 expires_date = token_data["expires_date"] if expires_date < time.time(): ##token已过期,需重新获取 token = self.access_token(secret) else: ##token未过期,可直接使用 token = token_data["access_token"] else: ##文件为空,调用接口获取token,并且写入文件 token = self.access_token(secret) ##文件不存在,调用接口获取token,并且写入文件 else: token = self.access_token(secret) return token
def start(self): if self._driver is None: root_path = get_path.get_root_Path()##'D:\\workspace\\pyworkspace\\TestDevApp\\pythoncode' config_path = os.path.join(root_path,"homework","my_yaml","config","xueqiu_config.yml") with open(config_path) as f : config = yaml.safe_load(f) desire_caps = config[0] desire_caps["appPackage"]=self._appPackage desire_caps["appActivity"] = self._appActivity address = config[1]["address"] self._driver = webdriver.Remote(address,desire_caps) else: self._driver.start_activity(self._appPackage,self._appActivity)
def start_case(): print("case开始....") yield print("case结束....") #命令行添加参数 def pytest_addoption(parser): group = parser.getgroup("my_args_group") group.addoption("--env", action="store", dest="env", default="test", help="将命令行参数 '--env' 添加到 pytest 配置中") root_path = get_root_Path() path = os.path.join(root_path, "my_yaml") def pytest_generate_tests(metafunc): #若方法参数包含start_case,则加载参数 if ("start_case" in metafunc.fixturenames): #获取命令行参数env的值,默认为test enviroment = metafunc.config.getoption("env") try: #从类变量func_params中获取方法对应的参数化文件及参数名称 #例:func_params = {"test_all": ["name,userid,mobile,department", "add_member_api.yml"]} param = metafunc.cls.func_params[metafunc.function.__name__] # @pytest.mark.parametrize("value1,value2,check",get_params(os.path.join(path,"add_case.yml"))) # tmp = metafunc.cls.get_params(os.path.join(path, enviroment, param[1])) #根据不同的env,获取不同路径下的yml文件,为方法进行参数化
def goto_add_member(self): self.steps( os.path.join(get_path.get_root_Path(), "homework", "my_yaml", "steps", "wework", "goto_add_member_steps.yml")) from pythoncode.homework.po.wework.add_member import AddMember return AddMember(self._driver)
def goto_contact(self): self.steps( os.path.join(get_path.get_root_Path(), "homework", "my_yaml", "steps", "wework", "goto_contact_steps.yml")) return Contact(self._driver)
def goto_add_manual(self): self.steps( os.path.join(get_path.get_root_Path(), "homework", "my_yaml", "steps", "wework", "goto_add_member_manual_steps.yml")) return EditMember(self._driver)
def test_index_import_empty(self): root = get_path.get_root_Path() file_path = os.path.join(root,"empty.xlsx") title = WorkWeixinIndex(self.driver).import_contact().upload_empty(file_path) assert title=="通讯录上传未成功"
def test_import_unvalid_index(self): root = get_path.get_root_Path() file_path = os.path.join(root, "test.gif") file = WorkWeixinIndex(self.driver).import_contact().upload_unvalid(file_path) assert file=="test.gif"
def test_import_index(self): root = get_path.get_root_Path() file_path = os.path.join(root,"import_contact_template.xlsx") WorkWeixinIndex(self.driver).import_contact().upload(file_path)
def login(self): file_path = os.path.join(get_root_Path(), "cookies.json") self.add_cookie(file_path) self.refresh() self.wait_default(10,"clickable",self.id,"menu_index") return WorkWeixinIndex(self._driver)
def test_index_import_unvalid(self, file_name): root = get_path.get_root_Path() file_path = os.path.join(root, file_name) text = self.index.import_contact().upload_unvalid(file_path) assert text == "将文件拖拽至此区域"
def test_index_import_empty(self, file_name): root = get_path.get_root_Path() file_path = os.path.join(root, file_name) title = self.index.import_contact().upload_empty(file_path) assert title == "通讯录上传未成功"