Python Selenium Mocking - Dictionary stub inside a mock crashes test with AttributeError why?











up vote
0
down vote

favorite












I have an issue where I have a dictionary stub inside a mock and my test crashes. Shouldn't the mock replace everything to begin with so I shouldn't even run into this issue?



script.py



from selenium import webdriver

def nothing_here_really_just_setting_up_the_logic_flow():
"""
This function is completely irrelevant. It just loads a website
returns the webdriver session as "driver" and a dictionary
"add extra xpath" with the addtional xpath syntax for the function
where my issue is at
"""

# Load a website
driver = webdriver.Chrome()
driver.get(#some website)

# Prepare extra xpath syntax
add_extra_xpath = {}
add_extra_xpath['add me'] = 1 # The actual dict is more complex then this and
# this number keeps changing so that's why I'm using a
# dict, if you're wondering

return driver, add_extra_xpath


def function_where_I_have_a_problem(driver, add_extra_xpath):
"""
Simply find an element on the site via a connotated xpath
"""

# Find an element by xpath with the help of "add_extra_path"
find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


test_script.py



import pytest
import script

from unittest.mock import MagicMock

# Mock the "driver" and stub the "add_extra_xpath" dictionary
@pytest.fixture
def fixture():

driver = MagicMock()
driver.find_element_by_xpath.side_effect = driver

add_extra_xpath = {}
add_extra_xpath['add me'] = 1

return driver, add_extra_xpath

def test_function_where_I_have_a_problem(fixture):

driver, add_extra_xpath = fixture

script.function_where_I_have_a_problem(driver, add_extra_xpath)

assert driver.call_count == 1


Running the test crashes due to an:



AttributeError: 'dict' object has no attribute 'find_element_by_xpath'



at



find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


Shouldn't the side_effect I set back in my fixture override this 'find_element_by_xpath'?










share|improve this question




















  • 1




    Can you post the full error trace? I'm wondering on what line the error is thrown.
    – killian95
    Nov 7 at 19:32










  • @killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
    – curiousgeorge
    Nov 7 at 19:51








  • 1




    Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
    – killian95
    Nov 7 at 19:57






  • 1




    I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
    – curiousgeorge
    Nov 7 at 20:21








  • 1




    Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
    – killian95
    Nov 7 at 20:22















up vote
0
down vote

favorite












I have an issue where I have a dictionary stub inside a mock and my test crashes. Shouldn't the mock replace everything to begin with so I shouldn't even run into this issue?



script.py



from selenium import webdriver

def nothing_here_really_just_setting_up_the_logic_flow():
"""
This function is completely irrelevant. It just loads a website
returns the webdriver session as "driver" and a dictionary
"add extra xpath" with the addtional xpath syntax for the function
where my issue is at
"""

# Load a website
driver = webdriver.Chrome()
driver.get(#some website)

# Prepare extra xpath syntax
add_extra_xpath = {}
add_extra_xpath['add me'] = 1 # The actual dict is more complex then this and
# this number keeps changing so that's why I'm using a
# dict, if you're wondering

return driver, add_extra_xpath


def function_where_I_have_a_problem(driver, add_extra_xpath):
"""
Simply find an element on the site via a connotated xpath
"""

# Find an element by xpath with the help of "add_extra_path"
find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


test_script.py



import pytest
import script

from unittest.mock import MagicMock

# Mock the "driver" and stub the "add_extra_xpath" dictionary
@pytest.fixture
def fixture():

driver = MagicMock()
driver.find_element_by_xpath.side_effect = driver

add_extra_xpath = {}
add_extra_xpath['add me'] = 1

return driver, add_extra_xpath

def test_function_where_I_have_a_problem(fixture):

driver, add_extra_xpath = fixture

script.function_where_I_have_a_problem(driver, add_extra_xpath)

assert driver.call_count == 1


Running the test crashes due to an:



AttributeError: 'dict' object has no attribute 'find_element_by_xpath'



at



find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


Shouldn't the side_effect I set back in my fixture override this 'find_element_by_xpath'?










share|improve this question




















  • 1




    Can you post the full error trace? I'm wondering on what line the error is thrown.
    – killian95
    Nov 7 at 19:32










  • @killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
    – curiousgeorge
    Nov 7 at 19:51








  • 1




    Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
    – killian95
    Nov 7 at 19:57






  • 1




    I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
    – curiousgeorge
    Nov 7 at 20:21








  • 1




    Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
    – killian95
    Nov 7 at 20:22













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an issue where I have a dictionary stub inside a mock and my test crashes. Shouldn't the mock replace everything to begin with so I shouldn't even run into this issue?



script.py



from selenium import webdriver

def nothing_here_really_just_setting_up_the_logic_flow():
"""
This function is completely irrelevant. It just loads a website
returns the webdriver session as "driver" and a dictionary
"add extra xpath" with the addtional xpath syntax for the function
where my issue is at
"""

# Load a website
driver = webdriver.Chrome()
driver.get(#some website)

# Prepare extra xpath syntax
add_extra_xpath = {}
add_extra_xpath['add me'] = 1 # The actual dict is more complex then this and
# this number keeps changing so that's why I'm using a
# dict, if you're wondering

return driver, add_extra_xpath


def function_where_I_have_a_problem(driver, add_extra_xpath):
"""
Simply find an element on the site via a connotated xpath
"""

# Find an element by xpath with the help of "add_extra_path"
find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


test_script.py



import pytest
import script

from unittest.mock import MagicMock

# Mock the "driver" and stub the "add_extra_xpath" dictionary
@pytest.fixture
def fixture():

driver = MagicMock()
driver.find_element_by_xpath.side_effect = driver

add_extra_xpath = {}
add_extra_xpath['add me'] = 1

return driver, add_extra_xpath

def test_function_where_I_have_a_problem(fixture):

driver, add_extra_xpath = fixture

script.function_where_I_have_a_problem(driver, add_extra_xpath)

assert driver.call_count == 1


Running the test crashes due to an:



AttributeError: 'dict' object has no attribute 'find_element_by_xpath'



at



find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


Shouldn't the side_effect I set back in my fixture override this 'find_element_by_xpath'?










share|improve this question















I have an issue where I have a dictionary stub inside a mock and my test crashes. Shouldn't the mock replace everything to begin with so I shouldn't even run into this issue?



script.py



from selenium import webdriver

def nothing_here_really_just_setting_up_the_logic_flow():
"""
This function is completely irrelevant. It just loads a website
returns the webdriver session as "driver" and a dictionary
"add extra xpath" with the addtional xpath syntax for the function
where my issue is at
"""

# Load a website
driver = webdriver.Chrome()
driver.get(#some website)

# Prepare extra xpath syntax
add_extra_xpath = {}
add_extra_xpath['add me'] = 1 # The actual dict is more complex then this and
# this number keeps changing so that's why I'm using a
# dict, if you're wondering

return driver, add_extra_xpath


def function_where_I_have_a_problem(driver, add_extra_xpath):
"""
Simply find an element on the site via a connotated xpath
"""

# Find an element by xpath with the help of "add_extra_path"
find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


test_script.py



import pytest
import script

from unittest.mock import MagicMock

# Mock the "driver" and stub the "add_extra_xpath" dictionary
@pytest.fixture
def fixture():

driver = MagicMock()
driver.find_element_by_xpath.side_effect = driver

add_extra_xpath = {}
add_extra_xpath['add me'] = 1

return driver, add_extra_xpath

def test_function_where_I_have_a_problem(fixture):

driver, add_extra_xpath = fixture

script.function_where_I_have_a_problem(driver, add_extra_xpath)

assert driver.call_count == 1


Running the test crashes due to an:



AttributeError: 'dict' object has no attribute 'find_element_by_xpath'



at



find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')


Shouldn't the side_effect I set back in my fixture override this 'find_element_by_xpath'?







python unit-testing selenium-webdriver mocking pytest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:45

























asked Nov 7 at 19:20









curiousgeorge

417




417








  • 1




    Can you post the full error trace? I'm wondering on what line the error is thrown.
    – killian95
    Nov 7 at 19:32










  • @killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
    – curiousgeorge
    Nov 7 at 19:51








  • 1




    Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
    – killian95
    Nov 7 at 19:57






  • 1




    I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
    – curiousgeorge
    Nov 7 at 20:21








  • 1




    Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
    – killian95
    Nov 7 at 20:22














  • 1




    Can you post the full error trace? I'm wondering on what line the error is thrown.
    – killian95
    Nov 7 at 19:32










  • @killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
    – curiousgeorge
    Nov 7 at 19:51








  • 1




    Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
    – killian95
    Nov 7 at 19:57






  • 1




    I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
    – curiousgeorge
    Nov 7 at 20:21








  • 1




    Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
    – killian95
    Nov 7 at 20:22








1




1




Can you post the full error trace? I'm wondering on what line the error is thrown.
– killian95
Nov 7 at 19:32




Can you post the full error trace? I'm wondering on what line the error is thrown.
– killian95
Nov 7 at 19:32












@killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
– curiousgeorge
Nov 7 at 19:51






@killian95 Thanks for mentioning that! I totally forgot about that part so I'll add it in. As a matter of fact, I realized I made a few mistakes in my code. I've editted it to reflect and replicate my issue now. I can't exactly post the full error trace since this is just a simple, made up replication of the actual code. However, it does replicate the actual issue I'm having; which is a crash due to an AttributeError.
– curiousgeorge
Nov 7 at 19:51






1




1




Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
– killian95
Nov 7 at 19:57




Got it. Well honestly I can't recreate the error you're getting from what you've posted -- there must be some difference between the 'example' code and your actual code. Is there any reason you can't post your real code?
– killian95
Nov 7 at 19:57




1




1




I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
– curiousgeorge
Nov 7 at 20:21






I can but I'm just simplifying it down because the rest of the code is irrelevant and long. In either case, isolating the actual code further seems to pass for some reason. So my answer is lingering somewhere but I'll uncover it eventually on my own now. Whether you realize it or not, you just asking your questions just pretty much answered my question. Lolz Thanks!!!
– curiousgeorge
Nov 7 at 20:21






1




1




Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
– killian95
Nov 7 at 20:22




Happy to help! I get it -- sometimes you just need to bounce it off someone to find whats wrong ;-)
– killian95
Nov 7 at 20:22

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196349%2fpython-selenium-mocking-dictionary-stub-inside-a-mock-crashes-test-with-attrib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196349%2fpython-selenium-mocking-dictionary-stub-inside-a-mock-crashes-test-with-attrib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()