3

I've been playing with the Selenium C# framework and been trying to do a facebook login, but without any luck.

This is what i got so far (based on this post: Testing a Facebook Connect application using Selenium?). I can't get it to work though.

ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com");
public void LoginWithEmailAndPassword(string email, string pass)
{
    sel.Start();
    sel.Open("http://www.facebook.com");
    sel.ClickAt("//img[\\@alt='Facebook']", "User clicks on Facebook Login");
    sel.WaitForPopUp("", "3000");
    sel.SelectPopUp("");
    sel.Type("email", email);
    sel.Type("pass", pass);
    sel.KeyPress("pass", "\\13");
    sel.SelectWindow("null");
}

So if someone could provide me with some sample code for how to do this, or guide me in the right direction, it would be appreciated.

Resolved

By using the record feature of the Firefox Selenium plugin I got the id's and the functions i needed to get it working. The login button seemed to require an XPath definition, which I also got from the Selenium IDE.

ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com");
public void LoginWithEmailAndPassword(string email, string pass)
{
    sel.Start();
    sel.Open("/");
    sel.Type("id=email", email);
    sel.Type("id=pass", pass);
    sel.Click("//label[@id='loginbutton']/input");
}
Community
  • 1
  • 1
David
  • 101
  • 1
  • 3
  • 15
  • What errors are you getting? Does that even compile? – iCollect.it Ltd Jun 11 '12 at 11:12
  • It builds. Should have mentioned that I'm testing the dll through the FitNesse framework. It throws an TargetInvocationException Selenium.SeleniumException: ERROR: Invalid xpath [2]: //img[\@alt='Facebook']. – David Jun 11 '12 at 11:18
  • When I removed the backslashes from the XPath, it no longer throws an exception. Facebook now loads in a browser window. But it doesn't login and no exceptions or other errors are shown. – David Jun 11 '12 at 11:47
  • 1
    Facebook uses SSL, so you should also change your Uri to https://www.facebook.com And change the sel.Open to sel.Open("/"). Or use the selenium IDE to record your tests – Preben Huybrechts Jun 11 '12 at 12:30
  • Didn't know about the recording feature. That simplified things. Thanks Preben. – David Jun 11 '12 at 13:36
  • Why are you using Selenium RC as opposed to Selenium WebDriver? Also beware that the IDE can produce quite unmaintainable code. – Arran Jun 11 '12 at 20:51
  • Selenium RC was part of a FitNesse tutorial i am following. Unmaintaintable in what aspect? Outdated element id's and such? – David Jun 12 '12 at 05:46
  • It mainly becomes unmaintainable when you try using complicated CSS and/or XPath selectors because the IDE isn't very clever when it comes to this. For example, it may give you a horrific long XPath solution where a better, more simple, more concise solution can be used. – Arran Jun 12 '12 at 20:40

1 Answers1

1

Try use WebDriver:

IWebDriver driver = new FireFoxDriver();
driver.GoToUrl("www.facebook.com");

var element = driver.FindElement(By.Id("email"));
element.SendKeys(email);
...
element = driver.FindElement(By.Id("submit button id"));
element.Click();

This link could help you get start.

Frigik
  • 449
  • 1
  • 3
  • 13