Azure WebJob, Selenium Webdriver and BrowserStack

Azure WebJob, Selenium Webdriver and BrowserStack

by No Comments

As Vladimir Vladimirovich once said:

"We discussed this very important issue yesterday over a beer"

and yes, we decided to use Azure WebJobs, Selenium and Browserstack to try to run simple Automation Tests.

Browserstack enables us to test web app across various browsers on different operating systems and mobile devices, offers reports, screenshots and videos. WebJobs allows you to run programs or scripts in background, on schedule without additional costs. Having that in mind this tools together may be very convenient choice for a small regression tests or a smoke tests.

For start lets create new WebJob project from the VS2015 named webJobBrowserstack.

Add Selenium references via Packege Manager Cosole and we are ready to start writing some tests.

Install-Package Selenium.WebDriver
Install-Package Selenium.Support
Install-Package Selenium.RC

After creating account, Browserstack will provide you Username and Access Key for Automate. Add it to the code below:

static void Main()
        {
            var host = new JobHost();
            IWebDriver browser;

	    //Set Capabilities
	    DesiredCapabilities capability = DesiredCapabilities.Safari();
            capability.SetCapability("browserstack.user", "aleksand");
            capability.SetCapability("browserstack.key", "xxxxxx");

            //Run
            browser = new RemoteWebDriver(
                new Uri("http://hub.browserstack.com/wd/hub/"), capability
            );
            browser.Navigate().GoToUrl("http://nultien.rs/");
            
            //Quit driver and dispose job
            browser.Quit();
            host.Dispose();
        }

For this test we are going to use Safari on latest OS X.

capability.SetCapability("browser", "Safari");
capability.SetCapability("browser_version", "9.0");
capability.SetCapability("os", "OS X");
capability.SetCapability("os_version", "El Capitan");
capability.SetCapability("resolution", "1600x1200");

After reorganizing the code your Program class should looks like:

using System;
using Microsoft.Azure.WebJobs;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace webJobBrowserstack
{
    // To learn more about Microsoft Azure WebJobs SDK, please see http://go.microsoft.com/fwlink/?LinkID=320976
    class Program
    {
        private static IWebDriver browser;
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        static void Main()
        {
            var host = new JobHost();
            Program test = new Program();

            //Test Steps
            test.SetUp();
            test.FindBlogLinkFromNavigationAndClickIt();
            test.CountArticles();
            
            //Quit driver and dispose job
            browser.Quit();
            host.Dispose();

        }
        public void SetUp()
        {
            //Set Capabilities for Safari
            DesiredCapabilities capability = DesiredCapabilities.Safari();
            capability.SetCapability("browserstack.user", "aleksandar");
            capability.SetCapability("browserstack.key", "xxxxxx");
            capability.SetCapability("browser", "Safari");
            capability.SetCapability("browser_version", "9.0");
            capability.SetCapability("os", "OS X");
            capability.SetCapability("os_version", "El Capitan");
            capability.SetCapability("resolution", "1600x1200");
            browser = new RemoteWebDriver(
                new Uri("http://hub.browserstack.com/wd/hub/"), capability
            );
        }

        public void FindBlogLinkFromNavigationAndClickIt()
        {
            browser.Navigate().GoToUrl("http://nultien.rs/");
            var blogLink = browser.FindElement(By.LinkText("BLOG"));
            blogLink.Click();
        }

        public void CountArticles()
        {
            var articles = browser.FindElements(By.TagName("article"));
            Console.WriteLine("There are: " + articles.Count.ToString() + "articles");
        }

    }
}

As you can see, our goal is to navigate through Safari to nultien.rs and to click on BLOG link. Then to count all <article> tags and to write result into console.

Now, let’s publish WebJob project.

From the solution explorer right click on webJobBrowserstack project select Publish as Azure WebJob.

Select frequency of WebJob execution. In my case I will leave it on Run on Demand and click OK.

File webjob-publish-settings.json will be generated accordingly in the root of the Properties.

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "webJobBrowserstack",
  "startTime": null,
  "endTime": null,
  "jobRecurrenceFrequency": null,
  "interval": null,
  "runMode": "OnDemand"
}

Note: Reschedule of the webjob is not possible from the Azure portal. If there is need for reschedule, remove this file and Add Azure WebJob will appear during publish proces, or you can simply edit JSON file.

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "webJobBrowserstack",
  "startTime": "2016-04-20T00:00:00+01:00",
  "endTime": "2016-04-21T00:00:00+01:00",
  "jobRecurrenceFrequency": "Hour",
  "interval": 1,
  "runMode": "Scheduled"
}

After setting scheduler parametars click OK and sign in with your Microsoft account.

 

Select Subscription and since we don’t have application, New Application Service.

Choose unique Web App name and Select App Service Plan.

After you add Web App yours publish profile will be created. You may Publish the App.

In Visual Studio Output window, you can monitor status of publishing.

Publish Succeeded.

Web App was published successfully http://webjobbs.azurewebsites.net/

 

WebJob is published and can be triggered through Azure Portal.

Find web app WebJobBS in All resources and navigate to the Settings.

Then open WebJobs blade from the Settings blade. Here you can right click it and select Run. When status is changed into Completed click on LOGS.

 

We can see WebJob returned success. In detailed log we can see that there are 8articles.

Logs on Browserstack shows completed with video of each step.

And also detailed log of test steps.

If want to see fail results, we can replace line

var blogLink = browser.FindElement(By.LinkText("BLOG"));

with

var blogLink = browser.FindElement(By.LinkText("blog"));

Logs on Azure Portal will show:

NoSuchElementException: Could not find element: {"using":"link text","value":"blog"}

And Browserstack will show same error and provide snapshot.

Congratulation, your success / fail test is completed.

As Bruce Wayne likes to say to Clark Kent:

“I’m through talking. Get out of my cave.”

Share:

No Comments

Leave a reply

Comment sent for approval