class TestLoginDDT_001:

    baseURL=ReadConfig.get_base_URL()
    logger=LogGen.log_gen()
    Datapath="./testdata/TestDataManagerLogin.xlsx"

    def test_login_ddt(self,setup):
        self.logger.info("****** TestLoginDDT_001 started ******")
        self.logger.info("****** verifying login ddt test ******")
        self.driver=setup
        self.driver.get(self.baseURL)

        self.lp=LoginPage(self.driver)
        self.rows=XLUties.get_row_count(self.Datapath,"Sheet1")
        print("Number of rows in Excel Data ",self.rows)

        lst_status=[]
        for r in range(2,self.rows+1):
            self.username=XLUties.read_data(self.Datapath,"Sheet1",r,1)
            self.password=XLUties.read_data(self.Datapath,"Sheet1",r,2)
            self.exp=XLUties.read_data(self.Datapath, "Sheet1",r,3)

            self.lp.set_user_id(self.username)
            self.lp.set_password(self.password)
            self.lp.click_on_login()
            actual_title=self.driver.title
            expected_title="Guru99 Bank Manager HomePage"
            if actual_title==expected_title:
                if self.exp=="Pass":
                    self.logger.info("------Passed------")
                    self.lp.click_on_logout()
                    self.driver.switch_to.alert.accept()
                    lst_status.append("Pass")
                elif self.exp=="Fail":
                    self.logger.info("------Failed------")
                    self.lp.click_on_logout()
                    time.sleep(5)
                    self.driver.switch_to.alertaccept()
                    lst_status.append("Fail")
            elif actual_title!=expected_title:
                if self.exp=="Pass":
                    self.logger.info("------Failed------")
                    lst_status.append("Fail")
                elif self.exp=="Fail":
                    self.logger.info("------Passed------")
                    lst_status.append("Pass")
        if "Fail" not in lst_status:
            self.logger.info("****** TestLoginDDT_001 Passed ******")
            self.driver.close()
            assert True
        else:
            self.logger.info("****** TestLoginDDT_001 Failed ******")
            self.driver.close()
            assert False
        self.logger.info("****** End of login DDT test ******")
        self.logger.info("**************** complete test login DDT ********************")
class Test_Login_001:

    base_url = ReadConfig.get_base_URL()
    user_name = ReadConfig.get_user_name()
    password = ReadConfig.get_password()
    log = LogGen.log_gen()

    @pytest.mark.smoke
    def test_open_browser(self, setup):
        self.log.info("------Open browser test------")
        self.driver = setup
        self.driver.get(self.base_url)
        actual_url = self.driver.current_url
        if actual_url == "http://demo.guru99.com/V4/":
            assert True
            self.log.info("------ open browser test passed ------")
            self.driver.close()
        else:
            self.driver.save_screenshot("../screenshots/" +
                                        'test_homepage_title.png')
            self.driver.close()
            self.log.error("------ open browser test failed ------")
            assert False

    @pytest.mark.smoke
    def test_home_page_title(self, setup):
        self.log.info("------ test login 001 -------")
        self.log.info("------ verifying homepage title ------")
        self.driver = setup
        self.driver.get(self.base_url)
        time.sleep(5)
        actual_title = self.driver.title
        if actual_title == "Guru99 Bank Home Page":
            assert True
            self.driver.close()
            self.log.info("------ home page title test is passed ------")
        else:
            self.driver.save_screenshot("../screenshots/" +
                                        'test_homepage_title.png')
            self.driver.close()
            self.log.error("------ home page title test is failed ------")
            assert False

    @pytest.mark.sanity
    @pytest.mark.regression
    def test_login(self, setup):
        self.log.info("------ verifyin login test ------")
        self.driver = setup
        self.driver.get(self.base_url)
        self.log.info("--- application launched...")
        self.lp = LoginPage(self.driver)
        self.lp.set_user_id(self.user_name)
        self.log.info("--- user id Entered : " + self.user_name)
        self.lp.set_password(self.password)
        self.log.info("--- password Entered : " + self.password)
        self.lp.click_on_login()
        self.log.info("--- clicked on login")
        self.msg = self.driver.find_element_by_xpath("//body").text

        #if self.msg == "Manger Id : mngr285385":
        if "Manger Id :" + " " + self.user_name in self.msg:
            assert True
            self.log.info("------ login test is passed ------")
            self.driver.close()
        else:
            self.driver.save_screenshot(".\\screenshots\\" + "test_login.png")
            self.driver.close()
            self.log.error("------ login test is failed")
            assert False