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