0

I've been working on Cross-Browser-Testing with my team for a while now and I've sorted out my Chrome tests.

So today I decided to try running my teammate's Edge tests and I'm getting the ResourceWarning: Enable tracemalloc to get the object allocation traceback message like crazy. About every second or so it prints out 1 to 3 of the same message.

I remember getting this message occassionaly while running Chrome tests during the beginning, but never to the extreme amount I got from Edge.

I've done some minor research (1, 2, 3) so I know this doesn't affect my Selenium runs.

But I'm curious as to what behind-the-scenes things are happening to cause this more on Edge than Chrome.

For specific information about my Edge Driver, I'm using the one from the Microsoft site for Edge version 80.0.361.66. I did do some config testing to see if that made any difference (driver version and edge version) but still the same amount.

This is especially weird because Edge uses Chromium. I'm wondering if it's an issue with how Selenium controls Edge.

Here's a sample Minimum Viable Code for Chrome

import time
import unittest
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class test_state(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.driver = Chrome('__tests__/drivers/chromedriver.exe')
        self.driver.maximize_window()
        self.driver.get("https://adbanker.com/")

    def test_state(self):
        try:
            url = self.driver.current_url
            print(url)
            time.sleep(5)
            title = self.driver.title
            print(title)
            WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="for"]')))
            lr = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.ID,'statenav')))
            lr.click()
            ce = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="line_dropdown"]/option[3]')))
            ce.click()
            ca = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="state_dropdown"]/option[6]')))
            ca.click()
            time.sleep(5)
        except:
            raise


    @classmethod
    def tearDown(self):
        self.driver.quit()

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

And here's a sample Minimum Viable Code for Edge

import time
import unittest
from selenium.webdriver import Edge
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class test_state(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.driver = Edge('__tests__/drivers/msedgedriver.exe')
        self.driver.maximize_window()
        self.driver.get("https://adbanker.com/")

    def test_state(self):
        try:
            url = self.driver.current_url
            print(url)
            time.sleep(5)
            title = self.driver.title
            print(title)
            WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="for"]')))
            lr = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.ID,'statenav')))
            lr.click()
            ce = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="line_dropdown"]/option[3]')))
            ce.click()
            ca = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="state_dropdown"]/option[6]')))
            ca.click()
            time.sleep(5)
        except:
            raise


    @classmethod
    def tearDown(self):
        self.driver.quit()

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

I've been doing more research on this, and specifically this error is pointing to line 374 of this file in Selenium's remote driver.

Upon closer inspection, it's hitting an if condition on line 374 and just doesn't know what to do, so it fails. But why?

due_rest
  • 31
  • 3
  • Don't know the complete answer, but "malloc" in c was grabbing memory allocations of a certain size. So trace malloc maybe returns the addresses of those memory allocations? Are you using original Edge or Edgium? Edgium (Still named Edge) is edge but it's using the chromium engine like google chrome now. – Monofuse May 08 '20 at 20:54
  • I'm using Edgium. I forgot to tag this as Python, but you have the gist of what trace malloc does. Its used for determining where memory is assigned for objects. When it's imported you can use it to look for memory leaks and compare snapshots of memory at certain points. – due_rest May 08 '20 at 21:32
  • Can you post the Enough code to reproduce the problem as in [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? Perhaps you are using tracemalloc model to trace memory blocks allocated by Python, or as [this link](https://stackoverflow.com/questions/21057942/python-unclosed-resource-is-it-safe-to-delete-the-file) said, you are forgot to close the file. – Zhi Lv May 11 '20 at 02:12
  • @ZhiLv-MSFT Added as requested. Even in this simple example I'm getting significant ResourceWarnings. Its weird. – due_rest May 11 '20 at 14:48

2 Answers2

0

I have tested your code on my side, it will show the following error: "ResourceWarning: unclosed "

We could try to ignore this warning using the following code:

Python 3 script:

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

More detail information, please check Python 3 unittest docs.

Besides, you could also refer to this thread to close the resource.

Zhi Lv
  • 10,958
  • 1
  • 6
  • 16
  • I have muted the warnings for now, but really I'm concerned about finding out why this issue is prevalent on Edge and not any of the other browsers I'm testing. Specifically, all the warnings I'm getting point to line 374 of the RemoteConnection selenium code. – due_rest May 12 '20 at 15:50
  • According to the [official docs](https://docs.python.org/3/library/warnings.html), this warning related to resource usage.After checking some similar thread with the similar warning, it seems that this warning happens because Requests uses a keep-alive model that means we try not to explicitly close sockets in many cases. Exactly why this is happening is likely due to you either using a Session object that you don't explicitly close at the end of your test,or using an older version of Requests.Since this warning doesn't terminate the program, generally,the workaround is to ignore this warning. – Zhi Lv May 13 '20 at 06:10
0

Upgrade to msedge v81 and use selenium==4.0.0a5, where you need to use:

    options = EdgeOptions()
    #options.add_argument('headless')
    #options.add_argument('disable-gpu')
    options.add_argument('log-level=0')
    options.use_chromium = True
    #options.binary_location = r"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
    self.driver = Edge(options=options)

On older seleniums there is no use_chromium key in Edge(). It is slower than Chrome and it dumps a lot of debug into stdout.

MortenB
  • 1,378
  • 12
  • 22