Extract project name from URL - java

I have URL
../p/xmlProj/bugs/...
I'm looking for a way to extract the project name into single variable using java code
I tried this
String url = "/p/xmlProject/bugs/";
final String[] projName = url.split("/",3);
But i didn't work for me !!

You don't need this 3 there. Simply write:
final String[] projName = url.split("/");
And the project name will be projName[3].

Related

How to extract basePath with pathParameters with RestAssured

So I am using RestAssured to test some endpoints and pulling the test result into an Allure report.
I am currently trying to fill the report with useful information about each test case, like the endopoint pinged.
To extract the url I tried using the following:
RequestSpecification requestSpec = RestAssured.given().spec(Request.getRequestSpecification());
QueryableRequestSpecification queryRequest = SpecificationQuerier.query(requestSpec);
String retrieveURI = queryRequest.getBaseUri();
System.out.println("Base URI is : "+retrieveURI);
String retrievePath = queryRequest.getBasePath();
System.out.println("Base PATH is : "+retrievePath);
where the Request.getRequestSpecification() returns a RequestSpecification build with RequestSpecBuilder .
the thing is my basePath is something like /info/{unitId}/something the value here is filled with a pathParameter once built. but the block
String retrievePath = queryRequest.getBasePath();
System.out.println("Base PATH is : "+retrievePath);
returns my basepath with {unitId} in it instead of the actual path parameter passed.
Is there a way to extract the basePath of a RestAssured request with the pathParameter within it ?
note, I know there is .log().uri() but this outputs the info in the counsole and I need it in a variable to send it to the Allure report.
To anyone this might help, QueryableRequestSpecification actually has a .getURI() method that wil essentially return the full uri with the pathParameters in it :
RequestSpecification requestSpec = RestAssured.given().spec(Request.getRequestSpecification());
QueryableRequestSpecification queryRequest = SpecificationQuerier.query(requestSpec);
String retrieveURI = queryRequest.getURI()
System.out.println("Full URI is : "+retrieveURI);
I use intelliJ on windows and for some reason the method was showing up red in my IDE
point of reference https://javadoc.io/doc/io.rest-assured/rest-assured/3.1.1/io/restassured/specification/QueryableRequestSpecification.html#getURI--

Extract part of Dynamic Url?

I have the following URl http://127.0.0.1/?code=AQABAAIAAAAGV_bv21
I need to capture the chracters after code=
but every time the URL is loaded that code is different..
I had something like this but since its dynamic I can not do this..
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
String code = url.substring(url.length() -10);
you can use something like below :-
String code = url.split("?code=")[1];
if you are on Android:
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
Uri uri = Uri.parse(url);
String code = uri.getQueryParameter("code");
or try the following regexp:
(\?|\&)([^=]+)\=([^&]+)
Try this.
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21";
String codeValue = url.replaceAll(".*code=([^&]*).*", "$1");
System.out.println(codeValue);
output:
AQABAAIAAAAGV_bv21
This method works even if other parameters are added. For example http://127.0.0.1/?id=123&code=AQABAAIAAAAGV_bv21&opt=yes

Question about extracting parameter from URL inside WebDriver

I need help with extracting parameter from URL. I have used
String url = driver.getCurrentUrl()
to catch current URL.
After that, I need to extract value from the parameter called oauth_verifier from
http://localhost:4200/consent?oauth_token=something&oauth_verifier=something
so I can use it later in code.
Any help would be great. Thanks in advance!
Try This One , I Am not Sure Its Right Way , But Its Work...
String str = "http://localhost:4200/consent?oauth_token=something&oauth_verifier=something";
String[] arrOfStr = str.split("/");
String temp=arrOfStr[3];
String temp2[]=temp.split("\\?|\\=");
To extract the value associated with oauth_verifier you need to induce WebDriverWait with ExpectedConditions as urlContains() the text oauth_verifier and you can use the following solution:
Code Block:
new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("oauth_verifier"));
String fullUrl = driver.getCurrentUrl();
String[] UrlParts = fullUrl.split("=");
System.out.println(UrlParts[2]);
Console Output:
something
Assuming its Java ,you should use the substring function of String .
yourstring.substring(indexOf1stCharacter,indexOfLastCharacter)
For your case:
url.substring(url.indexOf("oauth_verifier=") + 15 , url.length());

How to read the public URL in GWT?

I m new in GWT and I m generating a web application in which i have to create a public URL.
In this public URL i have to pass hashtag(#) and some parameters.
I am finding difficulty in achieving this task.
Extracting the hashtag from the URL.
Extracting the userid from the URL.
My public URL example is :: http://www.xyz.com/#profile?userid=10003
To access the URL in GWT you can use the History.getToken() method. It will give you the entire string that follows the hashtag ("#").
In your case (http://www.xyz.com/#profile?userid=10003) it will return a string "profile?userid=10003". After you have this you can parse it however you want. You can check if it contains("?") and u can split it by "?" or you can get a substring. How you get the information from that is really up to you.
I guess you already have the URL. I'm not that good at Regex, but this should work:
String yourURL = "http://www.xyz.com/#profile?userid=10003";
String[] array = yourURL.split("[\\p{Lower}\\p{Upper}\\p{Punct}}]");
int userID = 0;
for (String string : array) {
if (!string.isEmpty()) {
userID = Integer.valueOf(string);
}
}
System.out.println(userID);
To get the parameters:
String userId = Window.Location.getParameter("userid");
To get the anchor / hash tag:
I don't think there is something, you can parse the URL: look at the methods provided by Window.Location.

url harvester string manipulation

I'm doing a recursive url harvest.. when I find an link in the source that doesn't start with "http" then I append it to the current url. Problem is when I run into a dynamic site the link without an http is usually a new parameter for the current url. For example if the current url is something like http://www.somewebapp.com/default.aspx?pageid=4088 and in the source for that page there is a link which is default.aspx?pageid=2111. In this case I need do some string manipulation; this is where I need help.
pseudocode:
if part of the link found is a contains a substring of the current url
save the substring
save the unique part of the link found
replace whatever is after the substring in the current url with the unique saved part
What would this look like in java? Any ideas for doing this differently? Thanks.
As per comment, here's what I've tried:
if (!matched.startsWith("http")) {
String[] splitted = url.toString().split("/");
java.lang.String endOfURL = splitted[splitted.length-1];
boolean b = false;
while (!b && endOfURL.length() > 5) { // f.bar shortest val
endOfURL = endOfURL.substring(0, endOfURL.length()-2);
if (matched.contains(endOfURL)) {
matched = matched.substring(endOfURL.length()-1);
matched = url.toString().substring(url.toString().length() - matched.length()) + matched;
b = true;
}
}
it's not working well..
I think you are doing this the wrong way. Java has two classes URL and URI which are capable of parsing URL/URL strings much more accurately than a "string bashing" solution. For example the URL constructor URL(URL, String) will create a new URL object in the context of an existing one, without you needing to worry whether the String is an absolute URL or a relative one. You would use it something like this:
URL currentPageUrl = ...
String linkUrlString = ...
// (Exception handling not included ...)
URL linkUrl = new URL(currentPageUrl, linkUrlString);

Categories

Resources