How to check ValidPage in CodedUI - coded-ui-tests

I am using Code first method instead of record method. I have login page from which i am moving to next page that is home page.
public HomePage SubmitClick(string userName, string Password)
{
HtmlEdit txtUsername = new HtmlEdit(_browserWindow);
txtUsername.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "txt_empid");
txtUsername.Text = userName;
HtmlEdit txtPassword = new HtmlEdit(_browserWindow);
txtPassword.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "txt_password");
txtPassword.Text = Password;
HtmlInputButton btnSubmit = new HtmlInputButton(_browserWindow);
btnSubmit.SearchProperties.Add(HtmlInputButton.PropertyNames.Id, "btn_submit");
Mouse.Click(btnSubmit);
return new HomePage(_browserWindow);
}
Redirecting is working fine. My question is how to identify if the open page is valid or not.
What i am planning is to check for some controls like button and hyperlinks if they exist then page is valid. Is it a good method.

First give _browserwindow.WaitForControlReady() it will wait till the browserwindow completely loads the page.
Once the page is loaded use Assert to check for the title of the page whether it is the same as expected.
_browserwindow.WaitForControlReady();
WinTabPage nextPage = new WinTabPage(_browserwindow);
Assert.AreEqual("Home Page", nextPage.Name);

Compare the URL of the next Page.If its the URL thats required then the test case passes.
Uri url = BrowserwindowObj.Uri;
This gives the URL of the current browser Page.

Related

How to I program a web-browser to perform a series of actions?

EDIT: I'm not looking for Facebook APIs! I'm simply using Facebook as an example. I intend to get my browser to perform actions on different websites that likely have no APIs.
Let's say I wish to create a program that will log into Facebook, lookup my friends list, visit each one of their profiles, extract the date + text of each post and write this to a file.
I have an idea how the algorithm should work. But I have absolutely no clue how to interface my code with the browser itself.
Now I'm a Java programmer, so I would very much imagine the pesudo code in Java would be to create a Browser Object then convert the current page's contents to HTML code so that the data can be parsed. I provided an example code below of what I think it ought to look like.
However is this the right way that I should be doing it? If it is, then where can I find a web browser object? Are there any parsers I can use to 'read' the content? How do I get it to execute javascript such as clicking on a 'Like' button?
Or are there other ways to do it? Is there a GUI version and then I can simply command the program to go to X/Y pixel position and click on something. Or is there a way to write the code directly inside my FireFox and run it from there?
I really have no clue how to go about doing this. Any help would be greatly appreciated! Thanks!
Browser browser = new Browser();
browser.goToUrl("http://facebook.com");
//Retrieve page in HTML format to parse
HtmlPage facebookCom = browser.toHtml();
//Set username & password
TextField username = facebookCom.getTextField("username");
TextField password = facebookCom.getTextField("password");
username.setText("user123");
password.setText("password123");
facebookCom.updateTextField("username", username);
facebookCom.updateTextField("password", password);
//Update HTML contents
browser.setHtml(facebookCom);
// Click the login button and wait for it to load
browser.getButton("login").click();
while (browser.isNotLoaded()) {
continue;
}
// Click the friends button and wait for it to load
browser.getButton("friends").click();
while (browser.isNotLoaded()) {
continue;
}
//Convert the current page (Friends List) into HTML code to parse
HtmlPage facebookFriends = browser.toHtml();
//Retrieve the data for each friend
ArrayList<XMLElement> friendList = facebookFriends.getXmlElementToArray("friend");
for (XMLElement friend : friendList) {
String id = friend.getId();
//Visit the friend's page
browser.goToUrl("http://facebook.com/" + id);
while (browser.isNotLoaded()) {
continue;
}
//Retrieve the data for each post
HtmlPage friendProfile = browser.toHtml();
ArrayList<XMLElement> friendPosts = friendProfile.getXmlElementToArray("post");
BufferedWriter writer = new BufferedWriter(new File("C:/Desktop/facebook/"+id));
//Write the date+text of every post to a text file
for (XMLElement post : friendPosts) {
String date = post.get("date");
String text = post.get("text");
String content = date + "\n" + text;
writer.append(content);
}
}
I think you are thinking about this the wrong way. You wouldn't really want to write a program to scrap the screen via the browser. It looks like you could take advantage of facebooks rest api and query for the data you are looking for. A link to get a users posts via rest api:
https://developers.facebook.com/docs/graph-api/reference/v2.6/user/feed
You could get their users id's from this endpoint:
https://developers.facebook.com/docs/graph-api/reference/friend-list/
Then plug the user ids into the first rest endpoint that was linked. Once you get your data coming back correctly via the rest api its fairly trivial to write that data out to a file.

Does Redirection to public page from search result is property of search portlet?

I used search portlet in my portal. And when i click on the search result always it redirects to result's public page. E.g: I searched for a site through site name. And the result comes. When i click on the result it redirect always redirect me to site's public page, even i am the member of the site.
I want when if user is the member of the site, it must be redirected to the site's private page.
You can hook the search portlet. In the main_search_result_form.jsp you can change the URL to your private page.
Make changes in the below lines.
viewFullContentURL = _getViewFullContentURL(request, themeDisplay, PortletKeys.ASSET_PUBLISHER, document);
viewFullContentURL.setParameter("struts_action", "/asset_publisher/view_content");
if (Validator.isNotNull(returnToFullPageURL)) {
viewFullContentURL.setParameter("returnToFullPageURL", returnToFullPageURL);
}
viewFullContentURL.setParameter("assetEntryId", String.valueOf(assetEntry.getEntryId()));
viewFullContentURL.setParameter("type", assetRendererFactory.getType());
if (Validator.isNotNull(assetRenderer.getUrlTitle())) {
if ((assetRenderer.getGroupId() > 0) && (assetRenderer.getGroupId() != scopeGroupId)) {
viewFullContentURL.setParameter("groupId", String.valueOf(assetRenderer.getGroupId()));
}
viewFullContentURL.setParameter("urlTitle", assetRenderer.getUrlTitle());
}

how to get home page url in orchard cms

I am building module to register customers and after registration i need to redirect the user to home page (default). I cant see a way as in Orchard everything works as content items.
Some of my code from Controller is given below
$ if (!ModelState.IsValid)
return new ShapeResult(this, _services.New.Checkout_Signup(Signup: signup));
var customer = _customerService.CreateCustomer(signup.Email, signup.Password);
customer.FirstName = signup.FirstName;
customer.LastName = signup.LastName;
customer.Title = signup.Title;
_authenticationService.SignIn(customer.User, true);
return Redirect("~/Home Page URL here...");
In Orchard, the home page has an empty string for its alias. It's possible to look up the RouteValueDictionary of an alias by calling the IAliasService.Get() method. Once you have this, you can simply pass it to RedirectToRoute().
So for the home page:
var homepage = _aliasService.Get(String.Empty);
return RedirectToRoute(homepage);
You can see Orchard using this mechanism to check the home page in the AutoroutePartDriver.cs file lines 66 - 72 in version 1.7.2.

Passing value of query string in multiple views MVC3

I am successfully redirecting user after Login to a page called CustomerStart. After that query string (url) looks fine with the ssn like this
localhost:1234/CustomerStart?ssn=850406-0297
I am doing this by writing this
return RedirectToAction("Index", "CustomerStart", new {ssn = model.ssn});
But when I click a button which has been defined in the _layout like this,
<li>#Html.ActionLink("CustomerStart","Index", "CustomerStart")</li>
my query string gets empty.
I want to keep my customer to be in the same CustomerStart page both after login or if they click the CustomerStart button.
I also want to send my customer in different views when they click any of the button under the above mentioned one.
like this,
<li>#Html.ActionLink("CustomerStart","Index", "CustomerStart")</li>
<li>#Html.ActionLink("Bill","Index", "Bill")</li>
So, i need to grab the value from the query string always. But I dont know how?
I am new in MVC3 and I am totally lost.
try
<li>#Html.ActionLink("CustomerStart","Index", "CustomerStart",
new { ssn=Request.QueryString["ssn"]},
null)
</li>
<li>#Html.ActionLink("Bill","Index", "Bill",
new { ssn=Request.QueryString["ssn"]},
null)
</li>

Sharepoint List redirect with new id

I have a list within Sharepoint, using a custom new form I have added a custom list form control ("New item form" for the list) and changed the SaveButton to a standard input HTML button, and added an 'onclick' event that is as follows:
onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={NewFormWizard2.aspx?id=}')}"
This works as in saves the data and redirects to the NewFormWizard2.aspx?id= page.
How do I get the ID of the created item to be passed to the redirected page?
Thus once the form is completed it would redirect to NewFormWizard2.aspx?id=23
jtherkel was close, but was missing a '}' on the end of the redirect url. I used an extra concat below
<input type="button" value="Submit" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={lists/MyListName/DispForm.aspx?ID=',/dsQueryResponse/Rows/Row/#ID,'}'))}" />
I am not sure where the ID will exist on the page you host the Javascript from. Does it appear in the querystring or on a field on the page?
There is nothing in the request or response that will identify the item. I have had this issue when doing some web loadtesting.
I can only suggest that your create the item using the webservices as that at gives you some return xml.
This answer does not solve the "new form" issue, but it might help others with the syntax for screens that contain existing list items.
I tested this quickly in my SharePoint (MOSS 2007) environment.
onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={NewFormWizard2.aspx?id=',/dsQueryResponse/Rows/Row/#ID))}"
The concat syntax is an XSLT instruction that tells the processor to combine the values enclosed in single quotes. I adapted this answer from info I found here.
Loading Values in a Custom List Form
http://wssdevelopment.blogspot.com/2007_04_01_archive.html
I hope this would be helpfull:
1- In SharePoint Designer create new page, call it for example "LastItem.aspx" and place a dataview on it with a single form view for the destination list item.
2-Limit paging to just one record, set the sorting by ID and descending and filter the list to just show item which is created by [current user].
3-Now you do not need to pass any query string to this page. just replace the default "OK" button in NewForm.aspx of the list with a standard HTML input button and add this to its definition "onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={LastItem.aspx}". After submitting a new item to list you will be redirected to an edit view of the created item.
You can do the same for save button in LastItem.aspx to redirect to some other page after clicking on save button.
found an approach using pure javascript (JQuery) and the SPAPI code from http://darrenjohnstone.net/.
The list contains two fields, title and BodyCopy
I've thewn created a form that asks for a title and a question, both text fields, then the submit button calls the following function: (note that ServerAddress and LIST_question need to be updated to your own details).
The function then uploads the details using the SOAP service within LISTS.ASMX and using the response gets the ID of the new item and redirects the page.
var LIST_question = '{xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}';
var ServerAddress = 'http://xxx/';
function submitQuestion()
{
var title = new String($("#title").val());
var t = new String($("#question").val());
t=t.trim();
if(t=="")
return;
title=title.trim();
if(title=="")
return;
var lists = new SPAPI_Lists(ServerAddress) ;
//
var newItem = { Title : title, BodyCopy : t};
var items = lists.quickAddListItem(LIST_question, newItem);
var id=-1;
if (items.status == 200)
{
var rows = items.responseXML.getElementsByTagName('z:row');
if(rows.length ==1)
{
var r = rows[0];
var id = r.getAttribute('ows_ID');
window.location.href='DispForm.aspx?ID='+id;
}
else
{
alert("Error: No row added");
}
}
else
{
alert('There was an error: ' + items.statusText);
return;
}
}
You can achieve this using JavaScript http://www.sharepointdrive.com/blog/Lists/Posts/Post.aspx?ID=9

Resources