Ejemplo n.º 1
0
class ForgotPasswordPage(BasePage):
    page_load_timeout = 10
    change_password_timeout = 10
    path_page = "/account/forgot_password"

    txb_email = ElementFactory.get_element_until(
        by=By.ID,
        locator="email-input",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    btn_send = ElementFactory.get_element_until(
        by=By.ID,
        locator="signin-button",
        wait_type=element_to_be_clickable,
        element_cls=SeleniumElement)

    lbl_error_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//*[@id='signin-button']/preceding-sibling::*",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    def open_forgot_password_page(self):
        self.open_page(self.path_page)

    def wait_for_forgot_password_page(self):
        self.wait_for_page(self.path_page, self.page_load_timeout)

    def submit_email(self, email):
        self.txb_email.clear_and_send_keys(email)
        self.btn_send.click()
Ejemplo n.º 2
0
class ChatBoxSimulator(BasePage):
    path_page = "/admin/setting_bot/appearance"
    btn_drop_down_list = ElementFactory.get_element_until(
        by=By.ID,
        locator="dropDownListBotTriggerButton",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement)
    txt_input_message = ElementFactory.get_element_until(
        by=By.ID,
        locator="inputmessage",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement)
    btn_send_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//textarea[contains(@id,'inputmessage')]"
        "/following-sibling::span[1]/*",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement)

    def open_configure_appearance_page(self):
        self.open_page(self.path_page)

    def send_message(self, message):
        self.txt_input_message.send_keys(message)
        self.txt_input_message.send_keys(Keys.RETURN)

    def get_result(self, messages):
        result = messages
        response = self.get_responses_list()
        for i in range(1, len(messages)):
            result[i].append(response[i][0])
        return result

    def get_responses_list(self):
        responses_list = []
        conversation = self.get_conversation()
        messages_data = json.loads(conversation)['chatbox']
        for message in messages_data['messages']:
            if not message['isUser']:
                responses_list.append([message['data']])
        return responses_list[0:]

    def get_conversation(self):
        storage = LocalStorage(self.driver)
        return storage.get('state')

    def clear_conversation(self):
        storage = LocalStorage(self.driver)
        storage.remove
 def base_component_element(self, ele_class: T) -> T:
     '''
     Because lazy initial finding we need to
     call __get__ method for finding element
     :param ele_class:
     :return: WebElement base on element class
     '''
     base_element = ElementFactory.get_element(
         ele_class, self.base_component_locator[0],
         self.base_component_locator[1])
     base_element()
     return base_element
class RegisterPage(BasePage):
    timeout = 10
    path_page = "/account/register"
    txt_username = ElementFactory.get_element(by=By.CSS_SELECTOR,
                                              locator="input#name-input",
                                              element_cls=SeleniumElement)
    txt_email = ElementFactory.get_element(by=By.CSS_SELECTOR,
                                           locator="input#email-input",
                                           element_cls=SeleniumElement)
    txt_password = ElementFactory.get_element(by=By.CSS_SELECTOR,
                                              locator="#password-input",
                                              element_cls=SeleniumElement)
    btn_register = ElementFactory.get_element(by=By.CSS_SELECTOR,
                                              locator="#register-button",
                                              element_cls=SeleniumElement)
    btn_register_temp = ElementFactory.get_element(
        by=By.XPATH,
        locator="//button[text()='REGISTER']",
        element_cls=InputElement)
    lbl_error_message = ElementFactory.get_element(
        by=By.XPATH,
        locator="//button[text()='Register again']/../span",
        element_cls=SeleniumElement)
    ckb_remember_me = ElementFactory.get_element(
        by=By.ID, locator="remember-me-checkbox", element_cls=SeleniumElement)

    @property
    def txt_header_text(self):
        return "Get started!"

    def open_register_page(self):
        self.open_page(self.path_page)

    def login_with_new_account(self, username, email, password):
        self.register_with_account(username, email, password)
        self.agree_with_terms_and_conditions()
        self.click_register_button()

    def register_with_account(self, username, email, password):
        self.txt_username.send_keys(username)
        self.txt_email.send_keys(email)
        self.txt_password.send_keys(password)

    def agree_with_terms_and_conditions(self, is_agree=True):
        if is_agree != self.ckb_remember_me.is_selected():
            self.driver.core_driver.execute_script(
                "arguments[0].click()",
                self.ckb_remember_me.get_wrapper_element)

    def click_register_button(self):
        self.btn_register.click()

    def wait_for_register_page(self):
        self.wait_for_page(self.path_page, self.timeout)
class IntegrationPage(BasePage):
    path_page = "/admin/setting_bot/integration"

    txb_script = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//*[@id='copy-button']/../following-sibling::div",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement)

    def get_script(self):
        return self.txb_script.get_element_text()

    def open_integration_page(self):
        self.open_page(self.path_page)
Ejemplo n.º 6
0
class ResetPasswordPage(BasePage):
    page_load_timeout = 10
    change_password_timeout = 10
    path_page = "/account/reset_password"

    txb_new_password = ElementFactory.get_element_until(
        by=By.ID,
        locator="password-input",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    btn_change_password = ElementFactory.get_element_until(
        by=By.ID,
        locator="signin-button",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    lbl_error_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//*[@id='signin-button']/preceding-sibling::*",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    def open_reset_password_page(self, token):
        self.open_page(ResetPasswordPage.get_full_url_from_token(token))

    def wait_for_forgot_password_page(self):
        self.txb_new_password

    def reset_password(self, new_password):
        self.txb_new_password.clear_and_send_keys(new_password)
        self.btn_change_password.click()

    @staticmethod
    def get_full_url_from_token(token):
        url_pattern = "{}/auth/recover_password?token={}"
        return url_pattern.format(Settings.BASE_URL, token)
class LoginPage(BasePage):
    path_page = "/account/login"
    loading_page_timeout = 10
    txt_email = ElementFactory.get_element(by=By.ID, locator="email-input", element_cls=SeleniumElement)
    txt_password = ElementFactory.get_element(by=By.ID, locator="password-input", element_cls=SeleniumElement)
    btn_login = ElementFactory.get_element(by=By.ID, locator="signin-button", element_cls=SeleniumElement)
    lbl_error_message = ElementFactory.get_element(by=By.XPATH, locator="//button[text()='Sign in again']/../span",
                                                   element_cls=SeleniumElement)
    ckb_remember_me = ElementFactory.get_element(by=By.ID, locator="remember-me-checkbox",
                                                 element_cls=SeleniumElement)

    @property
    def txt_header_text(self):
        return "Nice to see you here!"

    def login_with_account(self, email, password):
        self.open_login_page()
        self.input_login_with_account(email, password)
        self.click_login_button()

    def open_login_page(self):
        self.open_page(self.path_page)

    def input_login_with_account(self, email, password):
        self.txt_email.clear_and_send_keys(email)
        self.txt_password.clear_and_send_keys(password)

    def click_login_button(self):
        self.btn_login.click()

    def wait_for_log_in_page(self):
        self.wait_for_page(path=self.path_page, timeout=self.loading_page_timeout)

    def check_remember_me(self, check=True):
        if check != self.ckb_remember_me.is_selected():
            self.driver.core_driver.execute_script("arguments[0].click()", self.ckb_remember_me.get_wrapper_element)
class ConversationPage(BasePage):
    path_page = "/admin/conversations"
    list_conversations_container = ElementFactory.get_list_element(
        by=By.XPATH,
        locator="//*[contains(@class, 'list') and contains(@class,'conversations')]//*[contains(@class,'item')]",
        element_cls=BaseElement
    )

    def __init__(self):
        self.__loader_component = LoaderComponent()

    def open_conversation_page(self):
        self.open_page(path=self.path_page)

    def get_last_message(self, conversation_index=0):
        self.__loader_component.wait_for_component_invisible()
        txt_last_message = self.list_conversations_container[conversation_index].find_element(
            by=By.TAG_NAME,
            value="abbr",
        )
        return txt_last_message.text
Ejemplo n.º 9
0
class ChangePasswordPage(BasePage):
    page_load_timeout = 10
    change_password_timeout = 10
    path_page = "/admin/manage_account/change_password"
    txb_old_password = ElementFactory.get_element_until(
        by=By.ID,
        locator="old-password-input",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    txb_new_password = ElementFactory.get_element_until(
        by=By.ID,
        locator="new-password-input",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    txb_confirm_password = ElementFactory.get_element_until(
        by=By.ID,
        locator="confirm-new-password-input",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    btn_submit = ElementFactory.get_element_until(
        by=By.ID,
        locator="change-password-button",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    btn_cancel = ElementFactory.get_element_until(
        by=By.ID,
        locator="cancel-button",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    txt_error_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//button[@id='change-password-button']/../../span",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    txt_success_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//*[@id='return-dashboard-button']/../../span",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)
    btn_back_to_dashboard = ElementFactory.get_element_until(
        by=By.ID,
        locator="return-dashboard-button",
        wait_type=visibility_of_element_located,
        element_cls=SeleniumElement)

    def open_change_password_page(self):
        self.open_page(self.path_page)

    def wait_for_change_password_page(self):
        self.wait_for_page(self.path_page, self.page_load_timeout)

    def change_password(self,
                        old_password,
                        new_password,
                        confirm_password=None):
        self.txb_old_password.clear_and_send_keys(old_password)
        self.txb_new_password.clear_and_send_keys(new_password)
        if confirm_password is not None:
            self.txb_confirm_password.clear_and_send_keys(confirm_password)
        else:
            self.txb_confirm_password.clear_and_send_keys(new_password)
        self.btn_submit.click()

    def get_error_message(self):
        return self.txt_error_message.get_element_text()

    def get_success_message(self):
        return self.txt_success_message.get_element_text()

    # TODO(namndoan): it may not work
    def wait_for_button_submit_disappear(self):
        return WebDriverWait(
            driver=self.driver, timeout=self.change_password_timeout
        ).until(
            staleness_of(self.btn_submit.get_wrapper_element),
            f"Button submit is still displayed after {self.change_password_timeout}s"
        )

    def wait_for_button_back_to_dashboard_display(self):
        # Wait for button back to dashboard visible
        return self.btn_back_to_dashboard

    # Additional code for logging out
    div_user_options = ElementFactory.get_element_until(
        element_cls=SeleniumElement,
        by=By.ID,
        locator="user-options-dropdown",
        wait_type=visibility_of_element_located)
    btn_log_out = ElementFactory.get_element_until(
        element_cls=SeleniumElement,
        by=By.XPATH,
        locator=
        "//*[@id='user-options-dropdown']//div//span[text()='Sign-out']",
        wait_type=visibility_of_element_located)

    def log_out(self):
        self.div_user_options.click()
        self.btn_log_out.click()
Ejemplo n.º 10
0
class KnowledgePage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        self.__add_faq_url_component = AddFaqUrlComponent()
        self.__loader_component = LoaderComponent()
        self.__faq_knowledge_datatable_component = FaqKnowledgeDataTableComponent(
        )

    timeout = 10
    add_url_knowledge_timeout = 300
    path_page = "/admin/setting_bot/knowledge"
    lbl_header_title = ElementFactory.get_element_until(
        by=By.CSS_SELECTOR,
        element_cls=BaseElement,
        locator="h2.header",
        wait_type=EC.visibility_of_element_located,
        time_out=120)

    btn_add_knowledge = ElementFactory.get_element_until(
        element_cls=ButtonElement,
        by=By.XPATH,
        locator="//div[contains(@class,'knowledge-styles') "
        "and @role='listbox']",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    btn_from_faq_url = ElementFactory.get_element_until(
        element_cls=ButtonElement,
        by=By.ID,
        locator="optionAddFAQUrl",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    btn_from_faq_manual_qa = ElementFactory.get_element_until(
        element_cls=ButtonElement,
        by=By.ID,
        locator="optionAddNewPairManually",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    btn_ok_add_faq_url = ElementFactory.get_element_until(
        element_cls=ButtonElement,
        by=By.ID,
        locator="submit-url-button",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    btn_cancel_add_faq_url = ElementFactory.get_element_until(
        element_cls=ButtonElement,
        by=By.ID,
        locator="cancel-process-button",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    txt_from_faq_url = ElementFactory.get_element_until(
        element_cls=InputElement,
        by=By.XPATH,
        locator="//div[contains(@class,'knowledge-styles')]"
        "//input[@placeholder='Enter a FAQ URL here']",
        wait_type=EC.element_to_be_clickable,
        time_out=30)

    def add_manual_faq_url(self, faq_url):
        self.__loader_component.wait_for_component_invisible()
        self.btn_add_knowledge.click()
        self.btn_from_faq_url.click()
        self.txt_from_faq_url.send_keys(faq_url)
        self.btn_ok_add_faq_url.click()

    def init_manual_question_pair_table(self):
        self.__loader_component.wait_for_component_invisible()
        self.btn_add_knowledge.click()
        self.btn_from_faq_manual_qa.click()
        self.__faq_knowledge_datatable_component.wait_for_manual_faq_section_visible(
        )

    def add_question_answer_data(self, question, answer):
        self.__faq_knowledge_datatable_component.input_new_manual_question_pair(
            question, answer)

    def modify_question_answer_at_index(self, index, question, answer):
        self.__faq_knowledge_datatable_component.modify_question_pair_at_index(
            index, question, answer)

    def get_faq_url_data(self) -> List['str']:
        self.__add_faq_url_component.wait_for_component_remove()
        return self.__faq_knowledge_datatable_component.get_list_knowledge_title(
        )

    def get_question_pair_data_in_gui(self):
        return self.__faq_knowledge_datatable_component.get_all_question_pair_data_in_table(
        )

    def get_faq_url_data_in_gui(self, faq_url):
        self.__faq_knowledge_datatable_component.click_on_data_table_with_faq_url(
            faq_url)
        return self.__faq_knowledge_datatable_component.get_all_faq_data_in_table_with_faq_url(
            faq_url)

    # Work
    def wait_for_create_page(self, time_out=None):
        time_out = time_out if time_out is not None else self.timeout
        self.wait_for_page(self.path_page, time_out)
        self.lbl_header_title

    def wait_for_knowledge_section_visible(self, title):
        component = FaqKnowledgeDataTableComponent.get_table_component_with_group_faqs(
            title)
        component.wait_for_component_visible(self.add_url_knowledge_timeout)

    def open_knowledge_page(self):
        self.open_page(self.path_page)
class CreateBotPage(BasePage):
    timeout = 10
    path_page = "/admin/manage_account/create_bot"
    txt_website_url = ElementFactory.get_element_until(
        by=By.ID,
        locator="website-input",
        wait_type=EC.visibility_of_element_located,
        element_cls=SeleniumElement)
    txt_bot_name = ElementFactory.get_element_until(
        by=By.ID,
        locator="chatbot-name-input",
        wait_type=EC.visibility_of_element_located,
        element_cls=SeleniumElement)
    btn_create_bot = ElementFactory.get_element_until(
        by=By.ID,
        locator="create-button",
        wait_type=EC.visibility_of_element_located,
        element_cls=SeleniumElement)

    lbl_error_message = ElementFactory.get_element(
        by=By.XPATH,
        locator="//button[@id='create-button']/../span",
        element_cls=SeleniumElement)
    # Should move to common page object
    txt_bottom_left_noti = ElementFactory.get_element(
        by=By.CSS_SELECTOR,
        locator="#root div.notifications-wrapper div.notification-message",
        element_cls=SeleniumElement)

    txt_header = ElementFactory.get_element_until(
        by=By.CSS_SELECTOR,
        locator="h2.header",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement)

    def __init__(self):
        self.__db_helper = DatabaseHelper(DatabaseType.MONGO_DB).database_query
        self.__loader_component = LoaderComponent()

    @property
    def txt_header_text(self):
        return "Create your virtual assistant"

    def get_header_text(self) -> str:
        return self.txt_header.get_element_text()

    def open_create_page(self):
        self.open_page(self.path_page)

    def create_bot_with_data(self, bot_name, website_url):
        self.input_bot_info_with_data(bot_name, website_url)
        self.click_create_bot_button()
        self.__loader_component.wait_for_component_invisible(60)

    def input_bot_info_with_data(self, bot_name, website_url):
        self.txt_bot_name.send_keys(bot_name)
        self.txt_website_url.send_keys(website_url)

    def click_create_bot_button(self):
        self.btn_create_bot.click()

    def wait_for_create_page(self):
        self.wait_for_page(self.path_page, self.timeout)

    def get_bot_data_via_user_email(self, user_email):
        return self.__db_helper.get_item_in_collection(
            "Bot", {"creators.0.email": user_email})

    def get_bot_data_via_user_email_and_bot_name(self, user_email, bot_name):
        return self.__db_helper.get_item_in_collection("Bot", {
            "creators.0.email": user_email,
            "name": bot_name
        })

    def get_list_question_via_bot_id(self, bot_id):
        return self.__db_helper.get_list_item_in_collection(
            "Qna", {"botId": bot_id})
Ejemplo n.º 12
0
class AppearancePage(BasePage):
    timeout = 10
    update_info_timeout = 1
    path_page = "/admin/setting_bot/appearance"
    lbl_header_title = ElementFactory.get_element_until(by=By.CSS_SELECTOR, element_cls=SeleniumElement,
                                                        locator="h2.header",
                                                        wait_type=EC.visibility_of_element_located,
                                                        time_out=120
                                                        )

    txb_title = ElementFactory.get_element_until(by=By.ID, locator="titleField",
                                                 wait_type=EC.visibility_of_element_located,
                                                 element_cls=SeleniumElement)

    txb_bot_name = ElementFactory.get_element_until(by=By.ID, locator="nameField",
                                                    wait_type=EC.visibility_of_element_located,
                                                    element_cls=SeleniumElement)

    img_avatar = ElementFactory.get_element_until(by=By.XPATH, locator="//*[@id='nameField']/../../..//img",
                                                  wait_type=EC.visibility_of_element_located,
                                                  element_cls=SeleniumElement)

    input_ava = ElementFactory.get_element_until(by=By.XPATH, locator="//input[@accept]",
                                                 wait_type=EC.visibility_of_element_located,
                                                 element_cls=SeleniumElement)

    lbl_error_message_of_upload_avatar = ElementFactory.get_element_until(by=By.XPATH,
                                                                          locator="//input[@accept]/../../span",
                                                                          wait_type=EC.visibility_of_element_located,
                                                                          element_cls=SeleniumElement)

    btn_cancel_upload_avatar = ElementFactory.get_element_until(by=By.XPATH,
                                                                locator="//button[@name='cancelButton']",
                                                                wait_type=EC.visibility_of_element_located,
                                                                element_cls=SeleniumElement
                                                                )

    btn_save_uploaded_avatar = ElementFactory.get_element_until(by=By.XPATH,
                                                                locator="//button[@name='saveButton']",
                                                                wait_type=EC.visibility_of_element_located,
                                                                element_cls=SeleniumElement)

    def get_header_title_text(self):
        return self.lbl_header_title.get_element_text()

    def get_expected_title(self, bot_name):
        # Title is converted to uppercase
        return "{}'S APPEARANCE".format(bot_name.upper())

    def open_appearance_page(self):
        self.open_page(self.path_page)

    def wait_for_appearance_page(self, time_out=None):
        time_out = time_out if time_out is not None else self.timeout
        self.wait_for_page(self.path_page, time_out)

    def change_bot_title(self, new_title):
        # Work around instead of clear_and_send_keys due to element is not user-editable
        self.txb_title.click()
        self.txb_title.clear_and_send_keys(new_title)
        self.lbl_header_title.click()
        time.sleep(self.update_info_timeout)

    def change_bot_name(self, new_name):
        self.txb_bot_name.click()
        self.txb_bot_name.clear_and_send_keys(new_name)
        self.lbl_header_title.click()
        time.sleep(self.update_info_timeout)

    def update_avatar(self, file_path):
        self.img_avatar.click()
        self.input_ava.send_keys(file_path)
        self.btn_save_uploaded_avatar.click()
class FaqKnowledgeDataTableComponent(BaseComponent):
    def __init__(self):
        BaseComponent.__init__(
            self, By.XPATH,
            f"//div[contains(@class,'knowledge-styles') and contains(@class,'accordion')]"
        )
        self.__manual_faq_section = SectionWithGroupFAQ('Manual Q&A')

    base_locator = "//div[contains(@class,'knowledge-styles') and contains(@class,'accordion')"
    lbl_knowledge_title = ".//div[contains(@class,'title')]"
    btn_add_pair = ".//*[@id='add-pair-button-1']"

    base_elements = ElementFactory.get_list_element(by=By.XPATH,
                                                    locator=base_locator,
                                                    element_cls=BaseElement)

    @staticmethod
    def get_table_component_with_group_faqs(section_name):
        return SectionWithGroupFAQ(section_name)

    @staticmethod
    def get_table_component_with_pair_faqs(section_name):
        return SectionWithPairFAQ(section_name)

    @staticmethod
    def get_number_of_section():
        return len(FaqKnowledgeDataTableComponent.base_elements)

    def get_list_knowledge_title(self) -> List['str']:
        list_knowledge_title = []
        for element in self.get_list_child_element_in_list_component(
                BaseElement, By.XPATH, self.lbl_knowledge_title):
            list_knowledge_title.append(element.get_attribute("innerText"))
        return list_knowledge_title

    def input_new_manual_question_pair(self, question: str, answer: str):
        self.__manual_faq_section.add_new_group(question, answer)

    def modify_question_pair_at_index(self, index, question, answer):
        # TODO(namndoan): Update this when question field become editable
        pass

    def click_on_data_table_with_faq_url(self, faq_url):
        section_for_faq_url = SectionWithGroupFAQ(faq_url)
        section_for_faq_url.open_section()

    def get_all_question_pair_data_in_table(self):
        self.__manual_faq_section.wait_for_component_visible()
        return self.__manual_faq_section.get_all_manual_faq()

    def get_all_faq_data_in_table_with_faq_url(self, faq_url):
        section_for_faq_url = SectionWithGroupFAQ(faq_url)
        section_for_faq_url.wait_for_component_visible()
        # Open the section if it is not active
        if not section_for_faq_url.is_active_section():
            section_for_faq_url.open_section()
        return section_for_faq_url.get_all_manual_faq()

    def wait_for_manual_faq_section_visible(self):
        return self.__manual_faq_section.wait_for_component_visible()

    def get_number_of_manual_pair(self) -> str:
        return self.__manual_faq_section.get_section_info()["length"]
Ejemplo n.º 14
0
class SectionWithGroupFAQ(BaseComponent):
    def __init__(self, section_name):
        BaseComponent.__init__(
            self, By.XPATH,
            f"//div[contains(@class,'knowledge-styles') and contains(@class,'accordion')"
            f" and .//*[normalize-space(text())='{section_name}']]")

    base_locator = "//div[contains(@class,'knowledge-styles') and contains(@class,'accordion')]"
    lbl_knowledge_title = ".//div[contains(@class,'title')]"  # /div
    btn_add_pair = ".//*[contains(@id,'add-pair-button')]"
    input_question_of_new_group = ".//div[@placeholder='Type a question']"
    input_answer_of_new_group = "//div[@placeholder='Type a question']/" \
                                "ancestor::tr//div[@placeholder='Type an answer']"
    tfoot_tag_name = "tfoot"
    div_header = ".//div[contains(@class,'title')]"
    row_contains_answer = ".//tr//div[@placeholder='Type an answer' and .='{0}']/ancestor::tr"
    row_contains_question = ".//tr/td[1]//span[.='{0}']/ancestor::tr"

    base_elements = ElementFactory.get_list_element(by=By.XPATH,
                                                    locator=base_locator,
                                                    element_cls=BaseElement)

    def get_section_info(self):
        lbl_title = self.get_list_child_element(BaseElement, By.XPATH,
                                                self.lbl_knowledge_title)

        result = {"title": lbl_title[0].text, "length": lbl_title[1].text}
        return result

    def is_active_section(self):
        title = self.get_child_element(BaseElement, By.XPATH,
                                       ".//div[contains(@class,'title')]")
        class_name = title.get_attribute('class')
        return ' active ' in f" {class_name} "

    def open_section(self):
        self.base_component_element(BaseElement).click()

    def collapse_section(self):
        self.get_child_element(BaseElement, By.XPATH, self.div_header).click()

    def add_new_group(self, first_question, answer):
        try:
            self.get_child_element(BaseElement, By.XPATH,
                                   self.btn_add_pair).click()
        except NoSuchElementException:
            pass
        except ElementNotVisibleException:
            pass
        self.get_child_element(
            BaseElement, By.XPATH,
            self.input_question_of_new_group).send_keys(first_question)
        self.get_child_element(
            BaseElement, By.XPATH,
            self.input_answer_of_new_group).send_keys(answer)
        self.get_child_element(BaseElement, By.TAG_NAME,
                               self.tfoot_tag_name).click()
        # time.sleep(1)

    def add_similar_question(self, answer, similar_question):
        row = self.find_row_element(answer=answer)
        btn_locator = ".//*[contains(@id,'add-similar-question')]/preceding-sibling::span"
        input_locator = ".//div[@placeholder='Type similar question']"
        # Find button add similar question
        btn_add_similar_question = self.get_child_element_if_exist(
            locator_type=By.XPATH,
            locator_value=btn_locator,
            parent_element=row)
        # Click on button add similar question if it exist
        (btn_add_similar_question
         is not None) and btn_add_similar_question.click()
        # Enter similar question
        row.find_element_by_xpath(input_locator).send_keys(similar_question)
        row.find_element_by_xpath(input_locator).send_keys(Keys.ENTER)

    def get_all_manual_faq(self) -> [dict]:
        result = []
        for row in self.find_all_row_element():
            questions = self.__get_all_similar_question_in_row(row)
            if len(questions) > 0:
                answer = row.find_element_by_xpath(
                    ".//div[@placeholder='Type an answer']").text
                result.append({"questions": questions, "answer": answer})
        return result

    def get_list_similar_question(self, answer):
        row = self.find_row_element(answer=answer)
        return self.__get_all_similar_question_in_row(row)

    def __get_all_similar_question_in_row(self, row: WebElement):
        result = []
        locator = "./td[1]/div[1]//span[@draggable]"
        similar_questions = row.find_elements_by_xpath(locator)
        for question in similar_questions:
            result.append(question.text)
        return result

    def find_row_element(self, answer=None, question=None) -> WebElement:
        if answer is not None:
            return self.get_child_element(
                BaseElement, By.XPATH, self.row_contains_answer.format(answer))
        elif question is not None:
            return self.get_child_element(
                BaseElement, By.XPATH,
                self.row_contains_question.format(question))
        else:
            raise Exception("Missing both answer and question")

    def find_all_row_element(self) -> List[WebElement]:
        return self.get_list_child_element(BaseElement, By.XPATH,
                                           ".//tbody//tr")
Ejemplo n.º 15
0
class ClientSimulator(BaseComponent):
    txt_feedback_title_locator = "//*[contains(@class, 'feedbackContainer')]/div[contains(@class, 'title')]"

    ico_open_bot = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//div[contains(@class, 'minimizedBoxContainer')]",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    chat_box = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//div[contains(@class, 'chatboxContainer')]",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    ico_close_bot = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//div[contains(@class, 'chatboxContainer')]//div[contains(@class, 'minimizeIcon')]",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    txb_message = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//div[contains(@class, 'chatboxContainer')]//textarea",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    list_txt_response = ElementFactory.get_list_element(
        by=By.XPATH,
        locator="//div[contains(@class,'messageContainerLeft')]//div[contains(@class,'messageTextLeft')]",
        element_cls=BaseElement
    )

    ifr_sample_app_chatbox = ElementFactory.get_element_until(
        by=By.CSS_SELECTOR,
        locator="#sample_app-minimized-box-frame",
        element_cls=BaseElement,
        wait_type=EC.visibility_of_element_located
    )

    ifr_chat_box = ElementFactory.get_element_until(
        by=By.ID, locator="sample_app-chatbox-frame",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    txt_feedback_title = ElementFactory.get_element_until(
        by=By.XPATH, locator=txt_feedback_title_locator,
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    list_feedback_icon = ElementFactory.get_list_element(
        by=By.XPATH,
        locator="//*[contains(@class, 'feedbackContainer')]/div[contains(@class, 'ratingIconsContainer')]/*",
        element_cls=BaseElement
    )

    txt_latest_response = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="(//*[contains(@class, 'styles__messageTextRight')])[last()]"
                "/../../../following-sibling::div[1]//*[contains(@class, 'styles__messageTextLeft')]",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    btn_send_msg = ElementFactory.get_element_until(
        by=By.XPATH,
        locator="//div[contains(@class, 'chatboxContainer')]//textarea/../../following-sibling::button",
        wait_type=EC.visibility_of_element_located,
        element_cls=BaseElement
    )

    def __init__(self):
        BaseComponent.__init__(self, By.ID, "sample_app-chatbox")

    def open_chat_box(self):
        self.click_on_chatbot_icon()

    def close_chat_box(self):
        self.click_on_chatbot_icon()

    def click_on_chatbot_icon(self):
        self.driver.core_driver.switch_to.frame(self.ifr_sample_app_chatbox.get_wrapper_element)
        self.ico_open_bot.click()
        self.driver.core_driver.switch_to.parent_frame()

    def is_open(self):
        try:
            is_open = self.ifr_chat_box.is_displayed()
        except TimeoutException:
            is_open = False
        return is_open

    def send_message(self, message):
        if not self.is_open():
            self.open_chat_box()
        self._switch_to_chat_frame()
        self.txb_message.send_keys(message)
        self.btn_send_msg.click()
        self._switch_to_parent_frame()

    def get_responses(self):
        self._switch_to_chat_frame()
        responses = list(map(lambda elem: elem.get_element_attribute("innerText"), self.list_txt_response))
        self._switch_to_parent_frame()
        return responses

    def create_stub_messages(self, message, quantity=1):
        self.open_chat_box()
        self._switch_to_chat_frame()
        for i in range(quantity):
            self.txb_message.send_keys(message + " " + str(i))
            self.btn_send_msg.click()
            self.wait_for_response()
        self._switch_to_parent_frame()

    def wait_for_feedback_returned(self):
        self._switch_to_chat_frame()
        try:
            is_visible = self.txt_feedback_title.is_displayed()
        except TimeoutException:
            is_visible = False
        self._switch_to_parent_frame()
        return is_visible

    def wait_for_response(self):
        # Trigger wait method after each message is sent by calling the element
        return self.txt_latest_response

    def get_feedback_title(self):
        self._switch_to_chat_frame()
        text = self.txt_feedback_title.get_element_text()
        self._switch_to_parent_frame()
        return text

    def get_list_feedback_text(self):
        feedback_text = []
        self._switch_to_chat_frame()
        for element in self.list_feedback_icon:
            action = ActionChains(self.driver.core_driver).move_to_element(element.get_wrapper_element)
            action.perform()
            # Relocated element due to change in DOM
            ele = self.driver.core_driver.find_element_by_xpath(self.txt_feedback_title_locator)
            feedback_text.append(ele.text)
        self._switch_to_parent_frame()
        return feedback_text

    def _switch_to_chat_frame(self):
        self.driver.core_driver.switch_to.frame(self.ifr_chat_box.get_wrapper_element)

    def _switch_to_parent_frame(self):
        self.driver.core_driver.switch_to.parent_frame()

    def open_wait_close(self):
        pass

    def get_bot_avatar_source(self):
        self._switch_to_chat_frame()
        result = self.driver.core_driver.find_element_by_xpath("//img").get_attribute("src")
        self._switch_to_parent_frame()
        return result