0

In Python 3+

def some_method():
   href = browser.find_element_by_css_selector('div').get_attribute('href')
   title = browser.find_element_by_css_selector('div > a').get_attribute('title')
   text = browser.find_element_by_css_selector('div > p > h2').get_attribute('title')

This is some code snippet from code and i was performing unit testing on it, but could not get exact result as expected. For above code I need to mock only browser.find_element_by_css_selector('div').get_attribute('href') so that I can pass my custom link instead of getting this from browser.

tried till now:

@patch.object(WebElement,'get_attribute',return_value = 'some_url')

If I patch this get_attribute method ,then this get mocked for all places which I do not want to.

 when(WebElement).get_attribute('href').thenReturn('some-url')
 expect(WebElement,atleast=1).get_attribute(eq("href")).thenReturn('some-url')

And if i mock this using mockito , then I get errors on next lines. I tried using both when and expect but nothing worked

Exception caught:
Called but not expected: get_attribute('title')
    Stubbed invocations are:
         get_attribute('href')
         get_attribute(<Eq: href>)

How can we return the mocked value with multiple calls with exact arguments? For ex: if paramater being passed in css_selector and get_attributes are div and href then return some fake Url. If values are p > div and title then return another fake value and leave as it is for rest of the calls

Deepak Sharma
  • 41
  • 1
  • 4

2 Answers2

0

Instead of just setting a return value for the mock, could you instead use a side_effect function. The side effect function could then test if “href” is the attributes being get, if it is return your custom link, if not then return the original function call.

  • can you please give an example of the same . I am unable to get you correctly – Deepak Sharma Mar 28 '21 at 16:46
  • Take a look at this https://stackoverflow.com/a/51591390/12694095 essentially instead of return_value use side_effect to call a simple function that takes the attribute key as an arg then check if that arg is == ‘href’, if it is return your custom link else return the original function passed with the attribute key. Beautiful! – ModellingGeek Mar 28 '21 at 18:34
  • i tried that way as well , but it still ran into problem. There are multiple lines with same `get_attribute('href')` but they differs by value goes inside of `find_element_by_css_selector('differs-by-this-value')` – Deepak Sharma Mar 29 '21 at 17:25
  • Not sure but as you need to mock a specific combination of args to a method from a specifically configured calling function it maybe worth exploring whether your side_effect can introspect the calling function configuration and decide what action to take based on these values. Perhaps you can use the method_calls method of the mock object https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.mock_calls – ModellingGeek Mar 29 '21 at 18:43
0

In mockito, it is

spy2(WebElement.get_attribute)
when(WebElement).get_attribute('href').thenReturn('some-url')
herr.kaste
  • 442
  • 3
  • 12