Assert command for checking the url in selenium webdriver - xpath

As there is no xpath and id for the url on the webpage, how can i check if my actual url is matching with the expected url?
I have provided my code below, but it did not work.
String Actualtext = driver.findElement(By.linkText("http://localhost:8080/imdb/homepage")).getText();
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");

You can validate it against the 'current url' as
String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "http://localhost:8080/imdb/homepage" );

In Python, it is not getCurrentUrl, but
driver.current_url

getText() is used to fetch the visible innertText, You can use the getAttribute method to fetch the url (for hyperlink), something like below
String Actualtext = driver.findElement("YourElementLocator").getAttribute("href")
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");

In selenium webdriver C#
String URL = driver.Url;
Assert.AreEqual(URL, "http://localhost:8080/imdb/homepage");
In Selenium webdriver java
String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "http://localhost:8080/imdb/homepage" );

Related

get json string from ajax response text

The response text of my Ajax request is this :
"<json>
<![CDATA[
{"status":true,"filterMap":{ "summary":"Summary","total":"Total","myProfileMsg":"Opening my profile, please wait","cgBase":"CG Base","export1":"Export"}}
]]>
<offsetinTime>19800000</offsetinTime></json>"
I want to extract just the json string from this.. ie. what i want is :
{"status":true,"filterMap":{ "summary":"Summary","total":"Total","myProfileMsg":"Opening my profile, please wait","cgBase":"CG Base","export1":"Export"}}
How do i do this?
Use replace() & split() functions of String to achieve it:
var json ='<json><![CDATA[{"status":true,"filterMap":{"summary":"Summary","total":"Total","myProfileMsg":"Opening my profile, please wait","cgBase":"CG Base","export1":"Export"}}]]><offsetinTime>19800000</offsetinTime></json>';
var extractedJson=json.replace('<json><![CDATA[','').split(']')[0];
document.body.innerHTML=extractedJson;

Rest query params in Java

I need to do some queries against my datastore in Java but I can't seem to get the parameters syntax right. I tried like this:
String params = "?Active=1";
String urlString = "https://api.parse.com/1/classes/Cars" + params;
Or as per the document here:
String params = "where={Active:1}";
But both ways generate an exception.
If I don't do the query and simply try to get all the objects with this request string:
String urlString = "https://api.parse.com/1/classes/Cars"
everything works fine. So the problem is definitely the params sequence. So is there a way to do Prase.com rest queries in Java?
EDIT: adding the exception string in response to a request from the first comment:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.parse.com/1/classes/Cars?where={Active:1}
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
I should also note that when I use the regular http syntax, as in
params = "?Dealer=asdf";
the query comes back with all the objects, as if the parameter wasn't there.
Here are a couple of working examples for the params string:
String params = "where={\"objectId\":\"ldl49l3kd98\"}";
String params = "where={\"CompanyName\":\"BMW\", \"Price\":{\"$gte\":29000,\"$lte\":49000}}";
And if you need non English characters, like I do, encode the param string like this:
params = URLEncoder.encode(params, "UTF-8");

Vaadin Image URL from StreamResource

I get my images from my database via the StreamResource and use afterwards the Image() class to show the picture. Is it possible to get from this StreamResource or Image class the URL?
Through the browser I can find out that the image has the URL that looks like: "... / Web/APP/connector/0/32/source/Picture-xxx.JPG"
I need the URL to the image for CSSInject.
Thanks in advance
So..
Client side Vaadin resource link pattern is like:
[protocol]+"://"+[currentUrl]+"/APP/connector/"+[uiId]+"/"+[cid]+"/source/"+[filename]
where:
protocol - request protocol,
currentUrl - current URL,
uiId - Vaadin UI identifier,
cid - connector identifier,
filename - filename.
Example function to get url:
String getResourceURL(AbstractClientConnector connector,FileResource resource){
String protocol = UI.getCurrent().getPage().getLocation().getScheme();
String currentUrl = UI.getCurrent().getPage().getLocation().getAuthority();
String cid = connector.getConnectorId();
Integer uiId = connector.getUI().getUIId();
String filename = resource.getFilename();
return protocol+"://"+currentUrl+"/APP/connector/"+uiId+"/"+cid+"/source/"+filename;
}
where:
connector is for example Image object
resource is FileResource
This is a typical use case for creating a request handler:
As mentioned in the provided link:
Request handlers are useful for catching request parameters or generating dynamic content, such as HTML, images, PDF, or other content.

Visit a URL with a variable altering the url, windows phone

I have a URL that is shown below:
URL is taken out due to contract reasons
WebClient webClient = new WebClient();
Uri uri = new Uri("http://www.URLhere.aspx?stopid=4556");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler
(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
I need to find a way to alter the end part, so change 4556 from a text block, into another text so that when the application send the request it finds the whole lot.
I thought you could do this:
WebClient webClient = new WebClient();
Uri uri = new Uri("http://www.URLhere.aspx?stopid=" + stopId);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler
(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
How does one do this?
Edit:
When i do the code above it returns as a null reference, so i am asuming it's not getting the text in the textbox.
It sounds like you're missing the encoding of your variable.
var myvar = "the simpsons";
Uri myUri = new Uri("http://www.URLhere.aspx?stopid=" + HttpUtility.UrlEncode(myvar));
See HttpUtility.UrlEncode Method - MSDN
there is no difference between
Uri uri = new Uri("http://www.URLhere.aspx?stopid=4556");
and
Uri uri = new Uri("http://www.URLhere.aspx?stopid=" + stopId);
so i really don't know whats your problem
Henry,
If I understand you question correctly, I think you want to append the value in a TextBox (not a TextBlock) to the URI. If that is correct and you're employing MVVM, you could bind your TextBox to a public property on the ViewModel (lets call it TextBoxProp)
private string _textBoxProp;
public string TextBoxProp
{
get{return _textBoxProp;}
set
{
_textBoxProp = value;
RaisePropertyChanged("TextBoxProp");
}
}
and then create your URI as follows:
Uri uri = new Uri(String.Format("http://www.URLhere.aspx?stopid={0}", TextBoxProp));
Be sure when you bind to the property in XAML that you set the mode to TwoWay.
I hope that helps

portlet:renderUrl - how to make URL relative?

I have problem with http vs https link in portlets. I work with jboss liferay 6.0 but it is not relevant in this case.
Portlets may be used in http or https mode. Portlet tag creates absolute URL. I would need this relative to work correctly with http vs https.
I know secure attribute, but I don't want it always secure.
<portlet:renderURL var="detailLink">
<portlet:param name="id" value="${recordId}" />
<portlet:param name="backURL" value="${backLink}" />
</portlet:renderURL>
Please no javascript for this.
Thank you
On the server, you will know whether you are in http or http mode. So you could create it server side using something like this
PortletUrl detailLink renderResponse.createRenderUrl();
detailLink.setSecure(renderRequest.isSecure());
//set all the params on detailLink here
model.setAttribute("detailLink",detailLink);
You can use the Relative URL concept here...
If you don't want to go ahead with javascript then you can create a Java method in your jsp page, preferably in a common jsp page like init.jsp and place the following method:
<%!
private String _getRelativePath(String cURL) {
if (Validator.isNull(cURL)) {
return cURL;
}
if (cURL.startsWith(Http.HTTP)) {
int pos = cURL.indexOf(
StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
cURL = cURL.substring(pos);
}
return cURL;
}
%>
You can call this method on your url and it would return the string having path and parameters.
For example; if you pass cURL as
http://localhost:8080/web/guest/home?p_p_id=58&p_p_lifecycle=1&p_p_state=pop_up&p_p_mode=view&_58_struts_action=%2Flogin%2Flogin
then it will return
/web/guest/home?p_p_id=58&p_p_lifecycle=1&p_p_state=pop_up&p_p_mode=view&_58_struts_action=%2Flogin%2Flogin
Also you can use themeDisplay.getURLPortal() method to split absolute url to get required relative url out of it.
<%!
private String _getRelativePath(String cURL, ThemeDisplay themeDisplay) {
if (Validator.isNull(cURL)) {
return cURL;
}
String [] urlArr = cURL.split(themeDisplay.getURLPortal());
return urlArr[1];
}
%>
This mechanism will fulfill your requirement.

Resources