How to combine two urls in selenium webdriver - java

I have declared two variable for url as below
Url = vars.get("http://stackoverflow.com");
url1 = vars.get("/questions");
driver.get(Url + Url1);
When I am executing it, it is opening a web browser but there is no url address in the browser and nothing is happening. Could you please let me know where I am wrong?

I think it is easiest to do if you put both urls like Strings as this:
Url = "http://stackoverflow.com";
url1 = "/questions";
And then you can combine both Strings with "+" simbol:
driver.get(Url + Url1);
But what it's exactly vars? Can you explain it please?
EDIT: .get method in Selenium Webdriver needs a String in their declaration to works properly. Look at this: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#get-java.lang.String-
Key methods are get(String), which is used to load a new web page, and the various methods similar to findElement(By), which is used to find WebElements.

Related

How do determine the final URL from a link in Java

This is a link generated from Google Alerts, and I would like to get where you get redirected. So I need the URL and I would have to retrieve it with Java. I have checked for the response, but no location header redirect.
https://www.google.com/url?rct=j&sa=t&url=http://naija247news.com/2016/03/nigerian-bond-yields-rise-after-cbns-interest-rate-hike-aimed-at-luring-investors/&ct=ga&cd=CAIyGjA3ZmJiYzk0ZDM0N2U2MjU6Y29tOmVuOlVT&usg=AFQjCNGs7HsYSodEUnECfdAatG6KgY18DA
Maybe something like this:
String URL = "https://www.google.com/url?rct=j&sa=t&url=http://naija247news.com/2016/03/nigerian-bond-yields-rise-after-cbns-interest-rate-hike-aimed-at-luring-investors/&ct=ga&cd=CAIyGjA3ZmJiYzk0ZDM0N2U2MjU6Y29tOmVuOlVT&usg=AFQjCNGs7HsYSodEUnECfdAatG6KgY18DA";
String subStr = URL.substring(URL.indexOf("url=") + 1, URL.indexOf("&ct"));
I forgot what the starting and ending position has to be exactly, which indexes. So you would have to verify that and check it creates a substring at the right position. But the basic idea is to cut out the URL you need and nothing more. This is an example for what you forwarded. It could be that you would have to search for something else to know the end of the substring, when you have a different URL (in the provided example I look for &ct, which maybe be not be the case in another URL). You will have to look up several URLs you have to know how to cut out the URL.

How to get an xsp value from URL link using Java

I am trying to retrieve the page name(.xsp)from the URL of the current page using Java. i have been able to accomplish the same thing with the Javascript below
context.getUrl().getSiteRelativeAddress(context).toString()
and it works but i want to get the same thing don using Java.
The best way to get SSJS variable names via Java is resolveVariable. This should work:
XSPContext context = (XSPContext) ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "context");
String pageName = context.getUrl().getSiteRelativeAddress(context).toString();
(Updated with correct syntax for second line, thanks Knut)

Deleting all words matching a regex pattern

I would like to remove the character sequences like "htsap://" or "ftsap://" from a String. Is it possible?
Let me illustrate my needs with an example.
Actual input String:
"Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For "ftsap://"example, the URL for CSM Library's home page is: "htsap://"www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to htsap://where a web page originates and who might be responsible for the information at that page or site."
Expected resulting String:
"Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For example, the URL for CSM Library's home page is: www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to where a web page originates and who might be responsible for the information at that page or site."
Patterns I tried: (not very sure it is a right way)
((.*?)(?=("htsap://|ftsap://")))
and:
((.*?)(?=("htsap://|ftsap://")))(.*)
Could anyone please suggest here?
Since you're escaping your quotes within your sample Strings, I'll assume you're working in Java.
You should try:
final String res = input.replaceAll("\"?\\w+://\"?", "");
Here is a link to a working example of what does this regex match exactly!
How it works:
It matches and removes any sequence of alphanumeric characters (and underscores), followed by :// and possibly preceded and/or followed by ".
EDIT: How to achieve the same result using a Matcher?
final String input = "Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For \"ftsap://\"example, the URL for CSM Library's home page is: \"htsap://\"www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to htsap://where a web page originates and who might be responsible for the information at that page or site.";
final Pattern p = Pattern.compile("\"?\\w+://\"?");
final StringBuilder b = new StringBuilder(input);
Matcher m;
while((m = p.matcher(b.toString())).find()) {
b.replace(m.start(), m.end(), "");
}
System.out.println(b.toString());
Use this regex:
"(ftsap|htsap).//"
And replace it with ''
Regex explained:
"(ftsap|htsap).//" with flag g
Debuggex Demo

How to compare two urls when the run time url generates dynamic id

I am learning automation. I am not able two compare to urls in which the dynamic ids get generated every run.
first url:
https://open.login.yahoo.com/openid/yrp/signin?idp=facebook&ts=1393975202&.intl=us&.lang=en-US&.done=http%3A%2F%2Fmail.yahoo.com&rpcrumb=ZJMkr60PLSv&.src=ym
comparing with runtime url where ids gets dynamically generated
first at idp=facebook&ts=139xxx
second at &rpcrumb=xxx
Please help
If that is the only difference, you can just remove these 2 attributes from both urls before comparing:
String url1 = url1.replaceFirst("ts=[^&]*&", "").replaceFirst("rpcrumb =[^&]*&", "");
String url1 = url2.replaceFirst("ts=[^&]*&", "").replaceFirst("rpcrumb =[^&]*&", "");
// now compare
if (url1.equalsIgnoreCase(url2)) {...}

How do I reverse route a static file?

At first I had this link to a twitter icon:
#{'/public/images/twitter-icon.png'/}
But now I want to show a Twitter-, Facebook- or LinkedIn icon depending on type. So, I created a FastTag that takes the type as a parameter and the code looks like this:
In the view:
#{myApp.icon contact.type/}
FastTag Java Code:
String type = (String) args.get("arg");
out.print("/public/images/" + type + "-icon.png");
It works fine. But, on our build server we run the app with a prefix on the uri like this
http://ourdomain.com/appname/...
Obviously /public/images... won't work here. So I figured I have to ask the Router for the proper address. I've tried the following with no success:
Router.reverse("/public/images/" + type + "-icon.png");
Router.reverse("/public/");
Router.reverse("staticDir:public");
All three result in a NoRouteFoundException. How can I get the correct route for my icons?
In the routes file I have the default route for static files
GET /public/ staticDir:public
I believe this is what you want:
String imageUrl = Router.reverse(VirtualFile.fromRelativePath("public/images/" + type + "-icon.png"));
Router.reverse be used generate URL form one action!
maybe you can define a route which include your app name and route one action eg:
GET /appname/public/ TestController.test
now,you can use
Router.reverse("TestController.test")
get the URL.
I think it's better to do something like:
GET /img/ staticDir:public/images
And in the template just:
out.print("/img/" + type + "-icon.png");

Categories

Resources