text: from model.contact import Contact
  import re
  
  
  class ContactHelper:
  
      def __init__(self, app):
          self.app = app
  
      def open_home_page(self):
          wd = self.app.wd
          if not (wd.current_url.endswith("/addressbook")):
              wd.find_element_by_link_text("home").click()
  
      def return_to_home_page(self):
          wd = self.app.wd
          if not (wd.current_url.endswith("/addressbook")):
              wd.find_element_by_link_text("home").click()
  
      def create(self, contact):
          wd = self.app.wd
          wd.find_element_by_link_text("add new").click()
          self.fill_contact_form(contact)
          self.save_form()
          self.return_to_home_page()
          self.contact_cache = None
  
      def fill_contact_form(self, contact):
          self.change_field_value("firstname", contact.firstname)
          self.change_field_value("middlename", contact.middlename)
          self.change_field_value("lastname", contact.lastname)
          self.change_field_value("nickname", contact.nik)
          self.change_field_value("title", contact.title)
          self.change_field_value("company", contact.company)
          self.change_field_value("address", contact.address)
          self.change_field_value("home", contact.home)
          self.change_field_value("mobile", contact.mobile)
          self.change_field_value("work", contact.work)
          self.change_field_value("fax", contact.fax)
          self.change_field_value("email", contact.email)
          self.change_field_value("email2", contact.email2)
          self.change_field_value("email3", contact.email3)
          self.change_field_value("homepage", contact.page)
          self.change_field_value("address2", contact.address2)
          self.change_field_value("phone2", contact.phone2)
          self.change_field_value("notes", contact.notes)
  
      def change_field_value(self, field_name, text):
          wd = self.app.wd
          if text is not None:
              wd.find_element_by_name(field_name).clear()
              wd.find_element_by_name(field_name).send_keys(text)
  
      def delete_first_contact(self):
          self.delete_contact_by_index(0)
  
      def delete_contact_by_index(self, index):
          wd = self.app.wd
          self.open_home_page()
          wd.find_elements_by_name("selected[]")[index].click()
          wd.find_element_by_xpath("//input[@value='Delete']").click()
          wd.switch_to_alert().accept()
          self.return_to_home_page()
          self.contact_cache = None
  
      def delete_contact_by_id(self, id):
          wd = self.app.wd
          self.open_home_page()
          self.select_contact_by_id(id)
          wd.find_element_by_xpath("//input[@value='Delete']").click()
          wd.switch_to_alert().accept()
          self.return_to_home_page()
          self.contact_cache = None
  
      def select_contact_by_id(self, id):
          wd = self.app.wd
          wd.find_element_by_css_selector("input[value='%s']" % id).click()
  
      def modify_first_contact(self, contact):
          self.modify_contact_by_index(contact, 0)
  
      def modify_contact_by_index(self, contact, index):
          wd = self.app.wd
          self.open_contact_to_edit_by_index(index)
          self.fill_contact_form(contact)
          wd.find_element_by_name("update").click()
          self.return_to_home_page()
          self.contact_cache = None
  
      def modify_contact_by_id(self, contact, id):
          wd = self.app.wd
          self.open_contact_to_edit_by_id(id)
          self.fill_contact_form(contact)
          wd.find_element_by_name("update").click()
          self.return_to_home_page()
          self.contact_cache = None
  
      def open_contact_to_edit_by_index(self, index):
          wd = self.app.wd
          self.open_home_page()
          wd.find_elements_by_css_selector("[title='Edit']")[index].click()
  
      def open_contact_to_edit_by_id(self, id):
          wd = self.app.wd
          self.open_home_page()
          wd.find_element_by_xpath("//a[@href='edit.php?id=%s']" % id).click()
  
      def open_contact_view_by_index(self, index):
          wd = self.app.wd
          self.open_home_page()
          wd.find_elements_by_css_selector("[title='Details']")[index].click()
  
      def save_form(self):
          wd = self.app.wd
          wd.find_element_by_xpath("(//input[@name='submit'])[2]").click()
  
      def count(self):
          wd = self.app.wd
          self.open_home_page()
          return len(wd.find_elements_by_name("selected[]"))
  
      contact_cache = None
  
      def get_contact_list(self):
          if self.contact_cache is None:
              wd = self.app.wd
              self.open_home_page()
              self.contact_cache = []
              for element in wd.find_elements_by_css_selector("tr[name='entry']"):
                  id = element.find_element_by_name("selected[]").get_attribute("value")
                  lastname = element.find_element_by_css_selector("td:nth-child(2)").text
                  name = element.find_element_by_css_selector("td:nth-child(3)").text
                  address = element.find_element_by_css_selector("td:nth-child(4)").text
                  all_emails = element.find_element_by_css_selector("td:nth-child(5)").text
                  all_contacts = element.find_element_by_css_selector("td:nth-child(6)").text
                  self.contact_cache.append(
                      Contact(firstname=name, lastname=lastname, id=id, address=address, all_emails=all_emails,
                              all_phones_from_home_page=all_contacts))
          return list(self.contact_cache)
  
      def get_contact_info_from_edit_page(self, index):
          self.open_contact_to_edit_by_index(index)
          wd = self.app.wd
          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")
          adress = wd.find_element_by_name("address").text
          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")
          home = wd.find_element_by_name("home").get_attribute("value")
          mobile = wd.find_element_by_name("mobile").get_attribute("value")
          work = wd.find_element_by_name("work").get_attribute("value")
          phone2 = wd.find_element_by_name("phone2").get_attribute("value")
          return Contact(firstname=firstname, lastname=lastname, home=home, mobile=mobile, work=work, phone2=phone2,
                         id=id, address=adress, 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
          home = self.find_in_content("H: ", text)
          mobile = self.find_in_content("M: ", text)
          work = self.find_in_content("W: ", text)
          phone2 = self.find_in_content("P: ", text)
          return Contact(home=home, mobile=mobile, work=work, phone2=phone2)
  
      def find_in_content(self, st, text):
          if text.find(st) >= 0:
              return re.search(st + "(.*)", text).group(1)
          else:
              return ""
  
      def add_to_group(self, contact_id, group_id):
          wd = self.app.wd
          self.open_home_page()
          self.select_contact_by_id(contact_id)
          wd.find_element_by_name("to_group").click()
          wd.find_element_by_css_selector('[name="to_group"] [value="%s"]' % group_id).click()
          wd.find_element_by_name("add").click()
          self.return_to_home_page()
  
      def delete_from_group(self, contact_id, group_id):
          wd = self.app.wd
          self.open_home_page()
          wd.find_element_by_name("group").click()
          wd.find_element_by_css_selector('[name="group"] [value="%s"]' % group_id).click()
          self.select_contact_by_id(contact_id)
          wd.find_element_by_name("remove").click()
          self.return_to_home_page()
  