DEV Community

Roel
Roel

Posted on • Updated on

How to Azure: Host a Selenium JavaScript Node Application in Azure and Send Email Notifications on Failures

How to Host a Selenium JavaScript Node Application in Azure and Send Email Notifications on Failures

Automated browser testing is essential for maintaining web application quality. Selenium is a powerful tool for automating browsers, and in this guide, we'll walk through hosting a Selenium JavaScript Node application on Azure App Service and setting up email notifications if the tests fail three times in a row. This approach ensures you are immediately aware of any repeated failures in your web application's functionality.

Prerequisites

To follow this tutorial, you’ll need:
(For more in depth about this process visit: https://dev.to/iamrule/how-to-js-automate-testing-with-selenium-46j4 )

  • Node.js and npm installed on your local machine.
  • A Selenium JavaScript script ready for deployment.
  • An Azure account with access to Azure App Services.
  • Basic understanding of using the Azure Portal.

Step-by-Step Instructions

Step 1: Prepare Your Selenium Script

Ensure your Selenium script is ready for deployment. Below is an example of a simple script that opens a webpage and checks for an element:

const { Builder, By, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

// Utility function to pause execution
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function runTest() {
  let options = new chrome.Options();
  options.addArguments('--no-sandbox', '--headless', '--disable-gpu');

  let driver = await new Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

  try {
    await driver.get('https://example.com'); // Replace with your target URL
    await driver.wait(until.elementLocated(By.id('test-element')), 10000);
    console.log('Element found and page loaded successfully.');
  } catch (error) {
    console.error('Error during test:', error);
    process.exit(1);
  } finally {
    await driver.quit();
  }
}

runTest();
Enter fullscreen mode Exit fullscreen mode

Ensure that the script runs in headless mode, as this is required for running Selenium scripts in a server environment like Azure App Services.

Step 2: Create an Azure Web App Using the Azure Portal

  1. Log in to the Azure Portal:

  2. Create a Resource Group:

    • In the left-hand menu, click on Resource groups.
    • Click + Create.
    • Fill in the Resource group name (e.g., SeleniumTestingRG) and select your Region.
    • Click Review + create and then Create.
  3. Create an Azure App Service:

    • Navigate back to the Home screen and select App Services.
    • Click + Create and choose Web App.
    • Fill in the Basics:
      • Subscription: Choose your Azure subscription.
      • Resource Group: Select the resource group you just created (SeleniumTestingRG).
      • Name: Enter a unique name for your web app (e.g., selenium-tests-app).
      • Publish: Select Code.
      • Runtime stack: Choose Node 14 LTS or the version matching your script.
      • Operating System: Select Windows or Linux (Linux is preferred for headless browser tests).
      • Region: Choose the same region as your resource group.
    • Click Next: Monitoring, then Review + create and Create.
  4. Deploy Your Selenium Script to the Web App:

    • After the deployment is complete, navigate to your Web App resource in the Azure Portal.
    • In the Deployment section, click on Deployment Center.
    • Choose Local Git or GitHub (if you want to connect to a repository).
    • Follow the steps to set up deployment from your local machine or GitHub repository.

Step 3: Configure the Application Settings

  1. Add Application Settings:

    • In the Web App resource, go to Configuration under the Settings section.
    • Click on Application settings.
    • Ensure that you add any necessary environment variables or settings needed for your Selenium script to run.
  2. Install Chrome and Chromedriver:

    • You will need to ensure that the Chrome browser and ChromeDriver are available on the server. This can be managed by adding startup commands or scripts to download and install Chrome and ChromeDriver on the server, but for simplicity, ensure your deployment package includes these if needed.

Step 4: Set Up Azure Monitor and Alerts

  1. Enable Logging and Monitoring:

    • Go to the Monitoring section in your Web App and select App Service logs.
    • Set Application Logging (Filesystem) to On and select a log level (e.g., Error).
    • Save the settings.
  2. Create an Alert Rule:

    • Go to Azure Monitor in the Azure Portal.
    • Click on Alerts > + New alert rule.
    • Set the Scope to your Web App.
    • Under Condition, choose Custom log search or Failed request depending on your script’s logging strategy.
    • Set up the query to detect three consecutive errors or failures.
    • Under Actions, click Add action group.
    • Fill in the details and add an Email/SMS/Push/Voice action.
    • Click Review + create and then Create.

Step 5: Test Your Setup

  1. Run a Test Deployment:

    • Deploy the Selenium script to the Azure Web App following the same steps as above.
    • Check the logs in the Azure Portal to ensure the script runs successfully.
  2. Trigger a Failure and Verify Alerts:

    • Modify your Selenium script to cause a deliberate failure (e.g., by changing the element ID).
    • Redeploy the script and monitor the logs for errors.
    • Ensure that after three consecutive failures, an email notification is sent to your configured address.

Common Issues/Troubleshooting

  • Deployment Errors: Make sure your package.json is correctly set up and all dependencies are installed before deploying.
  • Environment Issues: If the browser does not open, check if the headless mode is enabled and ensure that Chrome and ChromeDriver are installed on the server.
  • Timeouts and Failures: Adjust the Selenium script to increase the wait time if elements take longer to load.

Conclusion

By following these steps, you have successfully hosted a Selenium JavaScript Node application on Azure and configured it to send email notifications if the test fails three times in a row. This setup helps automate your testing process and keeps you informed of any critical issues without constant manual checking.

Remember to keep monitoring your Azure services and update your Selenium scripts as your web application evolves.

Happy testing! 🚀

Top comments (0)