我有一张带有员工照片的主页。如果我点击照片,它会将我重定向到员工个人资料页面。 如何使用页面对象模型,selenium Web驱动程序/ testNg。
自动执行此操作答案 0 :(得分:0)
我认为您所寻找的是类似文件“home_page.py”
class HomePage(object):
def __init__(self, driver):
self.driver = driver
self.url = "http://www.homepage.com/"
self.employee_photo - = "//a[@class='employee_photo']/img"
def goto_homepage(self):
self.driver.get(self.url)
def get_employee_photo(self):
return self.driver.find_element_by_xpath(self.employee_photo)
def click_on_employee_photo(self):
photo = self.get_employee_photo()
photo.click()
在您的测试文件中:
from home_page import HomePage
from selenium import webdriver
class TestSuite(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_clicking_employee_photo_redirects(self):
home_page = HomePage(self.driver)
home_page.goto_homepage()
employee_photo_url = home_page.get_employee_photo().get_attribute('href')
home_page.click_on_employee_photo()
redirect_page_url = self.driver.current_url
self.assertEqual(redirect_page_url, employee_photo_url)
这就是基础知识。如果我正确理解你在寻找什么。