Fixed storage paths for Selenium screenshots or logs possible?



  • Details:

    Currently I use Python 3.10 and Selenium. I used here in the area of functional testing here in particular UI Testing accordingly also Selenium screenshots. But since I work on different computers, the paths to the folder change accordingly.

        # -*- coding: utf-8 -*-
        import unittest
    
    import HtmlTestRunner
    from selenium import webdriver
    from selenium.common.exceptions import NoAlertPresentException
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    # logging.basicConfig(filename = file_name, level = logging.DEBUG)
    
    ### Browser Testing Checklist ###
    
    
    '''
    Browser Testing Checklist   
    
    Project name: | <required> |
    Project manager name: | <required> |
    Project description:  | <required> |
    
    Browser Test Plan   
    
    Which browser versions are being used by your target audience?  
    Can you ensure the availability of all those browser versions?  
    Which devices / hardware is needed for conducting those browser tests?  
    
    Content yes/no
    Are all images available across browsers?   
    Are used fonts working for every browser version?   
    Are all audio files available for every browser?    
    Can the content be consumed on every browser version?   
    
    Layout  yes/no
    Is the layout consistent?   
    Are all layout element behaving as expected?    
    Is the (responsive) layout working in all browser versions? 
    Are animations and effects running smoothly and as expected?    
    
    Functionality   yes/no
    Are all features useable in the targeted browser versions?  
    Are all available integrations and 3rd party services running as expected?  
    Are all forms and other dynamic interactions working across browsers?   
    "Are all interactions working equally good with touch input 
    (e.g. from a mobile device) and mouse input (e.g. from a desktop computer)?"
    
    Information: https://docs.google.com/spreadsheets/d/1cVWP19y7YKtGlMKgWbPkTdzmMJICLTtFb_g5UrC-dMA/edit#gid=0 
    
    '''
    
    '''Define class'''
    
    
    class OpenDevcrowd(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
            self.driver.implicitly_wait(30)
            self.base_url = "https://www.google.com/"
            self.verificationErrors = []
            self.accept_next_alert = True
    
    
        ### Start Testing here:###
        # what is testing # | window size | target |
        # tomyside Page     |  1920, 994  |  https://tomyside.com/      |
    
        '''Start Test Mainpage here '''
    
        def test_tomyside_mainpage(self):
            # Test name: open_tomyside_mainpage
            # Step # | name | target | value
            # 1 | open | / |
            self.driver.get("https://www.tomyside.com/")
            # 2 | setWindowSize | 1920x994 |
            self.driver.set_window_size(1920, 994)
            # 3 | wait | wait 30 |
            self.driver.implicitly_wait(30)
            # 4 | wait | wait 30 |
            self.driver.save_screenshot("/home/myspaceonlinux/PycharmProjects/Hybrid_Testframework/Framework/Media/Screenshots/Mainpage/mainpage.png")
            # 5 | close | Firefox |
            self.driver.close()
    
        '''
        Setup: This part is for Assertion setup, logging and more 
        '''
    
        def is_element_present(self, how, what):
            try:
                self.driver.find_element(by=how, value=what)
            except NoSuchElementException as e:
                return False
            return True
    
        def is_alert_present(self):
            try:
                self.driver.switch_to_alert()
            except NoAlertPresentException as e:
                return False
            return True
    
        def close_alert_and_get_its_text(self):
            try:
                alert = self.driver.switch_to_alert()
                alert_text = alert.text
                if self.accept_next_alert:
                    alert.accept()
                else:
                    alert.dismiss()
                return alert_text
            finally:
                self.accept_next_alert = True
    
        def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)
    

    if name == 'main':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(
    output='/home/tomyspace/PycharmProjects/Hybrid_Testframework/Framework/Media/TestRun_Logs'))

    Questions:

    Is there a possibility to set the paths in the structure so that they can still be executed on different computers without constantly adjusting the path?



  • May be you can do something like, get the current working directory and then combine it with your folder/filename to dynamically store your screenshots and logs in a specific folder relative to your project directory.

    I've used something like this in Java,

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
    System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
    File file = new File(System.getProperty("user.dir") + "//data/report-" + currentDate + ".csv");
    String logFile = "./Target/Result-" + currentDate + ".jtl";
    

    You can figure out something on the same line in Python with,

    driver.save_screenshot('./project_directory/save_screenshot.png') for storing in your current project/code directory.

    Note: I haven't worked with Python personally. But, you should be able to get the current directory and combine it as a string to the filename and parse it to save your screenshot/logs.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2