text: from os import path
  from selenium.webdriver.support.select import Select
  from global_vars import root_path
  from model.contact import Contact
  import re
  
  
  class ContactHelper:
  
      def __init__(self, app):
          self.app = app
  
      def create(self, contact):
          wd = self.app.wd
          wd.find_element_by_link_text("add new").click()
          self.fill_contact_form(contact)
          wd.find_element_by_xpath("(//input[@name='submit'])[2]").click()
          self.contact_cache = None
  
      def create_simple_contact(self):
          self.create(Contact(firstname="Tester", middlename="Something", lastname="Trump",
                         photo="picture.jpg", nickname="super nickname", title="QA engineer",
                         company="Google", address="Kremlin", homephone="1111111",
                         mobilephone="2222222", workphone="3333333", fax="4444444",
                         email="test@test.com", email2="test2@test.com", email3="test3@test.com",
                         homepage="google.com", bday="29", bmonth="April", byear="1991", aday="22",
                         amonth="August", ayear="2015", address_2="Moscow", secondaryphone="5555555",
                         notes="Cool guy"))
  
      def delete_first_contact(self):
          self.delete_contact_by_index(0)
  
      def delete_contact_by_index(self, index):
          wd = self.app.wd
          self.open_contacts_page()
          wd.find_elements_by_name("selected[]")[index].click()
          wd.find_element_by_xpath("//input[@value='Delete']").click()
          wd.switch_to.alert.accept()
          wd.find_element_by_css_selector("div.msgbox")
          self.contact_cache = None
  
      def delete_contact_by_id(self, id):
          wd = self.app.wd
          self.open_contacts_page()
          wd.find_element_by_id(id).click()
          wd.find_element_by_xpath("//input[@value='Delete']").click()
          wd.switch_to.alert.accept()
          wd.find_element_by_css_selector("div.msgbox")
          self.contact_cache = None
  
      def edit_first_contact(self, contact):
          self.edit_contact_by_index(0, contact)
  
      def edit_contact_by_index(self, index, contact):
          wd = self.app.wd
          self.open_contacts_page()
          wd.find_elements_by_xpath("//img[@title='Edit']")[index].click()
          self.fill_contact_form(contact)
          wd.find_element_by_name("update").click()
          self.contact_cache = None
  
      def edit_contact_by_id(self, id, contact):
          wd = self.app.wd
          self.open_contacts_page()
          wd.find_element_by_xpath("//tr[@name='entry']//input[@id='%s']/../..//img[@title='Edit']" % str(id)).click()
          self.fill_contact_form(contact)
          wd.find_element_by_name("update").click()
          self.contact_cache = None
  
      def fill_contact_form(self, contact):
          wd = self.app.wd
          wd.find_element_by_name("firstname").click()
          wd.find_element_by_name("firstname").clear()
          wd.find_element_by_name("firstname").send_keys(contact.firstname)
          wd.find_element_by_name("middlename").click()
          wd.find_element_by_name("middlename").clear()
          wd.find_element_by_name("middlename").send_keys(contact.middlename)
          wd.find_element_by_name("lastname").click()
          wd.find_element_by_name("lastname").clear()
          wd.find_element_by_name("lastname").send_keys(contact.lastname)
          wd.find_element_by_name("nickname").click()
          wd.find_element_by_name("nickname").clear()
          wd.find_element_by_name("nickname").send_keys(contact.nickname)
          wd.find_element_by_name("photo").send_keys(path.join(root_path, "") + contact.photo)
          wd.find_element_by_name("title").click()
          wd.find_element_by_name("title").clear()
          wd.find_element_by_name("title").send_keys(contact.title)
          wd.find_element_by_name("company").click()
          wd.find_element_by_name("company").clear()
          wd.find_element_by_name("company").send_keys(contact.company)
          wd.find_element_by_name("address").click()
          wd.find_element_by_name("address").clear()
          wd.find_element_by_name("address").send_keys(contact.address)
          wd.find_element_by_name("home").click()
          wd.find_element_by_name("home").clear()
          wd.find_element_by_name("home").send_keys(contact.homephone)
          wd.find_element_by_name("mobile").click()
          wd.find_element_by_name("mobile").clear()
          wd.find_element_by_name("mobile").send_keys(contact.mobilephone)
          wd.find_element_by_name("work").click()
          wd.find_element_by_name("work").clear()
          wd.find_element_by_name("work").send_keys(contact.workphone)
          wd.find_element_by_name("fax").click()
          wd.find_element_by_name("fax").clear()
          wd.find_element_by_name("fax").send_keys(contact.fax)
          wd.find_element_by_name("email").click()
          wd.find_element_by_name("email").clear()
          wd.find_element_by_name("email").send_keys(contact.email)
          wd.find_element_by_name("email2").click()
          wd.find_element_by_name("email2").clear()
          wd.find_element_by_name("email2").send_keys(contact.email_2)
          wd.find_element_by_name("email3").click()
          wd.find_element_by_name("email3").clear()
          wd.find_element_by_name("email3").send_keys(contact.email_3)
          wd.find_element_by_name("homepage").click()
          wd.find_element_by_name("homepage").clear()
          wd.find_element_by_name("homepage").send_keys(contact.homepage)
          wd.find_element_by_name("bday").click()
          Select(wd.find_element_by_name("bday")).select_by_visible_text(contact.bday)
          wd.find_element_by_name("bmonth").click()
          Select(wd.find_element_by_name("bmonth")).select_by_visible_text(contact.bmonth)
          wd.find_element_by_name("byear").click()
          wd.find_element_by_name("byear").clear()
          wd.find_element_by_name("byear").send_keys(contact.byear)
          wd.find_element_by_name("aday").click()
          Select(wd.find_element_by_name("aday")).select_by_visible_text(contact.aday)
          wd.find_element_by_name("amonth").click()
          Select(wd.find_element_by_name("amonth")).select_by_visible_text(contact.amonth)
          wd.find_element_by_name("ayear").click()
          wd.find_element_by_name("ayear").clear()
          wd.find_element_by_name("ayear").send_keys(contact.ayear)
          wd.find_element_by_name("address2").click()
          wd.find_element_by_name("address2").clear()
          wd.find_element_by_name("address2").send_keys(contact.address_2)
          wd.find_element_by_name("phone2").click()
          wd.find_element_by_name("phone2").clear()
          wd.find_element_by_name("phone2").send_keys(contact.secondaryphone)
          wd.find_element_by_name("notes").click()
          wd.find_element_by_name("notes").clear()
          wd.find_element_by_name("notes").send_keys(contact.notes)
  
      def open_contacts_page(self):
          wd = self.app.wd
          if not (wd.current_url.endswith("/addressbook/index.php") and wd.find_element_by_id("search_count")):
              wd.find_element_by_link_text("home").click()
  
      def count(self):
          wd = self.app.wd
          self.open_contacts_page()
          return len(wd.find_elements_by_name("selected[]"))
  
      contact_cache = None
  
      def get_contacts_list(self):
          if self.contact_cache is None:
              wd = self.app.wd
              self.open_contacts_page()
              self.contact_cache = self.get_contacts_for_contacts_list()
          return self.contact_cache
  
      def open_contact_to_edit_by_index(self, index):
          wd = self.app.wd
          self.app.open_home_page()
          row = wd.find_elements_by_name("entry")[index]
          cell = row.find_elements_by_tag_name("td")[7]
          cell.find_element_by_tag_name("a").click()
  
      def open_contact_view_by_index(self, index):
          wd = self.app.wd
          self.app.open_home_page()
          row = wd.find_elements_by_name("entry")[index]
          cell = row.find_elements_by_tag_name("td")[6]
          cell.find_element_by_tag_name("a").click()
  
      def get_contact_info_from_edit_page(self, index):
          wd = self.app.wd
          self.open_contact_to_edit_by_index(index)
          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")
          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")
          secondaryphone = wd.find_element_by_name("phone2").get_attribute("value")
          address = wd.find_element_by_name("address").get_attribute("value")
          email = 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, homephone=homephone, workphone=workphone,
                         mobilephone=mobilephone, secondaryphone=secondaryphone, address=address,
                         email=email, email2=email2, email3=email3)
  
      def get_contact_from_view_page(self, index):
          wd = self.app.wd
          self.open_contact_view_by_index(index)
          text = wd.find_element_by_id("content").text
          homephone = re.search("H: (.*)", text).group(1)
          workphone = re.search("W: (.*)", text).group(1)
          mobilephone = re.search("M: (.*)", text).group(1)
          secondaryphone = re.search("P: (.*)", text).group(1)
          return Contact(homephone=homephone, workphone=workphone,
                         mobilephone=mobilephone, secondaryphone=secondaryphone)
  
      def add_contact_to_group(self, index, group_id):
          wd = self.app.wd
          self.app.contact.open_contacts_page()
          wd.find_elements_by_name("selected[]")[index].click()
          select = Select(wd.find_element_by_name("to_group"))
          select.select_by_value(group_id)
          wd.find_element_by_name("add").click()
  
      def get_contacts_for_the_group(self, group_id):
          wd = self.app.wd
          self.open_contacts_page()
          select = Select(wd.find_element_by_name("group"))
          select.select_by_value(group_id)
          return self.get_contacts_for_contacts_list()
  
      # returns contact position on the contacts page by contact id
      def get_contact_index_by_id(self, contact_id):
          contacts = self.get_contacts_list()
          for index, contact in enumerate(contacts):
              if contact.id == contact_id:
                  return index
  
      def remove_contact_from_group(self, index):
          wd = self.app.wd
          wd.find_elements_by_name("selected[]")[index].click()
          wd.find_element_by_name("remove").click()
  
      def get_contacts_for_contacts_list(self):
          wd = self.app.wd
          contacts = []
          for element in wd.find_elements_by_xpath("//tr[@name='entry']"):
              attributes = element.find_elements_by_xpath(".//td")
              lastname = attributes[1].text
              firstname = attributes[2].text
              id = element.find_element_by_name("selected[]").get_attribute("value")
              address = attributes[3].text
              all_emails = attributes[4].text
              all_phones = attributes[5].text
              contacts.append(Contact(firstname=firstname, lastname=lastname, address=address, id=id,
                                               all_phones_from_homepage=all_phones,
                                               all_emails_from_homepage=all_emails))
          return contacts
  
  
      def merge_phones_like_on_homepage(self, contact):
          return "\n".join(filter(lambda x: x != "",
                                  map(lambda x: self.clear(x),
                                      filter(lambda x: x is not None,
                                             [contact.homephone, contact.mobilephone, contact.workphone, contact.secondaryphone]))))
  
      def merge_emails_like_on_homepage(self, contact):
          return "\n".join(filter(lambda x: x != "",
                                  map(lambda x: self.truncate_whitespaces(x),
                                      filter(lambda x: x is not None,
                                             [contact.email, contact.email_2, contact.email_3]))))
  
      def clear(self, s):
          return self.truncate_whitespaces(re.sub("[() -]", "", s))
  
      def truncate_whitespaces(self, s):
          # remove extra whitespaces sequences in the middle of the string
          cleared_str = re.sub("\s+", " ", s)
          # cut whitespace in the end of string
          cleared_str = cleared_str.strip()
          return cleared_str
  
      # convert db data to look like the contact data from the home page
      def make_contacts_like_on_homepage(self, contacts):
          for contact in contacts:
              contact.firstname = self.truncate_whitespaces(contact.firstname)
              contact.lastname = self.truncate_whitespaces(contact.lastname)
              contact.address = self.truncate_whitespaces(contact.address)
              contact.all_phones_from_homepage = self.merge_phones_like_on_homepage(contact)
              contact.all_emails_from_homepage = self.merge_emails_like_on_homepage(contact)
          return contacts
  