Selenium web driver :Click on photo and page rediret

95 Views Asked by At

I have a home page with employee photo. If i click on the photo , it redirects me to employee profile page. How to automate this using page object model , selenium Web driver/testNg.

1

There are 1 best solutions below

0
On

I think what your looking for is something like this a file "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()

In your test file:

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)

So that is the basics. If I understand correctly what you are looking for.