Pytest bdd error "scenario_wrapper() missing 1 required positional argument: 'request'" - python

I'm learning pytest and I get an error trying to run a test that worked fine before I added pytest-bdd(feature file, step file is the one I'm trying to run)
This is my code:
import pytest
import time
from selenium import webdriver
from pytest_bdd import scenario, given, when, then
#scenario('../feature/learn.feature', 'learning pytest-bdd')
#when('Does work-->website open, find element')
def does_work():
driver = webdriver.Edge(r"C:/Users/artri/Downloads/edgedriver_win64/msedgedriver.exe")
driver.get('https://www.example.com/')
driver.maximize_window()
print(driver.title)
time.sleep(5)
element = driver.find_element_by_xpath("//button[#class='btn btn-outline-light btn-lg btn-close']")
element.click()
name = driver.find_element_by_xpath("//a[#href='/items/something/']").text
driver.quit()
return name
#then('Get text "something"')
def test_work():
assert does_work() == 'something'
It finds the element and checks if it's value is right. I would like to know why after adding steps and making the feature file it doesn't work. Other test in the same steps file works just like they did before.
The error massage:
#then('Get text "something"')
def test_work():
> assert does_work() == 'something'
E TypeError: scenario_wrapper() missing 1 required positional argument: 'request'
I looked at a couple similar questions, but non were understandable for me.
If anyone has any idea where the issue might be or some links would be very appreciated!

You have decorator without function def.
From documentation at: https://github.com/pytest-dev/pytest-bdd
#scenario('publish_article.feature', 'Publishing the article')
def test_publish():
pass
You need to decorate empty function.

Related

Python Selenium - Trying to fail a test when expected value is not found

I have an application where users can create lists...once they create a list it will show up in a table on their homepage of the app. Only lists the user creates should display in the "created by" column of the table.
I'm trying to have the test fail and print if the expected name is not found.
users_names = context.browser.find_elements_by_xpath("//tr//td[7]")
expected_name = "%s" % name
for name in expected_name:
if name.text != expected_name:
print("Failed\n", users_names)
If I understand your question correctly, you would like to verify that the only lists shown are in-fact created by the user. There's a few ways of doing this, and the example below can be improved.
We have our setUp() that's used to get everything ready before we
start our test, profiles, preferences etc etc
Then test_show_only_user_created_lists() is the meat of our test,
where we hold the functionality of what we're testing (you may have
to place your remaining code within this section, if you had to do
anything before checking the list)
Then we have our tearDown() which we use to finish off our remaining
test, in this case we just close down our browser.
We could use our handy unit test library and asserting only after our loop has finished. But we must make sure to populate our 'expected_name' variable.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class listTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_show_only_user_created_lists(self):
driver = self.driver
driver.get("http://yoursite.com")
users_names = driver.find_elements_by_xpath("//tr//td[7]")
testOutcome = 1
for name in users_names:
if name.text != expected_name:
testOutcome = 0
### Assert ####
self.assertTrue(testOutcome)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
We can then run our test with the following command with indication of passes or failures:
python selenium-python-test.py

clickAndHold is not working in Selenium webdriver (python)

I have a function with actions like click, focus, etc. and works fine, but I need the function clickAndHold and returns an error when I try to run test.
This is a piece of code of my function:
def start_action(self, selector, action, value):
browser = self.d
element = browser.find_element_by_xpath(selector)
if action == 'clickAndHold':
actions = ActionChains(browser)
actions.clickAndHold(element)
actions.perform()
And this is the error:
AttributeError: 'ActionChains' object has no attribute 'clickAndHold'
Please help me!
In Python this method called click_and_hold(). Try to use it instead of clickAndHold()
Note that in Python in most cases snake_case used instead of camelCase

getElementById() takes exactly 1 argument (2 given)

I want to do an automation of the Internet Explorer. Open the Internet Explorer, navigate to login.live.com and set a value into the email textbox.
Here's the simple script:
import win32com.client
import time
IE = win32com.client.DispatchEx("InternetExplorer.Application")
IE.Visible = 1
IE.Navigate('login.live.com')
time.sleep(5)
DOC = IE.document
DOC.getElementById('i0116').value = 'test'
The last line always returns the following TypeError:
getElementById() takes exactly 1 argument (2 given)
When I try to add the value through the console of the Internet Explorer it works.
Btw. the getElementsByTagName() method works without any Errors.
Thanks for any help!
Okay.. I wrote a workaround for this:
DOC = IE.Document
inputs = DOC.documentElement.getElementsByTagName('input')
for field in inputs:
if field.id == 'i0116':
email = field
break
email.value = 'example#test.com'
For browser automation I recommend to use the Selenium library.
As this answer suggests you have to use
DOC.Body.getElementById('i0116').value = 'test'

Selenium, how to start Firefox with addons?

I want to load the Firefox Addon RequestPolicy. This is how i tried it:
rp = open(wd + "/requestpolicy.xpi")
firefoxProfile = FirefoxProfile()
firefoxProfile.add_extension(rp)
self.driver = webdriver.Firefox(firefoxProfile)
self.usr = user.User(self.driver, username, password, world)
self.usr.login()
No error, according to the Docs it should work, but it doesn't, it still starts without the addon.
Next thing i've tried is calling it this way:
self.driver = webdriver.Firefox(browser_profile=firefoxProfile)
Output:
TypeError: __init__() got an unexpected keyword argument 'browser_profile'
But this is an aspect of python i dont know much about. I got that idea because the source looks that way.
I don't have enough Stackoverflow rep to leave a comment on your question, and unfortunately I don't know the answer to your question, but for what it's worth you need to call webdriver.Firefox() with firefox_profile, not browser_profile, as you have done.
See also: http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/firefox/webdriver.py#33
what I did and worked was:
profile=webdriver.FirefoxProfile()
profile.add_extension("/home/.../.mozilla/firefox/zrdb9ki8.default/extensions/{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}.xpi") # for adblockplus
profile.set_preference("extensions.adblockplus.currentVersion", "2.8.2")
Fox = webdriver.Firefox(profile)
Fox.get(website_Url) #https://.....
It took me a few hours to find a solution.
All you need to do is download your extension as .xip file.
And then add this line to your code:
driver.install_addon('/Users/someuser/app/extension.xpi', temporary=True)
Replace "/Users/someuser/app/extension.xpi" with path to your extension .xip file.
Additionally you should not open the xpi file directly. Instead try to just give the address:
firefoxProfile.add_extension(wd + "/requestpolicy.xpi")

Why do I get TypeError: get() takes exactly 2 arguments (1 given)? Google App Engine

I have been trying and trying for several hours now and there must be an easy way to retreive the url. I thought this was the way:
#from data.models import Program
import basehandler
class ProgramViewHandler(basehandler.BaseHandler):
def get(self,slug):
# query = Program.all()
# query.filter('slug =', fslug)
self.render_template('../presentation/program.html',{})
Whenever this code gets executed I get this error on the stacktrace:
appengine\ext\webapp__init__.py", line 511, in call
handler.get(*groups)
TypeError: get() takes exactly 2 arguments (1 given)
I have done some debugging, but this kind of debugging exceeds my level of debugging. When I remove the slug from def get(self,slug) everything runs fine.
This is the basehandler:
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class BaseHandler(webapp.RequestHandler):
def __init__(self,**kw):
webapp.RequestHandler.__init__(BaseHandler, **kw)
def render_template(self, template_file, data=None, **kw):
path = os.path.join(os.path.dirname(__file__), template_file)
self.response.out.write(template.render(path, data))
If somebody could point me in the right direction it would be great! Thank you! It's the first time for me to use stackoverflow to post a question, normally I only read it to fix the problems I have.
You are getting this error because ProgramViewHandler.get() is being called without the slug parameter.
Most likely, you need to fix the URL mappings in your main.py file. Your URL mapping should probably look something like this:
application = webapp.WSGIApplication([(r'/(.*)', ProgramViewHandler)])
The parenthesis indicate a regular expression grouping. These matched groups are passed to your handler as arguments. So in the above example, everything in the URL following the initial "/" will be passed to ProgramViewHandler.get()'s slug parameter.
Learn more about URL mappings in webapp here.
If you do this:
obj = MyClass()
obj.foo(3)
The foo method on MyClass is called with two arguments:
def foo(self, number)
The object on which it is called is passed as the first parameter.
Maybe you are calling get() statically (i.e. doing ProgramViewHandler.get() instead of myViewHandlerVariable.get()), or you are missing a parameter.

Categories

Resources