0

I am new to automated browser testing using selenium webdriver (Python 3). The following method I am already using for testing and taking screenshots:

from selenium import webdriver
from selenium.webdriver.support.events import EventFiringWebDriver
from selenium.webdriver.support.events import AbstractEventListener
import unittest

class ScreenshotListener(AbstractEventListener):
    def on_exception(self, exception, driver):
        driver.get_screenshot_as_file("C:/Error.png")

class Test1_Chrome(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()                    # Set chrome browser
        self.driver1 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port(self):
        driver = self.driver1
        driver.get("http://")              # Opens url into chrome
        driver.get_screenshot_as_file("C:/Chrome_screenshots/Screenshot1.png")
        driver.find_element_by_css_selector("a[name*='']").click()
        driver.get_screenshot_as_file("C:/Chrome_screenshots/Screenshot2.png")
        driver.find_element_by_css_selector("b[name*='']").click()
        driver.get_screenshot_as_file("C:/Chrome_screenshots/Screenshot3.png")
        driver.find_element_by_css_selector("a[href*='']").click()
        driver.get_screenshot_as_file("C:/Chrome_screenshots/Screenshot4.png")

    def tearDown(self):
        time.sleep(2)
        driver = self.driver1
        driver.close()

class Test2_IE(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie()                    # Set IE browser
        self.driver2 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port(self):
        driver = self.driver2
        driver.get("http://")              # Opens url into IE
        driver.get_screenshot_as_file("C:/IE_screenshots/Screenshot1.png")
        driver.find_element_by_css_selector("a[name*='']").click()
        driver.get_screenshot_as_file("C:/IE_screenshots/Screenshot2.png")
        driver.find_element_by_css_selector("b[name*='']").click()
        driver.get_screenshot_as_file("C:/IE_screenshots/Screenshot3.png")
        driver.find_element_by_css_selector("c[href*='']").click()
        driver.get_screenshot_as_file("C:/IE_screenshots/Screenshot4.png")

    def tearDown(self):
        time.sleep(2)
        driver = self.driver2
        driver.close()

class Test3_Firefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()                    # Set Firefox browser
        self.driver3 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port(self):
        driver = self.driver3
        driver.get("http://")              # Opens url into FireFox
        driver.get_screenshot_as_file("C:/FF_screenshots/Screenshot1.png")
        driver.find_element_by_css_selector("a[name*='']").click()
        driver.get_screenshot_as_file("C:/FF_screenshots/Screenshot2.png")
        driver.find_element_by_css_selector("b[name*='']").click()
        driver.get_screenshot_as_file("C:/FF_screenshots/Screenshot3.png")
        driver.find_element_by_css_selector("c[href*='']").click()
        driver.get_screenshot_as_file("C:/FF_screenshots/Screenshot4.png")

    def tearDown(self):
        time.sleep(2)
        driver = self.driver3
        driver.close()

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

Question: I want to create some loop or function for these screeenshots in such a way that : Saves all three browser element screenshots in a different folder with all different names. Example: ChromeSreenshots- Screenshot1, screenshot2, etc; IESreenshots- Screenshot1, screenshot2, etc; FirefoxSreenshots- Screenshot1, screenshot2, etc. How can I create such a function which I call into same or different python file?

The code I used already has "test_port(self)" function which is common for all three browsers except the screenshots(location and name). I want to use "test_port(self)" function by calling into all three browser tests but the problem is I want the screenshots to be in different locations and different names. I searched on web but did not found something good to solve this problem.

D.j
  • 5
  • 7
  • Please post the code you have tried and why it didn't meet your requirements. There are plenty of examples like this on the web that you can pull from and put together at least an attempt at this. Once you do that, if it still doesn't work... post the code here and update the question. – JeffC Oct 14 '16 at 15:00
  • Hello! I updated the code! – D.j Oct 17 '16 at 11:44

2 Answers2

0

I look at your code and all three classes, Test1-3, are the same except for the browser instance. What that tells me is that you should instantiate the WebDriver instance before the test and pass it into the class. Once inside the class, you can detect what instance of WebDriver you are using (using .isinstance(), see this question) and then you can create a subfolder for Chrome vs Firefox vs IE, etc. and put all the screenshots in there. When you are done, you should have a folder for each browser that contains 4 screenshots, e.g. c:\chrome\ss1.png, c:\chrome\ss2.png, c:\ff\ss1.png, c:\ff\ss2.png, c:\ie\ss1.png, c:\ie\ss2.png, and so on.


NOTE: python is not a language I program in so this code may not be optimal. Some sample code

def test_port(self):
    driver = self.driver1
    driver.get("http://")              # Opens url
    path = get_screenshot_path()
    driver.get_screenshot_as_file(path + "Screenshot1.png")
    driver.find_element_by_css_selector("a[name*='']").click()
    driver.get_screenshot_as_file(path + "Screenshot2.png")
    driver.find_element_by_css_selector("b[name*='']").click()
    driver.get_screenshot_as_file(path + "Screenshot3.png")
    driver.find_element_by_css_selector("a[href*='']").click()
    driver.get_screenshot_as_file(path + "Screenshot4.png")

def get_screenshot_path(self)
    path_root = "c:\"
    if isinstance(self.driver, webdriver.Chrome())
        # the driver is a instance of the Chrome driver
        path = path_root + "chrome\"
    if isinstance(self.driver, webdriver.Firefox())
        # the driver is a instance of the Firefox driver
        path = path_root + "firefox\"
    # create the subfolder if it doesn't exist
    os.makedirs(path, exist_ok=True)
    return path
Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thank you, I checked the link but I am still not clear about how to use .isinstance() in my case (may because I am new to Python also). Could you please give some more example by code or something that gives me a more clear picture. You mentioned "put all the screenshots in the created folder", my question is do we put it manually? – D.j Oct 17 '16 at 15:01
  • I updated my answer with some sample code that I cobbled together from a few examples. Hopefully it will work and point you in the right direction. – JeffC Oct 17 '16 at 16:19
  • Thank you! I tried it but the problem now is by using isinstance() the driver was not closing on teardown(). – D.j Oct 27 '16 at 12:03
0

By using isinstance() (answer from JeffC), the driver was not closing on teardown(). Therefore I found some simpler solution. Does anyone know some method that makes this code look shorter example : setup and teardown functions are repeating. I saw other questions which says about desiredCapabities. I really don't know how it works in my case and with python.

import..

class ScreenshotListener(AbstractEventListener):
    def on_exception(self, exception, driver):
        driver.get_screenshot_as_file("C:/Error.png")

class Test1_Chrome(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()                    # Set chrome browser
        self.driver1 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port1(self):
        driver = self.driver1
        driver.get("http://")              # Opens url into chrome
        global directory
        directory = 'C:/Chrome_screenshots/'
        test_port(self)

    def tearDown(self):
        time.sleep(2)
        driver = self.driver1
        driver.close()

class Test2_IE(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie()                    # Set IE browser
        self.driver2 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port2(self):
        driver = self.driver2
        driver.get("http://")              # Opens url into IE
        global directory
        directory = 'C:/IE_screenshots/'
        test_port(self)

    def tearDown(self):
        time.sleep(2)
        driver = self.driver2
        driver.close()

class Test3_Firefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()                    # Set Firefox browser
        self.driver3 = EventFiringWebDriver(self.driver, ScreenshotListener())
        self.driver.maximize_window()                       # Maximize window
        time.sleep(0.30)                                    # Wait 30 seconds

    def test_port3(self):
        driver = self.driver3
        driver.get("http://")              # Opens url into FireFox
        global directory
        directory = 'C:/Firefox_screenshots/'
        test_port(self)

    def tearDown(self):
        time.sleep(2)
        driver = self.driver3
        driver.close()

def test_port()
    driver.get_screenshot_as_file( directory + "Screenshot1.png")
    driver.find_element_by_css_selector("a[name*='']").click()
    driver.get_screenshot_as_file( directory + "Screenshot2.png")
    driver.find_element_by_css_selector("b[name*='']").click()
    driver.get_screenshot_as_file("directory + Screenshot3.png")
    driver.find_element_by_css_selector("c[href*='']").click()
    driver.get_screenshot_as_file( directory + "Screenshot4.png")


if __name__ == '__main__':
    unittest.main()
Community
  • 1
  • 1
D.j
  • 5
  • 7