问答 selenium 自动化测试:为什么 manage_page.click_system_menu() 放在 test_add_user() 中没办法定位到该按钮,但是放在 test_login_user() 中就可以定位到

picture111 · 2024年05月27日 · 最后由 加菲Danny 回复于 2024年05月31日 · 5039 次阅读

class TestLanqiaoSystem(unittest.TestCase):
@classmethod
def setUpClass(cls):
gecko_path="C:\LanQiaoTest\project\PythonLanqiaoTest\driver\geckodriver.exe"
service=Service(gecko_path)
options=webdriver.FirefoxOptions()
options.set_preference('security.fileuri.strict_origin_policy', False)
options.set_preference('security.fileuri.origin_policy', '*')
cls.driver=webdriver.Firefox(service=service,options=options)
cls.driver.implicitly_wait(5)
cls.driver.maximize_window()
cls.driver.get("https://66546ef7debba6d0473e7e85.hz-iframe-svc.simplelab.cn/login")

@classmethod
def tearDownClass(cls):
cls.driver.quit()

def test_login_user(self):
login_page=LoginPage(self.driver)
login_page.input_username("admin")
login_page.input_password("admin123")
login_page.input_code("666")
login_page.click_auto_login()
login_page.click_login()
duanyan1=login_page.get_username_text()
self.assertEqual(duanyan1,"蓝桥超管")
sleep(2)

def test_person_center(self):
manage_page=UserManagePage(self.driver)
manage_page.click_person_center()
self.driver.switch_to.frame(1)
manage_page.click_modify_photo()
self.driver.switch_to.default_content()
self.driver.switch_to.frame(2)
manage_page.click_upload_photo("D:\photopc\2.jpg")
self.driver.switch_to.default_content()
manage_page.click_upload_okk()
sleep(4)

def test_add_user(self):
manage_page=UserManagePage(self.driver)
add_page=AddPage(self.driver)
manage_page.click_system_menu()
manage_page.click_user_menu()
self.driver.switch_to.frame("iframe2")
add_page.click_add_button()
self.driver.switch_to.default_content()
self.driver.switch_to.frame(2)
add_page.input_username("测试程序员")
add_page.input_telephone("13788888888")
add_page.input_email("123456@qq.com")
add_page.input_logincount("test")
add_page.input_loginpwd("admin123")
add_page.click_user_sex()
add_page.click_user_state()
add_page.click_job()
add_page.click_add_okk()
self.driver.switch_to.default_content()
self.driver.switch_to.frame("iframe2")
sleep(2)
duanyan2=manage_page.get_page_num()
self.assertEqual(duanyan2,"显示第 1 到第 3 条记录,总共 3 条记录")
sleep(2)

def test_manage_user(self):
manage_page=UserManagePage(self.driver)
manage_page.input_search_name("test")
manage_page.click_user_state("停用")
manage_page.click_starttime()
manage_page.click_endtime()
manage_page.click_search()
sleep(2)
duanyan3=manage_page.get_page_num()
self.assertEqual(duanyan3,"显示第 1 到第 1 条记录,总共 1 条记录")
manage_page.click_reset()
manage_page.click_ceshibumen()
sleep(1)
duanyan4=manage_page.get_kong_text()
self.assertEqual(duanyan4,"没有找到匹配的记录")
manage_page.click_reset()
manage_page.click_box()
manage_page.click_delete_button()
manage_page.click_delete_okk()
self.driver.switch_to.default_content()
sleep(2)
duanyan5=manage_page.get_page_num()
self.assertEqual(duanyan5,'显示第 1 到第 2 条记录,总共 2 条记录 ')
sleep(2)

if name=='main':
suit=unittest.TestSuite()
suit.addTest(TestLanqiaoSystem('test_login_user'))
suit.addTest(TestLanqiaoSystem('test_person_center'))
suit.addTest(TestLanqiaoSystem('test_add_user'))
suit.addTest(TestLanqiaoSystem('test_manage_user'))
runner=unittest.TextTestRunner()
runner.run(suit)

共收到 2 条回复 时间 点赞

你调用的 click_system_menu(),在 LoginPage,UserManagePage 这两个页面的元素都是一样的吗?


可以看到两个页面并不一致
首先可以考虑两个页面的 button 属性是否一致
在一个就是看一下代码逻辑是否有问题或者等待时间的问题

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册