Hi I am creating a browser in python using PyQt, I am trying to allow the user to disable all Java Script alerts which is a protected function under qwebpage called javaScriptAlert, is there a way to disable this function or protected functions in general?
in the main class where browser can be defined as
self.browser = QWebView()
and then do another class
class MyWebPage(QWebPage):
def javaScriptAlert(self):
pass
then back in the main class do
self.page = MyWebPage()
self.browser.setPage(self.page)
Related
I currently have Python 3.7.3 with robotframework 3.1.1 and created a Python library (MySite.py) like this:
from selenium import webdriver
from collections import namedtuple
from HomePage import *
class Pages(object):
def __init__(self, handle):
self._pages = {}
self._home_window = handle
pass
#property
def home_window(self):
return self._home_window
#home_window.setter
def home_window(self, v):
self._home_window = v
#property
def homePage(self):
return self._pages['home']
def add(self, name, page):
self._pages[name] = page
def get(self, name):
return self._pages[name]
def getPages(self):
return self._pages
class MySite(object):
def __init__(self):
self._driver = None
#property
def driver(self):
return self._driver
#driver.setter
def driver(self, v):
self._driver = v
def close_all_windows(self):
# Close all windows
pages = self.pages.getPages()
for name, page in pages.items():
page.close()
def open_my_page(self):
self.driver = webdriver.Ie("IEDriverServer_Win32_3.141.0\\IEDriverServer.exe")
# Define pages
self.pages = Pages(self.driver.current_window_handle)
self.pages.add('home', HomePage(self.driver, self.pages))
# Open browser
self.driver.get(www.mypage.com)
# Code to wait to finish loading the page
When I create a Robot script to open and close the browser in one test case, it works:
*** Settings ***
Library MySite.py
*** Test Case ***
Open Browser to mypage.com and close browser
Open My Page
Close All Windows
But when I create a Robot script to open a browser in one test case and then on another test case to close it I get AttributeError: 'MySite' object has no attribute 'pages':
*** Settings ***
Library MySite.py
*** Test Case ***
Open Browser to mypage.com
Open My Page
Close Browser
Close All Windows
It seems like my instance variables are not being saved off for the second robot test. Do you know why this may be? Or what I am doing wrong?
By default, a new instance of the library is created for each test. You need to set the scope such that an instance of the class is created once for each suite or once for each test run.
This is mentioned in the user guide, in a section titled Test Library Scope:
Test libraries can control when new libraries are created with a class attribute ROBOT_LIBRARY_SCOPE. This attribute must be a string and it can have the following three values:
TEST CASE
A new instance is created for every test case. A possible suite setup and suite teardown share yet another instance. This is the default.
TEST SUITE
A new instance is created for every test suite. The lowest-level test suites, created from test case files and containing test cases, have instances of their own, and higher-level suites all get their own instances for their possible setups and teardowns.
GLOBAL
Only one instance is created during the whole test execution and it is shared by all test cases and test suites. Libraries created from modules are always global.
(emphasis mine)
To set the scope to "TEST SUITE" so that the instance is created only once for the whole suite, you would start your class definition like this:
class MySite(object):
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
I have created a python code to initialise a chrome browser and I want to pass this driver instance to Robot Framework so that the keywords of RF will work on this instance. Please let me know how could i do the same.
The Py code for intializing a file is :
'class SeleniumKeywords:
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self):
self.driver = None
def open_browser(self, browser="chrome"):
driver_class = drivers[browser]
self.driver = driver_class()
def go_to(self, url):
self.driver.get(url)'
Now when using it in Robot framework, the browser opens but the RF keywords doesnt work on it(selenium2library keywords). Basically I opening an browser instance using the custom keyword and maximizing using selenium2library keywords in RF. Unfortunately it doesnt work. Please let me know how to pass this browser instance to RF:
'*** Settings ***
Library ExtendedSelenium2Library
Library ../Resources/SeleniumKeywords.py
Resource ../Global/GlobalConfig.robot
*** Test Cases ***
Reuse Session ID
SeleniumKeywords.Open Browser chrome
maximize browser window
SeleniumKeywords.Go To ${URL} '
The maximize browser window is a RF keyword and I want it to work on my browserinstance
I have written my own library, but I extended the Selenium2Library and i can mix the Selenium2Library keywords with my own. The Selenium2Library or in your case, the ExtendedSelenium2Library will not recognize the session you just started in Python and will give the "No browser is open" error. The "Maximize Browser Window" keyword relies on a browser that was previoulsy opened with the "Open Browser" keyword.
If you really need your own Selenium library, you can do something like this:
class SeleniumKeywords(ExtendedSelenium2Library):
def go_to(self, url, browser="chrome"):
self.open_browser(url, browser)
I have a field in a property browser where the user sets a file path. I would like for them to get a file browser when the click on the line to edit it.
the file browser is
class TargetPropertiesBrowser(QtTreePropertyBrowser):
def __init__(self):
self._variantManager = QtVariantPropertyManager()
general_group = self._variantManager.addProperty(QtVariantPropertyManager.groupTypeId(), "General")
self._outputPath = self._variantManager.addProperty(QVariant.String, Target.OUTPUT_PATH)
self._outputPath.setToolTip("Output Directory")
general_group.addSubProperty(self._outputPath)
Now lets say I have some class PopUpBrowser that defines the popup I want displayed when they click in the property browser on the file path line. I can't find an example or documentation on how to alter the behavior or the QtTreePropertyBrowser.
Edit:
If there is a signal I can connect to for when a user clicks on the line that would be fine, however I don't see such a signal in the docs. I'm also not seeing any Enum for a variant manager (or any alternate managers) that supports a widget or button that could link a widget. Sorry if I was unclear.
Connect one of the Widget's Signals (e.g. clicked()) to a slot method in your class: http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html
I currently am doing some automated testing via Appium and python for testing an Android application. I'd like to abstract away some of the details so the tests read easier.
Right now I have just have an entire class doing the testing. For example, I'd like to turn this:
# authentication
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
username = self.driver.find_element_by_name('username')
password = self.driver.find_element_by_name('pw')
username.send_keys('some username')
password.send_keys('some password')
login_button = self.driver.find_element_by_name('Login')
login_button.click()
Into something like this:
Authentication.login(self.driver, 'largedata_lgabriel#hmbnet.com', 'R3DruM24')
Where our Authentication class might look like this:
class Authentication:
def login(driver, username, password):
input_username = driver.find_element_by_name('username')
input_password = driver.find_element_by_name('pw')
input_username.send_keys(username)
input_password.send_keys(password)
login_button = driver.find_element_by_name('Login')
login_button.click()
This would require creating an 'Authentication' class, but I'm unsure how to import these methods into my main testing class, and how to share the web driver object.
Here's how I structure this kind of thing. I also use unittest.TestCase which I want to highly recommend for any python automation; it'll allow you to lean on setUpClass, tearDownClass (one-time for all the derivative tests), and setUp, tearDown (before each test function) to do a lot of the set up and tear down stuff you need to do.
For my iOS-specific Appium tests:
Base file: ios_base.py
class iOSBase(unittest.TestCase):
#classmethod
def setUpClass(self):
# Set up logging if you want it
# Set up the XCode and iOS Simulator details you need if you're doing iOS
# Set up Appium
def setUp(self):
# Per test, I like to log the start of each test
self.log.info("--- {0} ---".format(self._testMethodName))
# etc. on setUp / tearDown stuff, whatever you need
# Helpful function like you have in mind
def login(self, username, password):
# You'll get self.driver for free by using setUpClass, yea!
Test file(s): test_login.py
from ios_base import iOSBase
class iOSLoginTests(iOSBase):
# Add any additional login-specific setUp you might want here, see also super()
def test_valid_login(self):
self.login(good_username, good_password)
def test_login_invalid_username(self):
self.login(bad_username, good_password)
# etc.
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
self.loadStarted.connect(self._load_started)
self.page().frameCreated.connect(self.onFrame)
# ...
browser = Browser()
browser.setHtml('<html>...</html>', baseUrl=QUrl('http://www.google.com/'))
After that, i need to catch content of all external resources loaded by QWebView. I need to get content of all CSS/Javascript files. How can i do that ? Related questions: question 1, question 2
I know i need to use QNetworkAccessManager somehow, but i don't have any example to use.
We need to make custom QNetworkReply class and get results in readyRead event results.