text: from model.contact import Contact
  import re
  
  
  class ContactHelper:
  
      def __init__(self, app):
          self.app = app
  
      def create_new_contact(self):
          wd = self.app.wd
          # create new contact
          wd.find_element_by_link_text("add new").click()
  
      def edit_contact(self, index):
          wd = self.app.wd
          wd.find_elements_by_xpath("//img[@title='Edit']")[index].click()
  
      def edit_contact_by_id(self, id):
          wd = self.app.wd
          wd.find_element_by_css_selector("a[href='edit.php?id=%s']" %id).click()
  
      #< a
      #href = "edit.php?id=68" > < img
      #src = "icons/pencil.png"
      #title = "Edit"
      #alt = "Edit" > < / a >
      def fill_edit_form(self, contact, index):
          wd = self.app.wd
          self.edit_contact(index)
          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("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("update").click()
          wd.find_element_by_partial_link_text("home page").click()
          self.cache_contact = None
  
      def fill_edit_form_by_id(self, id, contact_now):
          wd = self.app.wd
          self.edit_contact_by_id(id)
          wd.find_element_by_name("firstname").click()
          wd.find_element_by_name("firstname").clear()
          wd.find_element_by_name("firstname").send_keys(contact_now.firstname)
          wd.find_element_by_name("middlename").click()
          wd.find_element_by_name("middlename").clear()
          wd.find_element_by_name("middlename").send_keys(contact_now.middlename)
          wd.find_element_by_name("lastname").click()
          wd.find_element_by_name("lastname").clear()
          wd.find_element_by_name("lastname").send_keys(contact_now.lastname)
          wd.find_element_by_name("nickname").click()
          wd.find_element_by_name("nickname").clear()
          wd.find_element_by_name("nickname").send_keys(contact_now.nickname)
          wd.find_element_by_name("company").click()
          wd.find_element_by_name("company").clear()
          wd.find_element_by_name("company").send_keys(contact_now.company)
          wd.find_element_by_name("address").click()
          wd.find_element_by_name("address").clear()
          wd.find_element_by_name("address").send_keys(contact_now.address)
          wd.find_element_by_name("update").click()
          wd.find_element_by_partial_link_text("home page").click()
          self.cache_contact = None
  
      def fill_out_form(self, contact):
          wd = self.app.wd
          self.create_new_contact()
          # fill out the form
          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("home").click()
          wd.find_element_by_name("home").clear()
          wd.find_element_by_name("home").send_keys(contact.homephone)
          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("mobile").click()
          wd.find_element_by_name("mobile").clear()
          wd.find_element_by_name("mobile").send_keys(contact.mobilephone)
          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("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.email2)
          wd.find_element_by_name("email3").click()
          wd.find_element_by_name("email3").clear()
          wd.find_element_by_name("email3").send_keys(contact.email3)
          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("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)
          self.submit_creating()
          wd.find_element_by_partial_link_text("home page").click()
          self.cache_contact = None
  
      def delete_contact_by_index(self, index):
          wd = self.app.wd
          wd.find_elements_by_name("selected[]")[index].click()
          wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
          wd.switch_to_alert().accept()
          self.cache_contact = None
  
      def select_contact_by_id(self, id):
          wd = self.app.wd
          wd.find_element_by_css_selector("input[value='%s']" % id).click()
  
      def delete_contact_by_id(self, id):
          wd = self.app.wd
          self.select_contact_by_id(id)
          wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
          wd.switch_to_alert().accept()
          self.cache_contact = None
  
  
      def add_cont_to_group(self):
          wd = self.app.wd
          wd.find_element_by_name("selected[]").click()
          wd.find_element_by_name("add").click()
          wd.find_element_by_partial_link_text("group page").click()
          self.cache_contact = None
  
      def add_cont_to_group_by_id(self, id, group_id):
          wd = self.app.wd
          self.select_contact_by_id(id)
          #wd.find_element_by_css_selector("select[name='to_group']").click()
          #wd.find_element_by_css_selector("option[value='%group_id']" %group_id).select()
          wd.find_element_by_xpath("//select[@name='to_group']/option[@value='%group_id']" %group_id).click()
          #wd.find_element_by_css_selector("a[href='edit.php?id=%s']" %id).click()
          wd.find_element_by_name("add").click()
          wd.find_element_by_partial_link_text("group page").click()
          self.cache_contact = None
  
      def submit_creating(self):
              wd = self.app.wd
              # submit creating
              wd.find_element_by_name("submit").click()
  
      def count(self):
          wd = self.app.wd
          self.app.open_home_page()
          return len(wd.find_elements_by_name("selected[]"))
  
      cache_contact = None
  
      def return_home(self):
          wd = self.app.wd
          if not(wd.current_url.endswith("/index.php")):
              wd.find_element_by_xpath("//div[@id='nav']//a[.='home']").click()
  
      def get_contact_list(self):
          if self.cache_contact is None:
              wd = self.app.wd
              self.return_home()
              self.cache_contact = []
              for element in wd.find_elements_by_name("entry"):
                  cells = element.find_elements_by_tag_name("td")
                  firstname = cells[2].text
                  lastname = cells[1].text
                  id = cells[0].find_element_by_tag_name("input").get_attribute("value")
                  address = cells[3].text
                  all_phones = cells[5].text
                  all_emails = cells[4].text
                  self.cache_contact.append(Contact(firstname=firstname, lastname=lastname,
                                                    all_phones_from_home_page=all_phones, all_emails_from_home_page=all_emails,
                                                    address=address, id=id))
          return list(self.cache_contact)
  
      def open_contat_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_contat_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")
          address = wd.find_element_by_name("address").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")
          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")
          secondaryphone = wd.find_element_by_name("phone2").get_attribute("value")
          return Contact(firstname=firstname, lastname=lastname, address=address, id=id,
                          homephone=homephone,
                          mobilephone=mobilephone, workphone=workphone, email=email, email2=email2,
                          email3=email3, secondaryphone=secondaryphone)
  
      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, mobilephone=mobilephone, workphone=workphone, secondaryphone=secondaryphone)
  
  