I have a URL with some dynamic parts
https://{env}-my-website.{domain}/UnDefault.aspx
And every time I want to go to another module I have to replace this part
UnDefault.aspx
with the module to which I want to navigate e.g.
Modules/Repairs/SNH.aspx
Is there an easy way to do that with Selenium or Regex?
this is what i tried :
String currentUrl = driver.getCurrentUrl();
String[] results = StringUtils.substringsBetween(currentUrl, "https", "?:cn|com");
String SNH= results[0]+"Modules/Repairs/SNH.aspx";
driver.navigate().to(SNH);
To replace the path part of the url (everything after the server):
String url = url.replaceAll("^(.*?//[^/]+/).*", "$1Modules/Repairs/SNH.aspx");
Related
I currently have a S3 bucket directory key like this:
String dir = "s3://mybucket/workflow/science/sweet-humoor/vars";
What I am trying to do is to get the prefix of this S3 directory, a prefix is actually without s3:://mybucket/, so what I want to have is workflow/science/sweet-humoor/vars
Now, what would be a elegant way to achieve this? I know the quickest way to do is to do a subString(13), but this will break whenever the bucket name changes.
How would you handle this?
Use a regular expression with replaceAll:
String result = directoryKey.replaceAll("s3://[^/]+/", "");
The regex here is:
s3://[^/]+/
It matches the part that you want to remove, which is s3:// followed by a bunch of non-slash characters, followed by a slash.
It's cleanest to use the Java library functions for paths instead of handling the Strings directly. What you have is an URL, so
URL url = new URL(dir);
URI uri = url.toURI();
Path fullpath = Paths.get(uri);
Now you have a Path (ie the "/mybucket/workflow/science/sweet-humoor/vars" part), and you can get the subpath by
// start index 1 to skip the first directory element
Path subpath = fullpath.subpath(1, fullpath.getNameCount()-1);
You can make a File out of this (subpath.toFile()), or just get the path string by
subpath.toString();
The URIBuilder class from the org.apache.http.client.utils package can do that.
URIBuilder builder = new URIBuilder(dir);
String thePath = builder.getPath();
This automatically extracts /workflow/science/sweet-humoor/vars from the path. The retrieved path does not include mybucket, because URIBuilder sees the first part immediately after the protocol specifier (s3://) as hostname.
Further processing can be done through Path p = Paths.get(thePath).
You can try this:
String dir2=dir.replaceAll("s3://"+dir.split("/")[2]+"/","");
String dir = "s3://mybucket/workflow/science/sweet-humoor/vars";
dir = dir.replace("//", "").substring( dir.indexOf("/") );
System.err.println(dir); // prints mybucket/workflow/science/sweet-humoor/vars
I would split the string by "/" and get the values from third index and join it with "/". Sample code in python.
input_string = "s3://mybucket/workflow/science/sweet-humoor/vars"
list1 = (input_string.split("/"))
print(list1)
print("/".join(list1[3:]))
Output:
workflow/science/sweet-humoor/vars
Now I know about FilenameUtils.getExtension() from apache.
But in my case I'm processing extensions from http(s) urls, so in case I have something like
https://your_url/logo.svg?position=5
this method is gonna return svg?position=5
Is there the best way to handle this situation? I mean without writing this logic by myself.
You can use the URL library from JAVA. It has a lot of utility in this cases. You should do something like this:
String url = "https://your_url/logo.svg?position=5";
URL fileIneed = new URL(url);
Then, you have a lot of getter methods for the "fileIneed" variable. In your case the "getPath()" will retrieve this:
fileIneed.getPath() ---> "/logo.svg"
And then use the Apache library that you are using, and you will have the "svg" String.
FilenameUtils.getExtension(fileIneed.getPath()) ---> "svg"
JAVA URL library docs >>>
https://docs.oracle.com/javase/7/docs/api/java/net/URL.html
If you want a brandname® solution, then consider using the Apache method after stripping off the query string, if it exists:
String url = "https://your_url/logo.svg?position=5";
url = url.replaceAll("\\?.*$", "");
String ext = FilenameUtils.getExtension(url);
System.out.println(ext);
If you want a one-liner which does not even require an external library, then consider this option using String#replaceAll:
String url = "https://your_url/logo.svg?position=5";
String ext = url.replaceAll(".*/[^.]+\\.([^?]+)\\??.*", "$1");
System.out.println(ext);
svg
Here is an explanation of the regex pattern used above:
.*/ match everything up to, and including, the LAST path separator
[^.]+ then match any number of non dots, i.e. match the filename
\. match a dot
([^?]+) match AND capture any non ? character, which is the extension
\??.* match an optional ? followed by the rest of the query string, if present
I need to extract a randomly generated part of an URL for a Selenium Test in Java.
When the browser opens a page, e.g.:
/edit_person.html?id=eb58cea3a3772ff656987792eb0a8c0f
then I'm able to show the url with:
String url = driver.getCurrentUrl();
but now I need to get only the randomly generated ID after the equals sign.
How do I extract the value of parameter id once I have the entire URL as a string in variable url?
URL.getQuery() will give the query portion as a String it is a simple regular expression match to isolate the part you want.
id=(.*) will get you what you want as long as it is the only thing in the query string.
This is how managed to solve the problem:
String url = driver.getCurrentUrl();
URL aURL = new URL(url);
url = aURL.getQuery();
String[] id = url.split("=");
System.out.println(id[1]);
Thanks to Jarrod Roberson!
I am trying to replace url with another url.
Below is the example of source url
http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries/
so this url should be replace with below url,
http://sysserver01.internal.com/var/www/html/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries/
It means if source url comes then the part after resources in source url must be appended with /var/www/html/(and rest of part after resources in source url).
This needs to be happen with rendom set of source url that contains resources string.
I dont have enough knowldege of string manipulation. So please someone help me to solve this query. Please try to solve it in JAVA as I choose this platform for my work.
String originalUrl = "http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries";
String newUrl = originalUrl.replaceAll("web/www/internal/projectwork/resources", "var/www/html");
String originalUrl = "http://sysserver01.internal.com/web/www/internal/projectwork/resources/injury-prevention-and-recovery/avoiding-injury/overview-of-running-injuries";
String newUrl = originalUrl.replace("web/www/internal/projectwork/resources", "var/www/html");
I'm trying to make use of google api as text-to-speech. So, I build a String then should pass it as a URL to a component to obtain a MP3 with the spoken words.
So, this is my code:
URI uri = new URI("http://translate.google.com/translate_tts?tl=es&q="+ URLEncoder.encode((String)this.text.getValue(), "UTF-8"));
When I make uri.toString() its return a well formed URL. If I copy and paste this output in the browser works pefectly.
But if I assign this returned String to the source property of a ice:outputMedia is not working. Then inspect the HTML generated in the page and the String in src property is:
http://translate.google.com/translate_tts?tl=es&q=Bobby+need+peanuts
The & symbol has been replaced by &.
How can I avoid this to make a valid URL?
You need to decode the url on the client side using Javascript.
var decoded = decodeURI(URI)