随笔:python+selenium进行UI自动化窗口跳转实例

#coding = utf-8


import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains


class Truelogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("***开始进行登录***")

    def setUp(self):
        # 屏蔽自动化受控提示 && 开发者提示
        self.option = webdriver.ChromeOptions()
        self.option.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
        # 屏蔽'保存密码'提示框
        self.prefs = {}
        self.prefs["credentials_enable_service"] = False
        self.prefs["profile.password_manager_enabled"] = False
        self.option.add_experimental_option("prefs", self.prefs)
        # 打开谷歌浏览器
        self.driver = webdriver.Chrome(options=self.option)
        # 窗口最大化
        self.driver.maximize_window()
        sleep(5)

        #打开登录界面
        self.driver.get("ur")
        sleep(5)
    def testcloudAIlogin(self):
        self.driver.switch_to.frame(0)
        self.driver.find_element_by_xpath("//input[@id='username']").send_keys("用户名")
        self.driver.find_element_by_xpath("//input[@id='password']").send_keys("密码*")
        self.driver.find_element_by_xpath("//div[@class='button']/a[@id='loginbtn']").click()
        sleep(5)
        # 获取当前窗口的句柄
        self.window1 = self.driver.current_window_handle
        #搜索框输入系统名称
        self.driver.find_element_by_css_selector("input#search-keyword").send_keys("名称")
        sleep(2)
        ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//a/span[text()='名称']")).perform()
        self.driver.find_element_by_xpath("//a/span[text()='名称']").click()
        sleep(10)

        # 获取所有窗口的句柄
        self.all_handles = self.driver.window_handles
        sleep(5)
        #切换到新窗口
        for self.handle in self.all_handles:
            if self.handle != self.window1:
                self.driver._switch_to.window(self.handle)
                print("***这是新窗口***")
        sleep(2)
        self.assertEqual("用户名",self.driver.find_element_by_xpath("//div[text()='用户名']").text)
        sleep(2)
        print("***登录完成***")

    def tearDown(self):
        self.driver.quit()
        print("***测试用例结束***")


    @classmethod
    def tearDownClass(cls):
         print("***一次测试结束***")

    if __name__ == '__main__':
        unittest.main()


你可能感兴趣的:(随笔)