Link object persisting across multiple pages within one browser instance using WatiN - c#

I'm just getting started with WatiN and attempting to test a large number of pages with authentication. I've taken the approach of only creating a new instance of IE each time new login details are required. Once authenticated, the framework needs to navigate to 2 pages on the site and click a link on each to download a file (repeated multiple times within one authenticated session for different clients).
Navigating to the pages is fine and the download is working with IE9 using a combination of WatiN and SendKeys(). However, when it navigates to the second page and attempts to find the Link object by Text (which has the same text as on the previous page) it returns the download URL from the first page. This means that essentially whatever page I direct WatiN to, it still seems to be persisting the Link object from the first page.
The first method creates my browser object and returns it to the parent class:
public IE CreateBrowser(string email, string password, string loginUrl)
{
Settings.MakeNewIe8InstanceNoMerge = true;
Settings.AutoCloseDialogs = true;
IE ie = new IE(loginUrl);
ie.TextField(Find.ById("Email")).TypeText(email);
ie.TextField(Find.ById("Password")).TypeText(password);
ie.Button(Find.ById("btnLogin")).Click();
Thread.Sleep(1500);
return ie;
}
I then iterate through logins, passing the URL for each required page to the following:
public void DownloadFile(IE ie, string url)
{
//ie.NativeBrowser.NavigateTo(new Uri(url));
ie.GoTo(url);
Thread.Sleep(1000);
//TODO: Why is link holding on to old object?
Link lnk = null;
lnk = ie.Link(Find.ByText("Download file"));
lnk.WaitUntilExists();
lnk.Focus();
lnk.Click();
//Pause to allow IE file dialog box to show
Thread.Sleep(2000);
//Alt + S to save
SendKeys.SendWait("%(S)");
}
The calling method ties it all together like so (I've obfuscated some of the details):
for (int i = 0; i < loginCount; i++)
{
using (IE ie = HelperClass.CreateBrowser(lLogins[i].Email, lLogins[i].Password, ConfigurationManager.AppSettings["loginUrl"]))
{
...Gets list of clients we're wanting to check
for (int j = 0; j < clientCount; j++)
{
string url = "";
switch ()
{
case "Page1":
string startDate = "20110831";
string endDate = "20110901";
url = String.Format(page1BaseUrl, HttpUtility.UrlEncode(lClients[j].Name), startDate, endDate);
break;
case "Page2":
url = String.Format(page2BaseUrl, HttpUtility.UrlEncode(lClients[j].Name));
break;
}
HelperClass.DownloadFile(ie, url);
}
}
}
Does anyone have any idea what could be causing this or how to get around it? Do I need to create a new IE object for each request?

Okay, so I've managed to find out what was causing my Link object (and the parent Page object) to persist across multiple URLs.
It seems that because I'm clicking the Link which forces the "Save As" box in IE9, this keeps the Page object current, even as the browser runs through all the other URLs in the background. This seems to update the HTML rendered in the window but not release the existing Page object (or possibly creates additional Page objects in memory).
Because I'm using SendKeys() to hit the "Save" button, rather than a handled dialog in WatiN, the dialog stays open and persists the Page object.
From the looks of things, I need to find a different, handled way of performing my file downloads/saving.

Related

C# IE11 Automation - Cannot Connect To Open IE Window

I'm trying to connect to an Internet Explorer window that is already open. Once connected I need to send some keystrokes (via SendKeys) to the IE window for some processing. I've got the following code below that works up until the SendKeys command. It finds the IE window titled "Graphics Database". When it hits "SendKeys.Send("{TAB}");" I get the error "An unhandled exception of type 'System.NullReferenceException' occurred".
Additional information: I also get the following on the NullReferenceException error. The weird thing is if I code to open a new IE window and then use SendKeys it works fine. Connecting to an existing windows seems to cause this issue.
SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.
Can anyone please help me figure out what to do to fix this?
Andy
InternetExplorer IE = null;
// Get all browser objects
ShellWindows allBrowsers = new ShellWindows();
if (allBrowsers.Count == 0)
{
throw new Exception("Cannot find IE");
}
// Attach to IE program process
foreach (InternetExplorer browser in allBrowsers)
{
if (browser.LocationName == "Graphics Database")
{
MessageBox.Show ("Found IE browser '" + browser.LocationName + "'");
IE = (InternetExplorer)browser;
}
}
IE.Visible = true;
System.Threading.Thread.Sleep(2000);
SendKeys.Send("{TAB}");
SendKeys.Send("G1007");
SendKeys.Send("{ENTER}");
I was able to resolve this issue. I could never get the IE.Visible = true to work. This seemed to do nothing in my code. I had to use the SetForegroundWindow() to set the focus to the IE window.
// Find the IE window
int hWnd = FindWindow(null, "Graphics Database - Internet Explorer");
if (hWnd > 0) // The IE window was found.
{
// Bring the IE window to the front.
SetForegroundWindow(hWnd);
This site helped me immensely with getting the SetForegroundWindow() working.
http://forums.codeguru.com/showthread.php?460402-C-General-How-do-I-activate-an-external-Window
Andy please bear with me as this will be long. First you are going to want to look mshtml documentation and Dom. https://msdn.microsoft.com/en-us/library/aa741314(v=vs.85).aspx I don't know why automation is so convoluted but it is. The UIautomation class works great for windows apps but has nothing really for IE that I've been able to find. Others will point to third parties like waitn and selenium. Waitn appears to no longer be supported and selenium won't let you grab an open IE browser. I have gone down this path recently because I wanted to be able to create an app to store my web passwords and auto fill them in since I can't save my username and passwords in browser due to security restrictions. I have an example here and hope it helps. First open up IE and navigate to http://aavtrain.com/index.asp. Then have a console project with mshtml referenced and shdocvw. Here is code below. It gets the window then finds elements for username, password, and submit. then populates the username and password and clicks the submit button. I don't have a login to this site so it won't log you in. I have been using it for my testing. Problem I have is sites with javascript login forms. If you get further with this info please post back as I am still trying to evolve the concepts and create something reusable.
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
Console.WriteLine("Starting Search\n\n\n");
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
if (ie.LocationURL.Contains("aavtrain"))
{
Console.WriteLine(ie.LocationURL);
Console.WriteLine("\n\n\n\n");
Console.WriteLine("FOUND!\n");
mshtml.HTMLDocument document = ie.Document;
mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");
Console.WriteLine("AutofillPassword");
foreach (mshtml.IHTMLInputElement i in elCol)
{
i.defaultValue = "John";
}
foreach (mshtml.IHTMLInputElement i in elCol2)
{
i.defaultValue = "Password";
}
Console.WriteLine("Will Click Button in 2 seconds");
Thread.Sleep(2000);
foreach (mshtml.HTMLInputButtonElement i in elCol3)
{
i.click();
}
}
}
Console.WriteLine("Finished");

disable IE visibility while using WatiN

I use watin, because I need to open some websites in the background for which the user needs to support Javascript. I don't know if WatiN is the best for this job, but at the moment it takes very long until Internet Explorer gets visible. I need to disable to popping up of Internet Explorer while using WatiN. User doesn't need to see the opening of sites. Is it possible while using WatiN to visit a website without showing it the user or should I use another alternative which supports JS on client side?
My code at the moment;
public static void visitURL()
{
IE iehandler = new IE("http://www.isjavascriptenabled.com");
if (iehandler.ContainsText("Yes"))
Console.WriteLine("js on");
else
Console.WriteLine("js off");
}
The WatIn.Core.IE class has a Visible property, you can initialize the object like that:
new WatiN.Core.IE() { Visible = true }
This way the IE will just blink on the screen when it's created, and then it will get hidden. You can later control the visibility of the IE with the ShowWindow method of WatiN.Core.IE class - I mean you can show it on the screen if you need, or you can hide again.
I use exactly that trick (of hiding IE) for writing UnitTests (using https://github.com/o2platform/FluentSharp_Fork.WatiN) that run in an hidden IE window
For example here is how I create a helper class (with an configurable hidden value)
public IE_TeamMentor(string webRoot, string path_XmlLibraries, Uri siteUri, bool startHidden)
{
this.ie = "Test_IE_TeamMentor".popupWindow(1000,700,startHidden).add_IE();
this.path_XmlLibraries = path_XmlLibraries;
this.webRoot = webRoot;
this.siteUri = siteUri;
}
which is then consumed by this test:
[Test] public void View_Markdown_Article__Edit__Save()
{
var article = tmProxy.editor_Assert() // assert the editor user (or the calls below will fail due to security demands)
.library_New_Article_New() // create new article
.assert_Not_Null();
var ieTeamMentor = this.new_IE_TeamMentor_Hidden();
var ie = ieTeamMentor.ie;
ieTeamMentor.login_Default_Admin_Account("/article/{0}".format(article.Metadata.Id)); // Login as admin and redirect to article page
var original_Content = ie.element("guidanceItem").innerText().assert_Not_Null(); // get reference to current content
ie.assert_Has_Link("Markdown Editor")
.link ("Markdown Editor").click(); // open markdown editor page
ie.wait_For_Element_InnerHtml("Content").assert_Not_Null()
.element ("Content").innerHtml()
.assert_Is(original_Content); // confirm content matches what was on the view page
var new_Content = "This is the new content of this article".add_5_RandomLetters(); // new 'test content'
ie.element("Content").to_Field().value(new_Content); // put new content in markdown editor
ie.button("Save").click(); // save
ie.wait_For_Element_InnerHtml("guidanceItem").assert_Not_Null()
.element ("guidanceItem").innerHtml()
.assert_Is("<P>{0}</P>".format(new_Content)); // confirm that 'test content' was saved ok (and was markdown transformed)
ieTeamMentor.close();
}
Here are a number of posts that might help you to understand how I use it:
https://github.com/TeamMentor/Dev/tree/master/Source_Code/TM_UnitTests/TeamMentor.UnitTests.QA/TeamMentor_QA_IE
http://blog.diniscruz.com/2014/07/how-to-debug-cassini-hosted-website-and.html
http://blog.diniscruz.com/2014/07/using-watin-and-embedded-cassini-to-run.html
http://blog.diniscruz.com/search/label/WatiN

Change Background image every visit

I have about 50 background images for my site. What i am looking to do is randomly present the user with a different one for every visit. By this i mean they will surf through the site with the same background image during their visit.
After they close the browser and re-visit or come back and visit later, they then are presented with a new random background image. Don't need to save anything on what their previous background image was, just a random new one for each new visit to the site.
Not sure it this can be done with C#, Javascript, JQuery or CSS.
EDIT: I am using ASP.net 4.0 C# for my web app. Thanks
Don't use cookies as stated in the comments. This will only add extra bandwidth to the header messages sent to the server.
Instead, use local storage in the browser to save what the last image was they used. When a new session is started increment this value, and display the next image.
I've used jStorage on projects and it works fine.
You can save the currently shown image in their browsers storage, and maybe a session ID. Later, you can check if the session ID has changed. If so, then change to a different image.
var image = $.jStorage.get("image", 0);
var session_id = $.jStorage.get("session", "put current session id here");
if(session_id != "current session id")
{
image = (image < 50) ? 0 : image+1;
$.jStorage.set("image",image);
$.jStorage.set("session","current session id");
}
// use image to set background
EDIT:
Don't place this JavaScript in each web page. Instead, place it in a ASP.NET page that responses as a Javascript content type and load it via the page's header. This way page caching on the browser won't affect the script when the session changes.
Keep it in the Session. Pick it at random when it's not already in the session, it will stay the same as long as they're at your site -- and next time they come back, they'll get a new one.
For example (my C# is a little rusty):
public getBackground (HttpSessionState session) {
String bg = (string) session["session.randomBG"];
if (bg == null) {
// pick a random BG & store it.
bg = "pick one";
session["session.randomBG"] = bg;
}
return bg;
}
Hope this helps!
var list = [
"/images01.png",
"/images02.png",
...
];
/*background url*/ = list[Math.floor(Math.random()*list.length];
Sure it is possible. I will use pseudo-code here to show you how it could be done. Surely soon examples in Java will appear.
In the beginning of each page:
StartSession()
If ! SessionVariable[myBackground] then
x=Randomize 50
SessionVariable[myBackground]="image0" + x + ".jpg"
endif
<style>
body {background-image:url(SessionVariable[myBackground];}
</style>
Make sure you use the style tag where appropriate. The SessionVariable[myBackground] is user-created. In PHP it would look like this:
$_SESSION['myBackground']
Best wishes,
Try this function:
/**
* Change background image hourly.
* Name your images with 0.jpg, 1.jpg, ..., 49.jpg.
*/
function getBackground2() {
var expires = 3600000,
numOfImages = 50,
seed = Math.round(Date.now() / expires % numOfImages);
return '/path/to/background/' + seed + '.jpg';
}

How to display contents of a listbox as soon as an item is added?

I have an application where i need to display a list of string outputs from a function in a System.Web.UI.ListBox. To get one string output (executing the function) it takes some time .I am adding the string output to the listbox after it is produced.Whats is my problem is that the application waits the entire time (till all items are calculated and added to listbox) and finally displays the entire listbox in one go (after waiting for quite a while due to the time required for generating large number of output string). Kindly someone help me to show the listox item on the page as soon as it is added.Thank you.
public void add_to_list_box()
{
int b;
for (b = 0; b <= 20; b++)
{
string message = demo(b);
ListBox1.Items.Add(message);
}
Button2.Text = "completed";
}
You have to understand that your code that is filling the ListBox is running on the web server, and it generates html / css / etc. After whole page is generated, it's send to the client. In your case client is Web browser, and after page is generated, send to the client only then is displayed.
In order to achieve your that instant display you have to use Ajax
Use Ajax. In the url put the add_to_list_box() function, and then in the success part of the Ajax, do this:
Button2.Text = "completed";

Webbrowser control is not showing Html but shows webpage

I am automating a task using webbrowser control , the site display pages using frames.
My issue is i get to a point , where i can see the webpage loaded properly on the webbrowser control ,but when it gets into the code and i see the html i see nothing.
I have seen other examples here too , but all of those do no return all the browser html.
What i get by using this:
HtmlWindow frame = webBrowser1.Document.Window.Frames[1];
string str = frame.Document.Body.OuterHtml;
Is just :
The main frame tag with attributes like SRC tag etc, is there any way how to handle this?Because as i can see the webpage completely loaded why do i not see the html?AS when i do that on the internet explorer i do see the pages source once loaded why not here?
ADDITIONAL INFO
There are two frames on the page :
i use this to as above:
HtmlWindow frame = webBrowser1.Document.Window.Frames[0];
string str = frame.Document.Body.OuterHtml;
And i get the correct HTMl for the first frame but for the second one i only see:
<FRAMESET frameSpacing=1 border=1 borderColor=#ffffff frameBorder=0 rows=29,*><FRAME title="Edit Search" marginHeight=0 src="http://web2.westlaw.com/result/dctopnavigation.aspx?rs=WLW12.01&ss=CXT&cnt=DOC&fcl=True&cfid=1&method=TNC&service=Search&fn=_top&sskey=CLID_SSSA49266105122&db=AK-CS&fmqv=s&srch=TRUE&origin=Search&vr=2.0&cxt=RL&rlt=CLID_QRYRLT803076105122&query=%22LAND+USE%22&mt=Westlaw&rlti=1&n=1&rp=%2fsearch%2fdefault.wl&rltdb=CLID_DB72585895122&eq=search&scxt=WL&sv=Split" frameBorder=0 name=TopNav marginWidth=0 scrolling=no><FRAME title="Main Document" marginHeight=0 src="http://web2.westlaw.com/result/dccontent.aspx?rs=WLW12.01&ss=CXT&cnt=DOC&fcl=True&cfid=1&method=TNC&service=Search&fn=_top&sskey=CLID_SSSA49266105122&db=AK-CS&fmqv=s&srch=TRUE&origin=Search&vr=2.0&cxt=RL&rlt=CLID_QRYRLT803076105122&query=%22LAND+USE%22&mt=Westlaw&rlti=1&n=1&rp=%2fsearch%2fdefault.wl&rltdb=CLID_DB72585895122&eq=search&scxt=WL&sv=Split" frameBorder=0 borderColor=#ffffff name=content marginWidth=0><NOFRAMES></NOFRAMES></FRAMESET>
UPDATE
The two url of the frames are as follows :
Frame1 whose html i see
http://web2.westlaw.com/nav/NavBar.aspx?RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=
Frame2 whose html i do not see:
http://web2.westlaw.com/result/result.aspx?RP=/Search/default.wl&action=Search&CFID=1&DB=AK%2DCS&EQ=search&fmqv=s&Method=TNC&origin=Search&Query=%22LAND+USE%22&RLT=CLID%5FQRYRLT302424536122&RLTDB=CLID%5FDB6558157526122&Service=Search&SRCH=TRUE&SSKey=CLID%5FSSSA648523536122&RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=
And the properties of the second frame whose html i do not get are in the picture below:
Thank you
I paid for the solution of the question above and it works 100 %.
What i did was use this function below and it returned me the count to the tag i was seeking which i could not find :S.. Use this to call the function listed below:
FillFrame(webBrowser1.Document.Window.Frames);
private void FillFrame(HtmlWindowCollection hwc)
{
if (hwc == null) return;
foreach (HtmlWindow hw in hwc)
{
HtmlElement getSpanid = hw.Document.GetElementById("mDisplayCiteList_ctl00_mResultCountLabel");
if (getSpanid != null)
{
doccount = getSpanid.InnerText.Replace("Documents", "").Replace("Document", "").Trim();
break;
}
if (hw.Frames.Count > 0) FillFrame(hw.Frames);
}
}
Hope it helps people .
Thank you
For taking html you have to do it that way:
WebClient client = new WebClient();
string html = client.DownloadString(#"http://stackoverflow.com");
That's an example of course, you can change the address.
By the way, you need using System.Net;
This works just fine...gets BODY element with all inner elements:
Somewhere in your Form code:
wb.Url = new Uri("http://stackoverflow.com");
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbDocumentCompleted);
And here is wbDocumentCompleted:
void wb1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var yourBodyHtml = wb.Document.Body.OuterHtml;
}
wb is System.Windows.Forms.WebBrowser
UPDATE:
The same as for the document, I think that your second frame is not loaded at the time you check for it's content...You can try solutions from this link. You will have to wait for your frames to be loaded in order to see its content.
The most likely reason is that frame index 0 has the same domain name as the main/parent page, while the frame index 1 has a different domain name. Am I correct?
This creates a cross-frame security issue, and the WB control just leaves you high and dry and doesn't tell you what on earth went wrong, and just leaves your objects, properties and data empty (will say "No Variables" in the watch window when you try to expand the object).
The only thing you can access in this situation is pretty much the URL and iFrame properties, but nothing inside the iFrame.
Of course, there are ways to overcome teh cross-frame security issues - but they are not built into the WebBrowser control, and they are external solutions, depending on which WB control you are using (as in, .NET version or pre .NET version).
Let me know if I have correctly identified your problem, and if so, if you would like me to tell you about the solution tailored to your setup & instance of the WB control.
UPDATE: I have noticed that you're doing a .getElementByTagName("HTML")(0).outerHTML to get the HTML, all you need to do is call this on the document object, or the .body object and that should do it. MyDoc.Body.innerHTML should get the the content you want. Also, notice that there are additional iFrames inside these documents, in case that is of relevance. Can you give us the main document URL that has these two URL's in it so we / I can replicate what you're doing here? Also, not sure why you are using DomElement but you should just cast it to the native object it wants to be cast to, either a IHTMLDocument2 or the object you see in the watch window, which I think is IHTMLFrameElement (if i recall correctly, but you will know what i mean once you see it). If you are trying to use an XML object, this could be the reason why you aren't able to get the HTML content, change the object declaration and casting if there is one, and give it a go & let us know :). Now I'm curious too :).

Categories