def get_contact_list(self): us_list = [] cursor = self.connection.cursor() try: cursor.execute( "select id, firstname, lastname, " "address, home, mobile, work, " "email, email2, email3 from addressbook where deprecated='0000-00-00 00:00:00'" ) for row in cursor.fetchall(): (us_id, firstname, lastname, address, home, mobile, work, email, email2, email3) = row us_list.append( Contact(id=str(us_id), firstname=firstname, lastname=lastname, address=address, homephone=home, mobilephone=mobile, workphone=work, email1=email, email2=email2, email3=email3)) finally: cursor.close() return us_list
def test_del_some_user(app, db, check_ui): # we remove 3 users to compensate 4 created for i in range(3): with allure.step('Cycle {}.'.format(i + 1)): if app.contact.count() == 0: app.contact.create( Contact(firstname="FIRST User first name", lastname="FIRST User last name", companyname="FIRST user center2m", address="FIRST User street, 21", id=None)) with allure.step('Given a list of contacts'): old_contacts = db.get_contact_list() user = random.choice(old_contacts) db_id = user.id index = None # Выясняем, какой ui index у выбранного контакта for c in range(len(old_contacts)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == db_id: index = c with allure.step('When I delete user "{}"'.format(user.firstname)): app.contact.delete_user_by_index(index) new_contacts = db.get_contact_list() with allure.step( 'New users list should be equal to old users list with deleted user removed' ): old_contacts.remove(user) assert sorted(old_contacts, key=Contact.id_or_max) == sorted( new_contacts, key=Contact.id_or_max) if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted( app.contact.get_contacts_list_from_table(), key=Contact.id_or_max)
def test_modify_some_user(app, db, check_ui): for mod_us in moduserdata: app.open_home_page() if len(db.get_contact_list()) == 0: app.contact.create( Contact(firstname="FIRST User first name", lastname="FIRST User last name", companyname="FIRST user center2m", address="FIRST User street, 21", id=None)) old_contacts = db.get_contact_list() user = random.choice(old_contacts) mod_us.id = user.id index = None for c in range(len(old_contacts)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == mod_us.id: index = c app.contact.modify_user_by_index(mod_us, index) new_contacts = db.get_contact_list() user.firstname = mod_us.firstname user.lastname = mod_us.lastname assert sorted(old_contacts, key=Contact.id_or_max) == sorted(new_contacts, key=Contact.id_or_max) if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted( app.contact.get_contacts_list_from_table(), key=Contact.id_or_max)
def test_modify_some_user(app, db, check_ui): for mod_us in moduserdata: app.open_home_page() if len(db.get_contact_list()) == 0: app.contact.create( Contact(firstname="FIRST User first name", lastname="FIRST User last name", companyname="FIRST user center2m", address="FIRST User street, 21", id=None)) with allure.step('Given a list of contacts'): old_contacts = db.get_contact_list() user = random.choice(old_contacts) mod_us.id = user.id index = None for c in range(len(old_contacts)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == mod_us.id: index = c with allure.step('When you modify user "{}"'.format(user.firstname)): app.contact.modify_user_by_index(mod_us, index) new_contacts = db.get_contact_list() with allure.step( 'Then new users list should be equal to old list where chosen user is modified' ): user.firstname = mod_us.firstname user.lastname = mod_us.lastname assert sorted(old_contacts, key=Contact.id_or_max) == sorted( new_contacts, key=Contact.id_or_max) if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted( app.contact.get_contacts_list_from_table(), key=Contact.id_or_max)
def get_phones_info_from_view_page(self, index): wd = self.app.wd self.open_user_view_by_index(index) full_text = wd.find_element_by_id("content").text homephone = "" if re.search("H: (.*)", full_text) is not None: homephone = re.search("H: (.*)", full_text).group(1) workphone = "" if re.search("W: (.*)", full_text) is not None: workphone = re.search("W: (.*)", full_text).group(1) mobilephone = "" if re.search("M: (.*)", full_text) is not None: mobilephone = re.search("M: (.*)", full_text).group(1) return Contact(homephone=homephone, mobilephone=mobilephone, workphone=workphone)
def test_add_contact_to_group(app, orm_db): app.open_home_page() groups = app.group.groups_without_empty_names(orm_db.get_group_list()) len_before = len(groups) for gr in range(len_before): if len(orm_db.get_contacts_not_in_group(groups[len_before-gr-1])) == 0: groups.pop(len_before-gr-1) if len(groups) == 0: app.contact.create(Contact(firstname="t_a_c_t_g User", lastname="t_a_c_t_g User last name", companyname="t_a_c_t_g user center2m", address="t_a_c_t_g User street, 21")) groups = app.group.groups_without_empty_names(orm_db.get_group_list()) if len(groups) == 0: app.group.create(Group(name="t_a_c_t_g Group")) groups = app.group.groups_without_empty_names(orm_db.get_group_list()) group = random.choice(groups) group_db_id = group.id cont_not_in_gr_list = orm_db.get_contacts_not_in_group(Group(id=group_db_id)) user_to_add = random.choice(cont_not_in_gr_list) index = 0 app.wd.get(app.base_url + "group.php") time.sleep(1) app.open_home_page() time.sleep(1) for c in range(len(orm_db.get_contact_list())): ui_id = app.contact.find_user_id_by_index(c) if ui_id == user_to_add.id: index = c app.contact.add_to_group(index, group_db_id) cont_list_after = None cont_list_after = orm_db.get_contacts_not_in_group(Group(id=group_db_id)) if cont_list_after is not None: for u in range(len(cont_list_after)): if cont_list_after[u].id == user_to_add.id: assert False assert True assert True
def get_contacts_list_from_table(self): if self.contacts_cache is None: wd = self.app.wd self.contacts_cache = [] for row in wd.find_elements_by_name("entry"): cells = row.find_elements_by_tag_name("td") id = cells[0].find_element_by_tag_name("input").get_attribute( "value") lastname = cells[1].text firstname = cells[2].text address = cells[3].text all_emails = cells[4].text all_phones = cells[5].text scanned_contact = Contact(firstname=firstname, lastname=lastname, id=id, address=address, all_phones_from_homepage=all_phones, all_emails_from_homepage=all_emails) self.contacts_cache.append(scanned_contact) return list(self.contacts_cache)
def test_del_some_user(app, db, check_ui): # we remove 4 users to compensate 4 created for i in range(4): if app.contact.count() == 0: app.contact.create(Contact(firstname="FIRST User first name", lastname="FIRST User last name", companyname="FIRST user center2m", address="FIRST User street, 21", id=None)) old_contacts = db.get_contact_list() user = random.choice(old_contacts) db_id = user.id index = None # Выясняем, какой ui index у выбранного контакта for c in range(len(old_contacts)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == db_id: index = c app.contact.delete_user_by_index(index) new_contacts = db.get_contact_list() old_contacts.remove(user) assert sorted(old_contacts, key=Contact.id_or_max) == sorted(new_contacts, key=Contact.id_or_max) if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted(app.contact.get_contacts_list_from_table(), key=Contact.id_or_max)
def get_contact_info_from_edit_page(self, index): wd = self.app.wd self.user_line_selected(index).find_element_by_xpath( ".//img[@alt='Edit']").click() firstname = wd.find_element_by_name("firstname").get_attribute("value") lastname = wd.find_element_by_name("lastname").get_attribute("value") id = wd.find_element_by_name("id").get_attribute("value") address = wd.find_element_by_name("address").get_attribute("value") homephone = wd.find_element_by_name("home").get_attribute("value") workphone = wd.find_element_by_name("work").get_attribute("value") mobilephone = wd.find_element_by_name("mobile").get_attribute("value") email1 = wd.find_element_by_name("email").get_attribute("value") email2 = wd.find_element_by_name("email2").get_attribute("value") email3 = wd.find_element_by_name("email3").get_attribute("value") return Contact(firstname=firstname, lastname=lastname, id=id, address=address, homephone=homephone, mobilephone=mobilephone, workphone=workphone, email1=email1, email2=email2, email3=email3)
# -*- coding: utf-8 -*- from model.user import Contact moduserdata = [ Contact(firstname="MODIFIED first name", lastname="X MODIFIED last name", companyname="MODIFIED center2m", address="MODIFIED street, 21", email3="*****@*****.**", homephone="+1(111)11-11111-11", mobilephone="", workphone="+3(333)33-33-33"), Contact(firstname="FIRST User first name", lastname="FIRST User last name", companyname="FIRST user center2m", address="FIRST User street, 21") ]
def test_delete_contact_from_group(app, orm_db): app.open_home_page() group_db_id = None user_to_remove_id = None groups = app.group.groups_without_empty_names(orm_db.get_group_list()) len_before = len(groups) for gr in range(len_before): # remove groups without contacts if len(orm_db.get_contacts_in_group(groups[len_before - gr - 1])) == 0: groups.pop(len_before - gr - 1) if len(groups) == 0: if len(orm_db.get_contact_list()) == 0: app.contact.create( Contact(firstname="t_a_c_t_g User first name", lastname="t_a_c_t_g User last name", companyname="t_a_c_t_g user center2m", address="t_a_c_t_g User street, 21", id=None)) if len(app.group.groups_without_empty_names( orm_db.get_group_list())) == 0: app.group.create( Group(name="t_a_c_t_g group", header="11 first group", footer="22 first group")) groups = app.group.groups_without_empty_names(orm_db.get_group_list()) group = random.choice(groups) group_db_id = group.id cont_of_gr_list = orm_db.get_contacts_not_in_group( Group(id=group_db_id)) user_to_add = random.choice(cont_of_gr_list) # let's specify the index of chosen user index = None app.wd.get(app.base_url + "group.php") time.sleep(1) app.open_home_page() time.sleep(1) for c in range(len(cont_of_gr_list)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == user_to_add.id: index = c app.contact.add_to_group(index, group_db_id) groups = app.group.groups_without_empty_names(orm_db.get_group_list()) len_before = len(groups) for gr in range(len_before): # remove groups without contacts if len(orm_db.get_contacts_in_group(groups[len_before - gr - 1])) == 0: # print("GROUP NAME WITHOUT CONNs = " + groups[len_before-gr-1].name) groups.pop(len_before - gr - 1) gr_ind = random.randrange(len(groups)) group_del_conn = groups[gr_ind] filtered_contact_list = orm_db.get_contacts_in_group(group_del_conn) count = len(filtered_contact_list) index = random.randrange(count) contact_deleted_id = app.contact.remove_user_from_group( group_del_conn.id, index) group_db_id = group_del_conn.id user_to_remove_id = contact_deleted_id cont_list_after = orm_db.get_contacts_in_group(Group(id=group_db_id)) for u in range(len(cont_list_after)): if cont_list_after[u].id == user_to_remove_id: assert False assert True
elif o == "-f": f = a def random_string(prefix, maxlen): symbols = string.ascii_letters + string.digits + " " * 10 return prefix + "".join( [random.choice(symbols) for i in range(random.randrange(maxlen))]) def random_digits(dig_len): symbols = string.digits return "".join([random.choice(symbols) for i in range(dig_len)]) testdata = [Contact(firstname="", lastname="", companyname="", address="")] + [ Contact( firstname=random_string("First_Name ", 10), lastname=random_string("Last_Name ", 10), companyname=random_string("Company ", 20), address=random_string("Address ", 20), email1= "*****@*****.**", # correct email parametrization is a bit complex thing homephone="+1(323)" + random_digits(7), mobilephone="+1(323)" + random_digits(7), workphone="+1(323)" + random_digits(7)) for i in range(n) ] file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f) with open(file_path, "w") as f_out:
def test_add_contact_to_group(app, orm_db): app.open_home_page() with allure.step('Given are existing contacts and groups'): groups = app.group.groups_without_empty_names(orm_db.get_group_list()) # Уберем из списка группы, куда уже нельзя добавить юзеров len_before = len(groups) for gr in range(len_before): if len( orm_db.get_contacts_not_in_group(groups[len_before - gr - 1])) == 0: groups.pop(len_before - gr - 1) # если после чистки групп с возможностью коннекта не осталось, добавим юзера if len(groups) == 0: app.contact.create( Contact(firstname="t_a_c_t_g User", lastname="t_a_c_t_g User last name", companyname="t_a_c_t_g user center2m", address="t_a_c_t_g User street, 21")) # если групп с непустыми названиями совсем нет, добавим группу и опять очистим groups = app.group.groups_without_empty_names( orm_db.get_group_list()) if len(groups) == 0: app.group.create(Group(name="t_a_c_t_g Group")) groups = app.group.groups_without_empty_names( orm_db.get_group_list()) # теперь во все группы из groups можно добавить хотя бы одного пользователя, поэтому выберем случайную with allure.step( 'Users and groups are prepared so it is possible to add user to group' ): group = random.choice(groups) group_db_id = group.id cont_not_in_gr_list = orm_db.get_contacts_not_in_group( Group(id=group_db_id)) # выбираем случайного пользователя user_to_add = random.choice(cont_not_in_gr_list) with allure.step('Then user "{}" is added to group "{}"'.format( user_to_add.firstname, group.name)): # let's specify the index of chosen user index = 0 app.wd.get(app.base_url + "group.php") time.sleep(1) app.open_home_page() time.sleep(1) # среди всех контактов найдем индекс нужного for c in range(len(orm_db.get_contact_list())): ui_id = app.contact.find_user_id_by_index(c) if ui_id == user_to_add.id: index = c app.contact.add_to_group(index, group_db_id) cont_list_after = None cont_list_after = orm_db.get_contacts_not_in_group( Group(id=group_db_id)) with allure.step('We check that user "{}" is added to group "{}"'.format( user_to_add.firstname, group.name)): # если контактов, не связанных с группой, нет, проверка не нужна - все уже правильно if cont_list_after is not None: # ищем id добавленного контакта среди недобавленных for u in range(len(cont_list_after)): if cont_list_after[u].id == user_to_add.id: assert False assert True assert True
def convert(contact): return Contact(id=str(contact.id), firstname=contact.firstname, lastname=contact.lastname)
def test_delete_contact_from_group(app, orm_db): app.open_home_page() with allure.step('I choose existing connection from contacts and groups'): group_db_id = None user_to_remove_id = None groups = app.group.groups_without_empty_names(orm_db.get_group_list()) len_before = len(groups) # почистим список групп for gr in range(len_before): # remove groups without contacts if len(orm_db.get_contacts_in_group(groups[len_before - gr - 1])) == 0: groups.pop(len_before - gr - 1) # если после чистки коннектов не осталось, ниже процесс создания коннекта if len(groups) == 0: # если совсем пользователей нет, добавим if len(orm_db.get_contact_list()) == 0: app.contact.create( Contact(firstname="t_a_c_t_g User first name", lastname="t_a_c_t_g User last name", companyname="t_a_c_t_g user center2m", address="t_a_c_t_g User street, 21", id=None)) # если групп с непустыми именами совсем нет, добавим if len( app.group.groups_without_empty_names( orm_db.get_group_list())) == 0: app.group.create( Group(name="t_a_c_t_g group", header="11 first group", footer="22 first group")) # теперь можно добавить хотя бы одного пользователя в случайную группу groups = app.group.groups_without_empty_names( orm_db.get_group_list()) group = random.choice(groups) group_db_id = group.id cont_of_gr_list = orm_db.get_contacts_not_in_group( Group(id=group_db_id)) # выбираем случайного пользователя user_to_add = random.choice(cont_of_gr_list) # let's specify the index of chosen user index = None app.wd.get(app.base_url + "group.php") time.sleep(1) app.open_home_page() time.sleep(1) for c in range(len(cont_of_gr_list)): ui_id = app.contact.find_user_id_by_index(c) if ui_id == user_to_add.id: index = c app.contact.add_to_group(index, group_db_id) # Проверки закончены, коннект точно есть. Берем случайную группу и удаляем любой контакт groups = app.group.groups_without_empty_names(orm_db.get_group_list()) # почистим список групп len_before = len(groups) for gr in range(len_before): # remove groups without contacts if len(orm_db.get_contacts_in_group(groups[len_before - gr - 1])) == 0: # print("GROUP NAME WITHOUT CONNs = " + groups[len_before-gr-1].name) groups.pop(len_before - gr - 1) gr_ind = random.randrange(len(groups)) group_del_conn = groups[gr_ind] filtered_contact_list = orm_db.get_contacts_in_group(group_del_conn) count = len(filtered_contact_list) index = random.randrange(count) with allure.step('When I remove random user from group "{}"'.format( group_del_conn.name)): # функция удаляет из группы юзера и возвращает его ID contact_deleted_id = app.contact.remove_user_from_group( group_del_conn.id, index) group_db_id = group_del_conn.id user_to_remove_id = contact_deleted_id with allure.step( 'Then this user should no more be in users list of group "{}"'. format(group_del_conn.name)): cont_list_after = orm_db.get_contacts_in_group(Group(id=group_db_id)) # ищем id удаленного контакта в группе for u in range(len(cont_list_after)): if cont_list_after[u].id == user_to_remove_id: assert False # если не нашли, порядок assert True