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'?
python unit-testing selenium-webdriver mocking pytest
add a comment |
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'?
python unit-testing selenium-webdriver mocking pytest
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
add a comment |
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'?
python unit-testing selenium-webdriver mocking pytest
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
python unit-testing selenium-webdriver mocking pytest
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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