Codementor Events

Windows Application Automation 02: Send Mail with Mail Client

Published Sep 29, 2018Last updated Nov 01, 2018
Windows Application Automation 02: Send Mail with Mail Client

In this post, we will take the mail client that comes with Windows 10 as an example, to show how to write a BDD automation script to send emails. You will learn how to develop automation scripts for Windows UI application using Node.js.

The Windows 10 Mail client is used for receiving and sending emails. As a Windows application, it can also be automated and tested. In the previous article, Windows Automation 01 - Getting Start with Calculator Automation, we have introduced how to automate Calculator, which is a relative simple app. Today I will explain in more details how to automate Mail client, a bit more complicated Windows 10 applications.

1.jpg

Content

  • Prerequisite
  • Create object model
  • Create scenarios
  • Implement script
  • Use hooks
  • Test report
  • Summary

Prerequisites

Prepare a Windows 10 computer, install the following tools:

  • Node.js: Node.js is used when install npm packages. We won't use any additional packages for this article, but you may use some external packages when you write script of your own.

  • CukeTest: which can be installed downloaded from (http://cuketest.com) or install also from Windows Store.
    The online document: CukeTest docs;

Create object model

First create a new CukeTest project using “Windows” template, like below:

2.png

You can name it “mail_test” or some name you prefer. Open the model.tmodel file by clicking it in project explorer, and it will be opened in Model Manager, a component in CukeTest. For now the model file is an empty one. Please add the following controls on the Mail application UI to the model:

  • “New mail”
  • “To:”
  • “Subject:”
  • “Message body”

For more information on how to add objects to the model, you can refer to the previous article Windows Automation 01 - Getting Start with Calculator Automation. The added model objects are as follows:

3.png

Create scenarios

Create a Cucumber test scenario in CukeTest and edit the send mail scenario:

4.png

The content of this feature file is:

Feature: Windows Mail Automation
Automate Mail to send an email

  Scenario: Send Mail
    Given click [New mail] button
    When enter TO address "user@domain.com"
    And enter "Greeting from CukeTest" in the Subject
    And set the mail body as "How are you today."
    When click [Send] button
    Then mail should be sent out successfully

Click on each arrow button next to the step in the above scenario to generate an automated script template file:

const { Given, When, Then } = require('cucumber');
const { TestModel } = require('leanpro.win');
const { Util } = require('leanpro.common');

let model = TestModel.loadModel(__dirname + "/model1.tmodel");

//// Your step definitions /////

Given(/^click \[New mail\] button$/, async function () {
    return 'pending';
});

When(/^enter TO address "([^"]*)"$/, async function (arg1) {
    return 'pending';
});

When(/^enter "([^"]*)" in the Subject$/, async function (arg1) {
    return 'pending';
});

When(/^set the mail body as "([^"]*)"$/, async function (arg1) {
    return 'pending';
});

When(/^click \[Send\] button$/, async function () {
    return 'pending';
});

Then(/^mail should be sent out successfully$/, async function () {
    return 'pending';
});

Implement the automation code according to the behavior described in each step of the scenario. So this is the key of BDD, you create scenario first, and only when the scenario is well defined then you implement the scenario.

Here is the implementation code:

const { Given, When, Then } = require('cucumber');
const { TestModel } = require('leanpro.win');
const { Util } = require('leanpro.common');
const CukeTest = require('cuketest');
const assert = require('assert');

let model = TestModel.loadModel(__dirname + "/model1.tmodel");

//// Your step definitions /////

Given(/^click \[New mail\] button$/, async function () {
    await model.getWindow("Window").activate();
    await model.getButton("New mail").click();
});

When(/^enter TO address "([^"]*)"$/, async function (email) {
    await model.getEdit("To:").clearAll();    
    await model.getEdit("To:").set(email);
});

When(/^enter "([^"]*)" in the Subject$/, async function (subject) {
    await model.getEdit("Subject").clearAll();
    await model.getEdit("Subject").pressKeys(subject);
});

When(/^set the mail body as "([^"]*)"$/, async function (content) {
    await model.getEdit("Subject").pressKeys('{tab}');
    await model.getEdit("Subject").pressKeys(content);
});

When(/^click \[Send\] button$/, async function () {
    await model.getButton("Send").click();
});

Then(/^mail should be sent out successfully$/, async function () {
    await CukeTest.delay(1000); //wait for UI to respond.
    let exists = await model.getButton("Send").exists();
    assert.equal(exists, false, 'send button should no longer exists');
});

After adding these step definitions code, you can first launch the Mail app manually, then run the automation script as a project and see how it automation Mail client, and it then shows you the html report.

Use Hooks

There is one more thing you can do to make the script more complete. You can add hooks to the project, which can be used to set the default timeout period, and take screenshot, or doing cleanup before exit.

Create a new support/hooks.js file:

const path = require('path');
const {Before, BeforeAll, After, AfterAll, setDefaultTimeout} = require('cucumber');
const cuketest = require('cuketest');
const { TestModel } = require('leanpro.win');

let  modelFile = path.join(__dirname, '../step_definitions/model1.tmodel');
let model = TestModel.loadModel(modelFile);

//set time out value for each step to be 10 seconds
setDefaultTimeout(10 * 1000);

// minimize CukeTest IDE before run
BeforeAll(async function() {
    return cuketest.minimize();
})

// Take screenshot after the scenario
After(async function() {
    let screen = await model.getWindow("Window").takeScreenshot();
    this.attach(screen, 'image/png')});

// restore CukeTest IDE after run
AfterAll(async function() {
    return cuketest.maximize();
})

Test report

Click on the CukeTest “Run Project” button, CukeTest will minimize the CukeTest window, automate the Mail application, and then shows the test report window after the run.

5.png
6.png

You can see it includes the summary part as well as detailed report for each step. It also take a screenshot of Mail app and add it to the report.

Summary

CukeTest has built-in Windows templates, and it provides BDD test frameworks and report templates. It also provides control spying and object model editing capabilities. With them, it is easy to create UI automation script for Windows 10 Store apps, Once the script is developed, you can just run this test script with CukeTest.

See also:
Windows Automation 01 - Getting Start with Calculator Automation

Discover and read more posts from CukeTest
get started
post comments1Reply
Mark G
5 years ago

Hi, if I wanted to set the size of the Windows application how would I do this? I can’t see how to do this in Cuketest