Frage

I'm trying to login to a website using Watin and I want to persist the session so I don't keep logging in every time.

However, each time I call LogIn the line if (browser.ContainsText(isLoggedInClass)) never finds the content which I can see in the open browser.

I've added the line string checkHtmltoSeeIfClassExists = browser.Body.Parent.OuterHtml; to physically check the string is contained in the html which it is.

I'm a bit stuck as it seems Watin just doesn't persist the session? Has anyone got any ideas what's wrong?

The code I have so far is below

using WatiN.Core;

namespace ProjectXYZ
{
    class Navigate
    {
        private IE browser;

        public void LogIn()
        {
            const string isLoggedInClass = "gwt-Hyperlink optionLink optionLink_myAccount";
            if (browser == null)
                browser = new IE("https://www.somewebsite.com");

            string checkHtmltoSeeIfClassExists = browser.Body.Parent.OuterHtml;

            if (browser.ContainsText(isLoggedInClass))
            {
                string test = "class found!";
            }
            else
            {
                browser.TextField(Find.ByName("username")).Value = xx.un;
                browser.TextField(Find.ByName("userpass")).Value = xx.pw;
                browser.Button(Find.ByTitle("Login")).Click();
            }

        }

    }

}
War es hilfreich?

Lösung

The mechanism you want is the AttachTo<> function built into the WatiN Framework. Since WatiN only supports a single-threaded apartment state, you have to manually manage your session(s).

The link above has a code example for using 'AttachTo<>' but, for the sake of being thorough, I have provided one below as well.

var IE = IE.AttachTo<IE>(Find.ByUrl(someURLHere));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top