How debuging twisted application in PyCharm - twisted

I would like to debug a Twisted Application in PyCharm
from twisted.internet import defer
from twisted.application import service, internet
from txjason.netstring import JSONRPCServerFactory
from txjason import handler
class Example(handler.Handler):
def __init__(self, who):
self.who = who
#handler.exportRPC("add")
#defer.inlineCallbacks
def _add(self, x, y):
yield
defer.returnValue(x+y)
#handler.exportRPC()
def whoami(self):
return self.who
factory = JSONRPCServerFactory()
factory.addHandler(Example('foo'), namespace='bar')
application = service.Application("Example JSON-RPC Server")
jsonrpcServer = internet.TCPServer(7080, factory)
jsonrpcServer.setServiceParent(application)
How to run app from command line I know, but how to start debugging in PyCharm can't understand

Create a new Run Configuration in PyCharm, under the "Python" section.
If you start this application using twistd, then configure the "Script" setting to point to that twistd, and the "script parameters" as you would have them on the command line. You'll probably want to include the --nodaemon option.
You should then be able to Run that under PyCharm, or set breakpoints and Debug it.

Related

QWebEngineView crashes when loading a PDF

I am porting my small project from pyqt5 to pyqt6 which deals with displaying PDFs using QWebEngineView.
I used this PyQt5 snippet to display the PDF below:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
class Window(QWidget):
def __init__(self):
super().__init__()
self.webView = QWebEngineView()
self.webView.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.webView.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
url = QUrl.fromLocalFile(r"C:/Users/Eliaz/Desktop/qt5cadaquesPart14.pdf")
self.webView.setUrl(url)
self.webView.show()
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec())
Now when I ported it into a PyQt6 snippet given in the code below, the program is just crashing without any error messages:
import sys
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import QWebEngineSettings
class Window(QWidget):
def __init__(self):
super().__init__()
self.webView = QWebEngineView()
self.webView.settings().setAttribute(QWebEngineSettings.WebAttribute.PluginsEnabled, True)
self.webView.settings().setAttribute(QWebEngineSettings.WebAttribute.PdfViewerEnabled, True)
url = QUrl.fromLocalFile(r"C:/Users/Eliaz/Desktop/qt5cadaquesPart14.pdf")
self.webView.setUrl(url)
self.webView.show()
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec())
Before exiting though, it shows these information on the console:
qt.webenginecontext:
GL Type: desktop
Surface Type: OpenGL
Surface Profile: CompatibilityProfile
Surface Version: 4.6
QSG RHI Backend: OpenGL
Using Supported QSG Backend: yes
Using Software Dynamic GL: no
Using Multithreaded OpenGL: yes
Init Parameters:
* application-name python
* browser-subprocess-path C:\Users\Eliaz\AppData\Local\Programs\Python\Python310\lib\site-packages\PyQt6\Qt6\bin\QtWebEngineProcess.exe
* create-default-gl-context
* disable-es3-gl-context
* disable-features ConsolidatedMovementXY,InstalledApp,BackgroundFetch,WebOTP,WebPayments,WebUSB,PictureInPicture
* disable-speech-api
* enable-features NetworkServiceInProcess,TracingServiceInProcess
* enable-threaded-compositing
* in-process-gpu
* use-gl desktop
Even with the additional information above, I can't still find what's wrong with my code but I'm certain that the path of the PDFs exists.
Also, I am on Windows 10 These are the versions of PyQt6 that I have. (Updated)
PyQt6 6.3.1
PyQt6-Qt6 6.3.1
PyQt6-sip 13.4.0
PyQt6-WebEngine 6.3.1
PyQt6-WebEngine-Qt6 6.3.1
Update:
I tried updating the pyqt6 packages installed in my system to 6.3.1, and testing it in a fresh virtual box but it still crashes as seen in this GIF.
This seems to be caused by a Windows-specific bug, since eveything works as expected on Linux using both PyQt-5.15.7 and PyQt-6.1.3. It's hard to say what the cause might be, given that there's no relevant output.
As a work-around, an alternative is to use PDF.js. See here for more details:
How to render PDF using pdf.js viewer in PyQt?
But note that it may be necessary to use the legacy version with PyQt5.

Could not instantiate new WebDriver instance of type class org.openqa.selenium.remote.RemoteWebDriver

Getting the below error while try running in sauce lab. I am not sure what is the issue to apply fix here. Could someone extend help?
Note: Secure remote tunnel is provisioned and running successfully.
Command:
mvn clean verify -Dwebdriver.driver="remote" -Dsaucelabs.browserName="chrome" -Dsaucelabs.browserVersion="102" -Dsaucelabs.platformName="windows 10" -Dsaucelabs.url="https://foo:boo#ondemand.eu-central-1.saucelabs.com:443/wd/hub" -Denv="dev" -Dwebdriver.base.url="https://test.com/login" -Dsaucelabs.accessKey="foo" -Dsaucelabs.username="boo" -Dcucumber.filter.tags="#test" -Dsaucelabs.tunnelName="koo" -Dsaucelabs.tunnelOwner="boo" -Dsaucelabs.name="POC" -Dwebdriver.remote.url="https://ondemand.us-west-1.saucelabs.com/wd/hub"
TestRunner.java
package xyz.abc;
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(plugin = {"pretty"},
features = "Test",
glue = "fooboo"
)
public class TestRunner {
}
Expected Results:-
Execution should take place in sauce labs.
Actual Results:-
Could not instantiate class org.openqa.selenium.remote.RemoteWebDriver
It seems you are not setting your Sauce Labs user and API key, which ends up as an unauthorized request.

How do I call scrapy from airflow dag?

My scrapy project runs perfectly well with 'scrapy crawl spider_1' command. How to trigger it (or call the scrappy command) from airflow dag?
with DAG(<args>) as dag:
scrapy_task = PythonOperator(
task_id='scrapy',
python_callable= ?)
task_2 = ()
task_3 = ()
....
scrapy_task >> [task_2, task_3, ...]
Run with BashOperator
https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/bash.html
with DAG(<args>) as dag:
scrapy_task = BashOperator(
task_id='scrapy',
bash_command='scrapy crawl spider_1')
If you're using virtualenv, you may use VirtualEnvOperator
or to use existing environment, you can use source activate venv && scrapy crawl spider_1
Run with PythonOperator
From scrapy documentation: https://docs.scrapy.org/en/latest/topics/practices.html#run-scrapy-from-a-script
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
process = CrawlerProcess(get_project_settings())
process.crawl('spider_1')
process.start() # the script will block here until the crawling is finished

Getting the error in python selenium testing for login and logout functionality

I am getting the error as...
AttributeError: 'LoginTestCase' object has no attribute 'driver'
And the code is
from selenium import webdriver;
from selenium.webdriver.common.keys import Keys
import time
import unittest
class LoginTest(unittest.TestCase):
#classmethod
def setUpclass(cls):
cls.driver = webdriver.Firefox("D:/Frontend/18-01-2021/selenium-testing")
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
def test_login_valid(self):
self.driver.get("http://localhost:4200")
self.driver.find_element_by_name("username").send_keys("admin")
self.driver.find_element_by_name("password").send_keys("admin#123")
self.driver.find_element_by_id('log').click()
self.driver.find_element_by_id('ab').click()
self.driver.find_element_by_id('usersinfo').click()
self.driver.find_element_by_id('log_out').click()
time.sleep(8)
#classmethod
def tearDownClass(cls):
cls.driver.close()
cls.driver.quit()
print("Test Completed")
I have built the above code for testing login to functionality using selenium with python unit testing .
But while executing the python code showing some errors as
AttributeError: 'LoginTestCase' object has no attribute 'driver'
Can anyone help me regarding this
There are some typos in your code:
setUpclass should be setUpClass
the test method is not indented correctly.
I think that fixing the setUpClass typo will get you going.

Recording Selenium tests for functional Plone test cases

I'd like to use Selenium for creating simple functional tests against Plone add-ons. The main driver here is that non-programmers can create and understand test cases with some effort, because they see in a web browser what's happening. What is the recommended best practice to
The test case prepares Plone site environment where the test will be run (installs add-ons, mocks up mail host, creates sample content)
How to run Plone functional test case to the point you can start a Selenium recording in a browser and how to open a browser with recording enabled?
How later run the recorded test output from Python code?
Are there other test recording frameworks out there which you combine with Plone? Able to tests against Javascripted UI is a requirement.
My guess is that there are tools that do the individual steps independently, but those tools don't work together exactly as you describe.
In my experience, the quality of recorded tests is so bad that a programmer will have to rewrite them anyway. It only gets worse when you have a lot of JavaScript. If in addition, you have a site that uses AJAX, one of the problems that occurs is that you will sometimes have to wait for a specific element to appear, before doing the next click, and this is where most recorders will fail.
I would also love to hear about a tool that is targeted to end users, and allows them to record and run Plone tests on their own, and if anyone knows about this kind of project, I would really like to get involved in its development.
plone.app.testing comes with seleniumtestlayer since 4.1.
Here is my own more sophisticated helper code:
"""
Some PSE 2012 Selenium notes
* https://github.com/plone/plone.seleniumtesting
* https://github.com/emanlove/pse2012
Selenium WebDriver API
* http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webdriver.py
Selenium element match options
* http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/common/by.py
Selenium element API (after find_xxx())
* http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webelement.py
You can do pdb debugging using ``selenium_helper.selenium_error_trapper()`` if you run
tests with ``SELENIUM_DEBUG`` turned on::
SELENIUM_DEBUG=true bin/test -s testspackage -t test_usermenu
Then you'll get debug prompt on any Selenium error.
"""
import os
# Try use ipdb debugger if we have one
try:
import ipdb as pdb
except ImportError:
import pdb
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.wait import WebDriverWait
from plone.app.testing import selenium_layers
SELENIUM_DEBUG = "SELENIUM_DEBUG" in os.environ
class SeleniumTrapper(object):
"""
With statement for break on Selenium errors to ipdb if it has been enabled for this test run.
"""
def __init__(self, driver):
self.driver = driver
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
"""
http://effbot.org/zone/python-with-statement.htm
"""
if isinstance(value, WebDriverException) and SELENIUM_DEBUG:
# This was Selenium exception
print "Selenium API call failed because of browser state error: %s" % value
print "Selenium instance has been bound to self.driver"
pdb.set_trace()
class SeleniumHelper(object):
"""
Selenium convenience methods for Plone.
Command Selenium browser to do common actions.
This mainly curries and delegates to plone.app.testing.selenium_layers helper methods.
More info:
* https://github.com/plone/plone.app.testing/blob/master/plone/app/testing/selenium_layers.py
"""
def __init__(self, testcase, driver=None):
"""
:param testcase: Yout test class instance
:param login_ok_method: Selenium check function to run to see if login success login_ok_method(selenium_helper)
"""
self.testcase = testcase
if driver:
# Use specific Selenium WebDriver instance
self.driver = driver
else:
# plone.app.tesrting selenium layer
self.driver = testcase.layer['selenium']
self.portal = testcase.layer["portal"]
def selenium_error_trapper(self):
"""
Create ``with`` statement context helper which will invoke Python ipdb debugger if Selenium fails to do some action.
If you run test with SELENIUM_DEBUG env var set you'll get dropped into a debugger on error.
"""
return SeleniumTrapper(self.driver)
def reset(self):
"""
Reset Selenium test browser between tests.
"""
def login(self, username, password, timeout=15, poll=0.5, login_cookie_name="__ac", login_url=None):
"""
Perform Plone login using Selenium test browser and Plone's /login_form page.
"""
submit_button_css = '#login_form input[name=submit]'
if not login_url:
# Default Plone login URL
login_url = self.portal.absolute_url() + '/login_form'
with self.selenium_error_trapper():
submit_button = self.open(login_url, wait_until_visible=submit_button_css)
self.find_element(By.CSS_SELECTOR, 'input#__ac_name').send_keys(username)
self.find_element(By.CSS_SELECTOR, 'input#__ac_password').send_keys(password)
submit_button.click()
# Check that we get Plone login cookie before the timeout
waitress = WebDriverWait(self.driver, timeout, poll)
matcher = lambda driver: driver.get_cookie(login_cookie_name) not in ["", None]
waitress.until(matcher, "After login did not get login cookie named %s" % login_cookie_name)
def logout(self, logout_url=None):
"""
Perform logout using Selenium test browser.
:param logout_url: For non-default Plone logout view
"""
if not logout_url:
logout_url = self.portal.absolute_url() + "/logout"
self.open(logout_url)
def get_plone_page_heading(self):
"""
Get Plone main <h1> contents as lowercase.
XXX: Looks like Selenium API returns uppercase if there is text-transform: uppercase?
:return: Empty string if there is no title on the page (convenience for string matching)
"""
try:
title_elem = self.driver.find_element_by_class_name("documentFirstHeading")
except NoSuchElementException:
return ""
if not title_elem:
return ""
return title_elem.text.lower()
def trap_error_log(self, orignal_page=None):
"""
Read error from the site error log and dump it to output.
Makes debugging Selenium tests much more fun when you directly see
the actual errors instead of OHO.
:param orignal_page: Decorate the traceback with URL we tried to access.
"""
# http://svn.zope.org/Zope/trunk/src/Products/SiteErrorLog/SiteErrorLog.py?rev=96315&view=auto
error_log = self.portal.error_log
entries = error_log.getLogEntries()
if len(entries) == 0:
# No errors, yay!
return
msg = ""
if orignal_page:
msg += "Plone logged an error when accessing page %s\n" % orignal_page
# We can only fail on traceback
if len(entries) >= 2:
msg += "Several exceptions were logged.\n"
entry = entries[0]
raise AssertionError(msg + entry["tb_text"])
def is_error_page(self):
"""
Check that if the current page is Plone error page.
"""
return "but there seems to be an error" in self.get_plone_page_heading()
def is_unauthorized_page(self):
"""
Check that the page is not unauthorized page.
..note ::
We cannot distingush login from unauthorized
"""
# require_login <-- auth redirect final target
return "/require_login/" in self.driver.current_url
def is_not_found_page(self):
"""
Check if we got 404
"""
return "this page does not seem to exist" in self.get_plone_page_heading()
def find_element(self, by, target):
"""
Call Selenium find_element() API and break on not found and such errors if running tests in SELENIUM_DEBUG mode.
"""
with self.selenium_error_trapper():
return self.driver.find_element(by, target)
def find_elements(self, by, target):
"""
Call Selenium find_elements() API and break on not found and such errors if running tests in SELENIUM_DEBUG mode.
"""
with self.selenium_error_trapper():
return self.driver.find_elements(by, target)
def click(self, by, target):
"""
Click an element.
:param by: selenium.webdriver.common.by.By contstant
:param target: CSS selector or such
"""
with self.selenium_error_trapper():
elem = self.driver.find_element(by, target)
elem.click()
def open(self, url, check_error_log=True, check_sorry_error=True, check_unauthorized=True, check_not_found=True, wait_until_visible=None):
"""
Open an URL in Selenium browser.
If url does not start with http:// assume it is a site root relative URL.
:param wait_until_visible: CSS selector which must match before we proceed
:param check_error_log: If the page has created anything in Plone error log then dump this traceback out.
:param check_sorry_error: Assert on Plone error response page
:param check_unauthorized: Assert on Plone Unauthorized page (login dialog)
:return: Element queried by wait_until_visible or None
"""
elem = None
# Convert to abs URL
if not (url.startswith("http://") or url.startswith("https://")):
url = self.portal.absolute_url() + url
selenium_layers.open(self.driver, url)
if check_error_log:
self.trap_error_log(url)
if wait_until_visible:
elem = self.wait_until_visible(By.CSS_SELECTOR, wait_until_visible)
# XXX: These should be waited also
if check_sorry_error:
self.testcase.assertFalse(self.is_error_page(), "Got Plone error page for url: %s" % url)
if check_unauthorized:
self.testcase.assertFalse(self.is_unauthorized_page(), "Got Plone Unauthorized page for url: %s" % url)
if check_not_found:
self.testcase.assertFalse(self.is_not_found_page(), "Got Plone not found page for url: %s" % url)
return elem
def wait_until_visible(self, by, target, message=None, timeout=10, poll=0.5):
"""
Wait until some element is visible on the page (assume DOM is ready by then).
Wraps selenium.webdriver.support.wait() API.
http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait
"""
if not message:
message = "Waiting for element: %s" % target
waitress = WebDriverWait(self.driver, timeout, poll)
matcher = lambda driver: driver.find_element(by, target)
waitress.until(matcher, message)
elem = self.driver.find_element(by, target)
return elem
(Not on github yet)