<gh_stars>0
from model.model_group import Group
import random

class GroupHelper:
    def __init__(self, app):
        self.app = app

    def select_group_by_index(self, index):
        wd = self.app.wd
        wd.find_elements_by_name("selected[]")[index].click()

    def select_group_by_id(self, id):
        wd = self.app.wd
        wd.find_element_by_css_selector("input[value='%s']" % id).click()

    def select_first_group(self):
        wd = self.app.wd
        self.select_group_by_index(0)

    def edit_if_not_none(self, field, text):
        wd = self.app.wd
        if text is not None:
            wd.find_element_by_name(field).click()
            wd.find_element_by_name(field).clear()
            wd.find_element_by_name(field).send_keys(text)
        else:
            pass

    def fill_group_form(self, group):
        wd = self.app.wd
        self.edit_if_not_none("group_name", group.name)
        self.edit_if_not_none("group_header", group.header)
        self.edit_if_not_none("group_footer", group.footer)

    def create(self, group):
        wd = self.app.wd
        # open_groups_page
        wd.find_element_by_link_text("groups").click()
        # init group creation
        wd.find_element_by_name("new").click()
        self.fill_group_form(group)
        # submit group creation
        wd.find_element_by_name("submit").click()
        self.return_to_groups_page()
        self.group_cache = None

    def edit_first_group(self):
        wd = self.app.wd
        self.edit_group_by_index(0)

    def edit_group_by_index(self, index, new_group_data):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_index(index)
        # click edit button
        wd.find_element_by_name("edit").click()
        self.fill_group_form(new_group_data)
        # submit edition
        wd.find_element_by_name("update").click()
        self.return_to_groups_page()
        self.group_cache = None

    def edit_group_by_id(self, id, new_group_data):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_id(id)
        # click edit button
        wd.find_element_by_name("edit").click()
        self.fill_group_form(new_group_data)
        # submit edition
        wd.find_element_by_name("update").click()
        self.return_to_groups_page()
        self.group_cache = None

    def add_selected_contact_to_selected_group_by_id(self, target_group):
        wd = self.app.wd
        # открыть выпадающий список
        to_group = wd.find_element_by_name("to_group")
        to_group.click()
        # выбор произвольной группы по value
        to_group.find_element_by_css_selector("[value='%s']" % target_group.id).click()
        wd.find_element_by_name("add").click()

    def select_some_group_to_view(self, target_group):
        wd = self.app.wd
        # открыть выпадающий список
        view_group = wd.find_element_by_name("group")
        view_group.click()
        # выбор произвольной группы по value
        view_group.find_element_by_css_selector("[value='%s']" % target_group.id).click()

#    def click_add_contact_to_group_button(self):
 #       wd = self.app.wd
 #       wd.find_element_by_name("add").click()
    #        self.app.navigation.open_home_page()

    def delete_first_group(self):
        wd = self.app.wd
        self.delete_group_by_index(0)

    def delete_group_by_index(self, index):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_index(index)
        # submit deletion
        wd.find_element_by_name("delete").click()
        self.return_to_groups_page()
        self.group_cache = None

    def delete_group_by_id(self, id):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_id(id)
        wd.find_element_by_name("delete").click()
        self.return_to_groups_page()
        self.group_cache = None

    def return_to_groups_page(self):
        wd = self.app.wd
        wd.find_element_by_link_text("group page").click()

    def count(self):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        return len(wd.find_elements_by_name("selected[]"))

    group_cache = None

    def get_group_list(self):
        if self.group_cache is None:
            wd = self.app.wd
            self.app.navigation.open_groups_page()
            self.group_cache = []
            wd.find_elements_by_css_selector("span.group")
            for element in wd.find_elements_by_css_selector("span.group"):
                text = element.text
                id = int(element.find_element_by_name("selected[]").get_attribute("value"))
                self.group_cache.append(Group(name=text, id=id))
        return list(self.group_cache)


from model.model_group import Group
import random

class GroupHelper:
    def __init__(self, app):
        self.app = app

    def select_group_by_index(self, index):
        wd = self.app.wd
        wd.find_elements_by_name("selected[]")[index].click()

    def select_group_by_id(self, id):
        wd = self.app.wd
        wd.find_element_by_css_selector("input[value='%s']" % id).click()

    def select_first_group(self):
        wd = self.app.wd
        self.select_group_by_index(0)

    def edit_if_not_none(self, field, text):
        wd = self.app.wd
        if text is not None:
            wd.find_element_by_name(field).click()
            wd.find_element_by_name(field).clear()
            wd.find_element_by_name(field).send_keys(text)
        else:
            pass

    def fill_group_form(self, group):
        wd = self.app.wd
        self.edit_if_not_none("group_name", group.name)
        self.edit_if_not_none("group_header", group.header)
        self.edit_if_not_none("group_footer", group.footer)

    def create(self, group):
        wd = self.app.wd
        # open_groups_page
        wd.find_element_by_link_text("groups").click()
        # init group creation
        wd.find_element_by_name("new").click()
        self.fill_group_form(group)
        # submit group creation
        wd.find_element_by_name("submit").click()
        self.return_to_groups_page()
        self.group_cache = None

    def edit_first_group(self):
        wd = self.app.wd
        self.edit_group_by_index(0)

    def edit_group_by_index(self, index, new_group_data):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_index(index)
        # click edit button
        wd.find_element_by_name("edit").click()
        self.fill_group_form(new_group_data)
        # submit edition
        wd.find_element_by_name("update").click()
        self.return_to_groups_page()
        self.group_cache = None

    def edit_group_by_id(self, id, new_group_data):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_id(id)
        # click edit button
        wd.find_element_by_name("edit").click()
        self.fill_group_form(new_group_data)
        # submit edition
        wd.find_element_by_name("update").click()
        self.return_to_groups_page()
        self.group_cache = None

    def add_selected_contact_to_selected_group_by_id(self, target_group):
        wd = self.app.wd
        # открыть выпадающий список
        to_group = wd.find_element_by_name("to_group")
        to_group.click()
        # выбор произвольной группы по value
        to_group.find_element_by_css_selector("[value='%s']" % target_group.id).click()
        wd.find_element_by_name("add").click()

    def select_some_group_to_view(self, target_group):
        wd = self.app.wd
        # открыть выпадающий список
        view_group = wd.find_element_by_name("group")
        view_group.click()
        # выбор произвольной группы по value
        view_group.find_element_by_css_selector("[value='%s']" % target_group.id).click()

#    def click_add_contact_to_group_button(self):
 #       wd = self.app.wd
 #       wd.find_element_by_name("add").click()
    #        self.app.navigation.open_home_page()

    def delete_first_group(self):
        wd = self.app.wd
        self.delete_group_by_index(0)

    def delete_group_by_index(self, index):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_index(index)
        # submit deletion
        wd.find_element_by_name("delete").click()
        self.return_to_groups_page()
        self.group_cache = None

    def delete_group_by_id(self, id):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        self.select_group_by_id(id)
        wd.find_element_by_name("delete").click()
        self.return_to_groups_page()
        self.group_cache = None

    def return_to_groups_page(self):
        wd = self.app.wd
        wd.find_element_by_link_text("group page").click()

    def count(self):
        wd = self.app.wd
        self.app.navigation.open_groups_page()
        return len(wd.find_elements_by_name("selected[]"))

    group_cache = None

    def get_group_list(self):
        if self.group_cache is None:
            wd = self.app.wd
            self.app.navigation.open_groups_page()
            self.group_cache = []
            wd.find_elements_by_css_selector("span.group")
            for element in wd.find_elements_by_css_selector("span.group"):
                text = element.text
                id = int(element.find_element_by_name("selected[]").get_attribute("value"))
                self.group_cache.append(Group(name=text, id=id))
        return list(self.group_cache)
# open_groups_page # init group creation # submit group creation # click edit button # submit edition # click edit button # submit edition # открыть выпадающий список # выбор произвольной группы по value # открыть выпадающий список # выбор произвольной группы по value #    def click_add_contact_to_group_button(self): #       wd = self.app.wd #       wd.find_element_by_name("add").click() #        self.app.navigation.open_home_page() # submit deletion