How to append link in current URL in Selenium using java - selenium

I want to know how to append link in current URL. For eg: me link is https://www.google.co.in/ in current program now I have to append /#q=ask+questions in this URL.
Please help.
I know how to get current url(by using getCurrentUrl() syntax)
Thanks

You could use URIBuilder. Something like this:
String someUrl = "https://www.google.co.in";
// or perhaps
// String someUrl = browser.getCurrentUrl();
URIBuilder uri = new URIBuilder(someUrl);
uri.setPath("search");
uri.addQueryParam("q", "ask+questions");
Assert.assertEquals(uri.toString(), "https://www.google.co.in/search?q=ask%2Bquestions");
// or perhaps
// browser.get(uri.toString());

getCurrentUrlUrl() has a return type of String. So you can save it's value and play around like you can with any String.
Take an example, I want to get this url and then append your string and then get() this new webpage:
String url = driver.getCurrentUrl();
String newurl = url+"/#q=ask+questions";
driver.get(newurl);

Related

Comparing entire url through Assert.assertEquals

I want to compare the url and print, so I have used the below code. But it was comparing the whole url like as below
Actual comparison done for the below url :
https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=
Code I have used :
String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "https://accounts.google.com" );
System.out.println(URL);
Solution needed:
I want compare only the 'https://accounts.google.com'
Please help me out to solve this issue
When you access the url https://accounts.google.com the url is set as :
https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=
This url is dynamic in nature. So you won't be able to use assertEquals() as :
assertEquals() is defined as :
void org.testng.Assert.assertEquals(String actual, String expected)
Asserts that two Strings are equal. If they are not, an AssertionError is thrown.
Parameters:
actual the actual value
expected the expected value
So assertEquals() will validate if two Strings are identical. Hence you see the error.
Solution
To assert the presence of https://accounts.google.com within the current url you can use the function Assert.assertTrue() as follows :
String URL = driver.getCurrentUrl();
Assert.assertTrue(URL.contains("https://accounts.google.com"));
System.out.println(URL);
Explanation
assertTrue() is defined as :
void org.testng.Assert.assertTrue(boolean condition)
Asserts that a condition is true. If it isn't, an AssertionError is thrown.
Parameters:
condition the condition to evaluate
In your case, you should not use 'assertEquals' instead use 'assertTrue'
Assert.assertTrue(URL.startsWith("https://accounts.google.com"));
I have used below code and it working fine for me
String URL = driver.getCurrentUrl();
if(URL.contains("url name"))
{
System.out.println("Landed in correct URL" +
"" + URL);
}else
{
System.out.println("Landed in wrong URL");
}
If You want to remove the Assert.assertTrue and check your url then use this simple technique as below code. AssertEquals, Assertions
Assert.assertTrue(url.equals("https://www.instagram.com/hiteshsingh00/?hl=en"));
can also be written as
Assert.assertEquals(true,url.equals("https://www.instagram.com/hiteshsingh00/?hl=en"));
also assertTrue(x == 2);
assertEquals(2,x);

Get Sitefinitys Generated Password Reset URL

I am trying to get the URL from the password reset which I receive via email of the sitefinity frontend login.
I need to send the URL with the username which is enterd in the form to a server to send the email.
I already tried to override the SendResetPasswordEmail of the LoginFormModel but that only gives me the URL where the reset is located at. (localhost/login/resetpassword)
It looks like the URL is generated in the method SendRecoveryPasswordMail of the Telerik.Sitefinity.Security.UserManager which is not overridable.
Is there a way to get the generated recovery URL to use it in a custom method?
Thanks in advance
Since you already have the URL of the reset password page, I guess your issue is getting the proper query string to pass to that page.
Looking at the source code with JustDecompile, the query string is made up of this:
?vk=userValidationKeyEncoded&cp=pr
The cp=pr seems to be hardcoded, so we leave it as is, the question is how the userValidationKeyEncoded is made.
Again, looking in the code, it is this line:
string userValidationKeyEncoded = UserManager.GetUserValidationKeyEncoded(userByEmail);
And finally:
private static string GetUserValidationKeyEncoded(User user)
{
object[] providerName = new object[] { user.ProviderName, ',', user.Id, ',', DateTime.UtcNow };
string str = string.Format("{0}{1}{2}{3}{4}", providerName);
return SecurityManager.EncryptData(str).UrlEncode();
}
You can use the above code to manually generate the validationKey.

how to handle special character( in values) of matrix parameter - Spring REST URL

I need to pass itemNumber1 value as 075/458
http://localhost:8080/projectroot/some/itemNumber=075%2F458
or
http://localhost:8080/projectroot/some/itemNumber=075/458
But this is not hitting my controller method:
#RequestMapping("/some/{number}")
public #ResponseBody void getSomething(
#MatrixVariable(required = true) String itemNumber1,
#MatrixVariable(required = false) String itemNumber2,
#MatrixVariable(required = false) String itemNumber3)
I see that you are trying to parse it via the URL, but you are trying to get it by #ResponseBody. If you are using GET method to parse the value, your response body will be empty. If you try to get the data via response body, try the POST method instead.

plus variable to string with beanshell scripting

Can anyone help me on this case, i want to put value of variable "pass" to String "formValue", but cannot load the right body for http post request using Jmeter:
steps,
ThreadGroup with HTTP Request has ${formValue} on body tab,
add beanShell PreProcessor with script bellow:
String pass = "123456";
String formValue = "{\"userName\": \"admin\",\"password\":vars.get("pass")}";
vars.put("formValue",formValue);
thanks!
If I correctly getting the idea your code should be amended as follows:
String pass = "123456";
vars.put("pass", pass);
String formValue = "{\"userName\": \"admin\",\"password\":\"" + vars.get("pass") + "\"}";
vars.put("formValue", formValue);
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more Beanshell and JMeter related tips and tricks.

An interesting Restlet Attribute behavior

Using Restlet 2.1 for Java EE, I am discovering an interesting problem with its ability to handle attributes.
Suppose you have code like the following:
cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
and on your browser you provide the following URL:
http://localhost:8100/testpath/command
then, of course, the attr attribute gets set to "command".
Unfortunately, suppose you want the attribute to be something like command/test, as in the following URL:
http://localhost:8100/testpath/command/test
or if you want to dynamically add things with different levels, like:
http://localhost:800/testpath/command/test/subsystems/network/security
in both cases the attr attribute is still set to "command"!
Is there some way in a restlet application to make an attribute that can retain the "slash", so that one can, for example, make the attr attribute be set to "command/test"? I would like to be able to just grab everything after testpath and have the entire string be the attribute.
Is this possible? Someone please advise.
For the same case I usually change the type of the variable :
Route route = cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
route.getTemplate().getVariables().get("attr") = new Variable(Variable.TYPE_URI_PATH);
You can do this by using url encoding.
I made the following attachment in my router:
router.attach("/test/{cmd}", TestResource.class);
My test resource class looks like this, with a little help from Apache Commons Codec URLCodec
#Override
protected Representation get() {
try {
String raw = ResourceWrapper.get(this, "cmd");
String decoded = new String(URLCodec.decodeUrl(raw.getBytes()));
return ResourceWrapper.wrap(raw + " " + decoded);
} catch(Exception e) { throw new RuntimeException(e); }
}
Note my resource wrapper class is simply utility methods. The get returns the string of the url param, and the wrap returns a StringRepresentation.
Now if I do something like this:
http://127.0.0.1/test/haha/awesome
I get a 404.
Instead, I do this:
http://127.0.0.1/test/haha%2fawesome
I have URLEncoded the folder path. This results in my browser saying:
haha%2fawesome haha/awesome
The first is the raw string, the second is the result. I don't know if this is suitable for your needs as it's a simplistic example, but as long as you URLEncode your attribute, you can decode it on the other end.