agleno
agleno

Reputation: 478

C# - Selenium Webdriver - Headless Browser - Unit Testing - Basics

I have been using Selenium Webdriver written in C# for a few months now and I am getting fairly capable with it. Here is my scenario in work, I am currently testing the UI for a web based browser product. The issue is I need to test many users at once and monitor the server memory etc. I have used jMeter in the past for server load testing and I would really like to use selenium to do something similar with as many users as possible.

From what I understand I might be able to achieve this using a headless browser in combination with Selenium (For anyone not aware, loading multiple browsers tends to eat your machine memory and limits you to under 6/7 browsers you can run simultaneously)

So I figure, I’ll use a headless browser and see how many I can get going at once. My research leads me to believe I can use SimpleBrowser along with selenium to achieve this.

It’s hard to find examples of the combination of the two of these. So my first question of many is, if this is a headless browser do the page elements actually exist or is it just requests that are sent between the client and the server?

For example, if I use selenium to find an element on a page I would write

IWebElement username = driver.FindElement(By.XPath("//input[@id='MainContent_txtUsername']")); username.SendKeys(userUserName);

I have written this the same for the unit test and and says it has passed. However, when I send in a password and "click" the login button I cannot get the next TestMethod to pass. i.e. after the log in I expect to find a button with the value "Change Company Password". This unit test does not pass, so it leads me to believe that it did not acutally log in.

So, my question is, in a headless browser do these elements exist at all or how does it work? If so, how do I "click" a log in button? Thanks Guys

Here is my code:

[TestMethod]
        public void AddPhoneToCartAndVerifyInCart()
        {
            //This is the placeholder to write actual code.
            Assert.AreEqual(" SPC Connect ", driver.Title);
        }
        [TestMethod]
        public void LogIn()
        {
            Assert.IsTrue((driver.FindElement(By.XPath("//input[@class='btn btn-info']")).Text).Contains("Login"));
            IWebElement username = driver.FindElement(By.XPath("//input[@id='MainContent_txtUsername']"));
            username.SendKeys(userAccount);
            IWebElement password = driver.FindElement(By.XPath("//input[@id='MainContent_txtPassword']"));
            password.SendKeys(userPassword);
            IWebElement clickSubmit = driver.FindElement(By.XPath("//input[@class='btn btn-info']"));
              clickSubmit.Click();

        }
        [TestMethod]
        public void LogInSuccess()
        {
            Assert.IsTrue((driver.FindElement(By.XPath("//input[@id='MainContent_btnChangeCompanyPassword']")).Text).Contains("Change Company Password"));

        }

Upvotes: 1

Views: 1261

Answers (1)

Alex Marculescu
Alex Marculescu

Reputation: 5770

Yes, those elements still exist in a headless browser. If you configure it correctly you shouldn't really need to alter your tests. Here's a sample (I've been using PhantomJS instead of SimpleBrowser, wiring it all up with StructureMap - but the principles should be more or less the same):

For<IWebDriver>().Use(() =>
{
    var options = new PhantomJSOptions();
    options.AddAdditionalCapability("javascriptEnabled", true);
    options.AddAdditionalCapability("unexpectedAlertBehaviour", "accept");
    options.AddAdditionalCapability("handlesAlerts", true);
    options.AddAdditionalCapability("databaseEnabled", true);
    options.AddAdditionalCapability("applicationCacheEnabled", true);
    options.AddAdditionalCapability("locationContextEnabled", true);
    options.AddAdditionalCapability("webStorageEnabled", true);
    options.AddAdditionalCapability("acceptsSslCerts", true);
    options.AddAdditionalCapability("browserConnectionEnabled", true);
    return new PhantomJSDriver(options);
});

Upvotes: 1

Related Questions