How can I use Selenium to verify the href URL's are properly formed? - django

I'm trying to figure out how to use Django Selenium Webdriver to test that the href URL's on a particular page are correctly formed.
e.g. http://www.domain.com/post/12/test_post
So I want to test a regex like:
'^post/(?P<id>\d+)/test_post)$'
While I know the name of the post(slug), I don't know what the ID will be.
How can I use Selenium to test that the href URL's are correctly formed?
I tried:
self._driver.find_element_by_xpath("//a[matches(#href='/post/(/?P<id>\d+)/test_post')]")
But I get this error:
*** InvalidSelectiorException: Message: u'The given selector //a[matches(#href=\'/post/(/?P<id>\\d+)/test_post\')] is either invalid or does not result in a WebElement. The following error occurred:\n[InvalidSelectorError] Unable to locate an element with the xpath expression //a[matches(#href=\'/post/(/?P<id>\\d+)/test_post\')] because of the following error:\n[Exception... "The expression is not a legal expression." code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)" location: "file:///var/folders/m
But I kept getting, ""The expression is not a legal expression."
self._driver.find_element_by_xpath("//a[starts-with(#href, '/post']")
self._driver.find_element_by_xpath("//a[starts-with(#href, 'post/']")
self._driver.find_element_by_xpath("//a[starts-with(#href, '/post/']")
Here I also got ""The expression is not a legal expression."
Is there a way I can verify that the href tags are properly formed?

Hy,
It seems that you are not closing the parentheses of the method matches
Try:
self._driver.find_element_by_xpath("//a[matches(#href, '/post/(?P<id>\d+)/test_post')]")

Related

Fail2Ban regex for Drupal log not matching

I am trying to match and ban certain patterns in my drupal logs (drupal 9).
I have taken the base drupal-auth regex, created a new conf and tried to amend it to my requirements but I seem to be failing at the first hurdle. This is the code that will give me anything that has the type 'user' and this is filtered by the user\ in the code below, just before the <HOST> block:
failregex = ^%(__prefix_line)s(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})(\/[\w\.-]+)*\|\d{10}\|user\|<HOST>\|.+\|.+\|\d\|.*\|.+\.$
If I want to search exactly the same pattern, but with say 'page not found' or 'access denied' instead of 'user' what do I need? I cannot seem to get it to match the moment the type has a space in it. It seems such a simple thing to do!
I am using fail2ban-regex --print-all-matched to test.

How to define a Regular Expression in Jmeter?

my problem is that I need to define a regular expression in Jmeter in order to make a DELETE method in my page. The real problem is that I cannot delete a specific token with an ID.
I defined a regular Expression which his name is "uidprofesor" that refers to the token. This token, when I try to delete it manually and catch the request, I get the following code in the Raw Response Data:
{"headers":
{"Location":["/api/profesores/5c745065-3155-4ff7-ac65-7699a36f611c"],
"X-afppApp-alert":["afppApp.profesor.created"],
"X-afppApp-params":["5c745065-3155-4ff7-ac65-7699a36f611c"]},
"body":{"id":"5c745065-3155-4ff7-ac65-7699a36f611c",
"nif":"12345678Q",
"name":"jmeter_122",
"firstSurname":"jmeter_122",
"secondSurname":"",
"address":"",
"postalCode":"",
"telephone":"",
"mobile":"",
"email":"",
"dateInsert":"24/04/2019"
},
"statusCode":"CREATED",
"statusCodeValue":201
}
, where I assume that "5c745065-3155-4ff7-ac65-7699a36f611c" is the ID.
If I wanted to define "uidprofesor" to catch that ID what should I define in the regular erxpression?
I tried defining the regular expression like:
{"headers":"(.+?)"
{"headers":{"Location":"(.+?)"
{"Location":"(.+?)"
But nothing. It shows the Default Value: NOT_FOUND and does not delete the selected token.
I let you here some extra info:
regular expression ${uidprofesor} = {"headers":"(.+?)"
In the HTTP Request the Path is:
https://desa1eap7.asturias.es/afpp-back/${uidprofesor}
If I do the deletion manually, in Google Chrome Developer Tab, It returns:
Requested URL: https://desa1eap7.asturias.es/afpp-back/api/profesores
If you need more info just ask it. Thanks.
Use jMeter JSON Extractor: https://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor
It uses JsonPath https://goessner.net/articles/JsonPath/
$.headers.Location[0]
Check online here https://jsonpath.com/

Vue.js - Element UI - Form validation regexp

I'd like to validate a form using vue-js 2.3 and element-ui
Apparently, they use async-validator to validate the form. The documentation is here.
Example
https://jsfiddle.net/skd3nyqh/
Problem
Uncaught Error: Unknown rule type D*([2-9]d{2})(D*)([2-9]d{2})(D*)(d{4})D*
I do not know how to validate a field with regexp. I find the documentation not explicit enough on that topic.
From the documentation.
Pattern
The pattern rule property indicates a regular expression that the
value must match to pass validation.
Use
{required: true, pattern:/\D*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4})\D*/, message: 'Please input phone', trigger: 'blur' }
Updated fiddle.

Regular expression for validating url with parameters

I have been searching high and low for a solution to this, but to no avail. I am trying to prevent users from entering poorly formed URLs. Currently I have this regular expression in place:
^(http|https)\://.*$
This does a check to make sure the user is using http or https in the URL. However I need to go a step further and validate the structure of the URL.
For example this URL: http://mytest.com/?=test is clearly invalid as the parameter is not specified. All of the regular expressions that I've found on the web return valid when I use this URL.
I've been using this site to test the expressions that I've been finding.
Look I think the best solution for testing the URL as :
var url="http://mytest.com/?=test";
Make 2 steps :
1- test only URL as :
http://mytest.com/
use pattern :
var pattern1= "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)([A-Za-z]){2,3}(\/)?";
2- split URL string by using pattern1 to get the URL query string and IF URL has Query string then make test on It again by using the following pattern :
var query=url.split(pattern1);
var q_str = query[1];
var pattern2 = "^(\?)?([0-9A-Za-z]+=[0-9A-Za-z]+(\&)?)+$";
Good Luck,
I believe the problem you are having comes from the fact that what is or is not a valid parameter from a query string is not universally defined. And specifically for your problem, the criteria for a valid query is still not well defined from your single example of what should fail.
To be precise, check this out RFC3986#3.4
Maybe you can make up a criteria for what should be an "acceptable" query string and from that you can get an answer. ;)

RegularExpressionAttribute fails validating right data

I have a regular expression that works great when I try it:
System.Text.RegularExpressions.Regex.IsMatch("universal",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
true
System.Text.RegularExpressions.Regex.IsMatch("universal £$%$£%",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
false
But when I use it as a validation filter:
[RegularExpression(#"^[A-Za-z0-9 ._’&-/s]{0,100}$", ErrorMessage = "The parameter is not valid")]
It works in the client side, but it does not work on the server side. For example when I pass the word "universal" the ModelState contains an error regarding the field marked with that regex validator.
This attribute is the only validation rule applied to that field, what may be the problem?
Cheers.