def validate( self, # The relative URL to validate. url, # An optional string that, if provided, must be in the text returned by the server. If this is a sequence of strings, all of the provided strings must be in the text returned by the server. expected_string="", # The number of validation errors expected. If None, no validation is performed. expected_errors=None, # The expected status code from the request. expected_status=200, # All additional keyword arguments are passed to the ``post`` method. **kwargs, ): try: try: self.post(url, **kwargs) except HTTPError as e: # If this was the expected result, return. if e.code == expected_status: # Since this is an error of some type, these paramets must be empty, since they can't be checked. assert not expected_string assert not expected_errors return "" else: raise assert self.status == expected_status if expected_string: if isinstance(expected_string, str): assert expected_string in self.text else: # Assume ``expected_string`` is a sequence of strings. assert all(string in self.text for string in expected_string) if expected_errors is not None and not self.pytestconfig.getoption( "skip_w3_validate"): # Redo this section using html5validate command line vld = Validator(errors_only=True, stack_size=2048) tmpname = self.tmp_path / "tmphtml.html" with open(tmpname, "w", encoding="utf8") as f: f.write(self.text) errors = vld.validate([str(tmpname)]) assert errors <= expected_errors return self.text except AssertionError: # Save the HTML to make fixing the errors easier. Note that ``self.text`` is already encoded as utf-8. validation_file = url.replace("/", "-") + ".html" with open(validation_file, "wb") as f: f.write(_html_prep(self.text)) print("Validation failure saved to {}.".format(validation_file)) raise except RuntimeError as e: # Provide special handling for web2py exceptions by saving the # resulting traceback. if e.args[0].startswith("ticket "): # Create a client to access the admin interface. admin_client = WebClient("{}/admin/".format( self.web2py_server_address), postbacks=True) # Log in. admin_client.post( "", data={"password": self.web2py_server.password}) assert admin_client.status == 200 # Get the error. error_code = e.args[0][len("ticket "):] admin_client.get("default/ticket/" + error_code) assert admin_client.status == 200 # Save it to a file. traceback_file = ("".join(c if c not in "\/:*?<>|" else "_" for c in url) + "_traceback.html") with open(traceback_file, "wb") as f: f.write(_html_prep(admin_client.text)) print("Traceback saved to {}.".format(traceback_file)) raise
def validate( self, # The relative URL to validate. url, # An optional string that, if provided, must be in the text returned by the server. If this is a list of strings, at least one of the provided strings but be in the text returned by the server. expected_string='', # The number of validation errors expected. If None, no validation is performed. expected_errors=None, # The expected status code from the request. expected_status=200, # All additional keyword arguments are passed to the ``post`` method. **kwargs): try: self.post(url, **kwargs) assert self.status == expected_status if expected_string: if isinstance(expected_string, str): assert expected_string in self.text else: # Assume ``expected_string`` is a list of strings. assert any(string in self.text for string in expected_string) if expected_errors is not None: vld = HTMLValidator() vld.validate_fragment(self.text) if len(vld.errors) != expected_errors: print('Errors for {}: {}'.format(url, len(vld.errors))) pprint(vld.errors) assert False if vld.warnings: print('Warnings for {}: {}'.format(url, len(vld.warnings))) pprint(vld.warnings) except AssertionError: # Save the HTML to make fixing the errors easier. Note that ``self.text`` is already encoded as utf-8. validation_file = url.replace('/', '-') + '.html' with open(validation_file, 'wb') as f: f.write(self.text.replace('\r\n', '\n')) print('Validation failure saved to {}.'.format(validation_file)) raise except RuntimeError as e: # Provide special handling for web2py exceptions by saving the # resulting traceback. if e.args[0].startswith('ticket '): # Create a client to access the admin interface. admin_client = WebClient('http://127.0.0.1:8000/admin/', postbacks=True) # Log in. admin_client.post( '', data={'password': self.web2py_server.password}) assert admin_client.status == 200 # Get the error. error_code = e.args[0][len('ticket '):] admin_client.get('default/ticket/' + error_code) assert admin_client.status == 200 # Save it to a file. traceback_file = url.replace('/', '-') + '_traceback.html' with open(traceback_file, 'wb') as f: f.write(admin_client.text.replace('\r\n', '\n')) print('Traceback saved to {}.'.format(traceback_file)) raise
def client(baseurl, fixture_create_testfile_for_application): '''Create a new WebClient instance once per module. ''' from gluon.contrib.webclient import WebClient webclient = WebClient(baseurl) return webclient
def validate(self, # The relative URL to validate. url, # An optional string that, if provided, must be in the text returned by the server. If this is a sequence of strings, all of the provided strings must be in the text returned by the server. expected_string='', # The number of validation errors expected. If None, no validation is performed. expected_errors=None, # The expected status code from the request. expected_status=200, # All additional keyword arguments are passed to the ``post`` method. **kwargs): try: try: self.post(url, **kwargs) except HTTPError as e: # If this was the expected result, return. if e.code == expected_status: # Since this is an error of some type, these paramets must be empty, since they can't be checked. assert not expected_string assert not expected_errors return '' else: raise assert self.status == expected_status if expected_string: if isinstance(expected_string, str): assert expected_string in self.text else: # Assume ``expected_string`` is a sequence of strings. assert all(string in self.text for string in expected_string) if expected_errors is not None and W3_VALIDATE: # Redo this section using html5validate command line vld = Validator(errors_only=True) tmpname = self.tmp_path / 'tmphtml.html' with open(tmpname, 'w') as f: f.write(self.text) errors = vld.validate([tmpname]) assert errors == expected_errors return self.text except AssertionError: # Save the HTML to make fixing the errors easier. Note that ``self.text`` is already encoded as utf-8. validation_file = url.replace('/', '-') + '.html' with open(validation_file, 'wb') as f: f.write(_html_prep(self.text)) print('Validation failure saved to {}.'.format(validation_file)) raise except RuntimeError as e: # Provide special handling for web2py exceptions by saving the # resulting traceback. if e.args[0].startswith('ticket '): # Create a client to access the admin interface. admin_client = WebClient('{}/admin/'.format(self.web2py_server_address), postbacks=True) # Log in. admin_client.post('', data={'password': self.web2py_server.password}) assert admin_client.status == 200 # Get the error. error_code = e.args[0][len('ticket '):] admin_client.get('default/ticket/' + error_code) assert admin_client.status == 200 # Save it to a file. traceback_file = url.replace('/', '-') + '_traceback.html' with open(traceback_file, 'wb') as f: f.write(_html_prep(admin_client.text)) print('Traceback saved to {}.'.format(traceback_file)) raise
# in MS Windows, create en environment variable (if not exists) # PYTHONPATH containing the path to web2py from gluon.contrib.webclient import WebClient import dommartin25 client = WebClient('http://www.dommartin25.fr') #Home page client.get('') assert ('Welcome on' in client.text) #test each page db = DAL('mysql://*****:*****@vps20356.ovh.net/dommartin25') for tablename in db.tables: print tablename # pages = db().select(db.page.ALL) # for page in pages: # client.get('pages/show_page/%s' %page.url) # assert(page.title in client.text) # # register # data = dict(first_name = 'Homer', # last_name = 'Simpson', # email = '*****@*****.**', # password = '******', # password_two = 'test', # _formname = 'register') # client.post('user/register',data = data)