1. Import Libraries
Before implementing test steps with TestLeft, you need to import the required classes:
C#
using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
2. Create a Driver Object
You must create an instance of the Driver class as you do this in a regular TestLeft test. The way you do this depends on your test. In our case, we create it in the first test step:
C#
[Given(@"I open Notepad")]
public void GivenIOpenNotepad()
{
IDriver driver = new SmartBear.TestLeft.LocalDriver();
}
We use the LocalDriver
class. This way we access the TestLeft test engine on the local computer. To access the TestLeft test engine running on a remote computer, use the RemoteDriver class.
3. Run the Tested Application
-
Use the created driver object to run the tested application:
C#
[Given(@"I open Notepad")]
public void GivenIOpenNotepad()
{
IDriver driver = new SmartBear.TestLeft.LocalDriver();
IProcess notepadProcess = driver.Applications.Run("notepad.exe");
}To learn more, see Running Applications From Tests.
4. Get Test Objects
When you create a test with TestLeft, you can inspect applications objects, their methods and properties by using TestLeft UI Spy and generate code to get them from the test.
-
Run the Notepad application.
-
In Visual Studio, select View > TestLeft UI Spy to open TestLeft UI Spy.
-
Configure the TestLeft UI Spy code generation settings. Click on the main toolbar and specify the following settings:
-
In the Languages section, select C#.
-
In The first object section, select Main form / Page. The Process variable name box should specify the name of the variable that contains a reference to the testing process. In our case, it is
notepadProcess
.
Click OK.
-
-
Hold the left mouse on the Pick Object button (the mouse cursor will turn into the target icon (). Drug the cursor to the Notepad window and drop it once a red frame appears around the edit box:
TestLeft UI Spy will select the object in the object tree. The panel on the right shows the methods and properties of the selected object:
-
To verify that the Notepad window contains no text, use the
wText
property. Right-click thewText
property and select Copy Member Call from the context menu:TestLeft will copy the needed code to the clipboard.
-
Paste the code to the method of the next test step:
C#
[Given(@"there is no text in Notepad")]
public void GivenThereIsNoTextInNotepad()
{
ITextEdit edit = notepadProcess.Find<ITopLevelWindow>(new WindowPattern()
{
WndClass = "Notepad"
}).Find<ITextEdit>(new WindowPattern()
{
WndClass = "Edit"
});
string wTextValue = edit.wText;
} -
In this code, Visual Studio cannot resolve the
notepadProcess
variable. To fix that, you need to declare the corresponding variable within the class scope:C#
public class RunTheUndoCommandUsingHotkeysSteps
{
// Declare the variable
private IProcess notepadProcess;
[Given(@"I open Notepad")]
public void GivenIOpenNotepad()
{
IDriver driver = new SmartBear.TestLeft.LocalDriver();
// Assign the object of the Notepad process to the notepadProcess variable
notepadProcess = driver.Applications.Run("notepad.exe");
}
...
} -
Use the
Assert.AreEqual
method to verify that the property contains an empty string:C#
[Given(@"there is no text in Notepad")]
public void GivenThereIsNoTextInNotepad()
{
ITextEdit edit = notepadProcess.Find<ITopLevelWindow>(new WindowPattern()
{
WndClass = "Notepad"
}).Find<ITextEdit>(new WindowPattern()
{
WndClass = "Edit"
});
string wTextValue = edit.wText;
Assert.AreEqual("", wTextValue);
}
5. Simulate User Actions
-
To implement the next test steps, you need to interact with the edit box. Therefore, you need to make it visible for other methods. Declare the corresponding variable at the beginning of the class:
C#
public class RunTheUndoCommandUsingHotkeysSteps
{
private IProcess notepadProcess;
private ITextEdit edit;
...
[Given(@"there is no text in Notepad")]
public void GivenThereIsNoTextInNotepad()
{
edit = notepadProcess.Find<ITopLevelWindow>(new WindowPattern()
{
WndClass = "Notepad"
}).Find<ITextEdit>(new WindowPattern()
{
WndClass = "Edit"
});
string wTextValue = edit.wText;
Assert.AreEqual("", wTextValue);
}
} -
On the next test step, you need to enter text in the Notepad window. Visual Studio’s IntelliSense shows a list of methods available to the object:
To simulate typing text, we will use the
Keys
method:C#
[When(@"I type ""(.*)""")]
public void WhenIType(string p0)
{
edit.Keys(p0);
}Note that the created method contains the
p0
argument. SpecFlow extracts the phrase in quotes from the text of the test step by using a regular expression and passes it to the argument. -
To simulate pressing hotkeys, we also use the
Keys
method:C#
[When(@"I press \[Ctrl \+ z]")]
public void WhenIPressCtrlZ()
{
edit.Keys("^z");
} -
Finally, we check that Notepad does not contain any text. Use the
Assert.AreEqual
method, as we did it earlier:C#
[Then(@"I should see no text in Notepad")]
public void ThenIShouldSeeNoTextInNotepad()
{
Assert.AreEqual("", edit.wText);
}
6. Close the Application
In our example, the Notepad application is still running after the test run is over. It means that when another scenario starts, it will run another instance of the application. This may cause errors and performance issues. To fix that, you must close the application when the scenario finishes. To do that, use the After
attribute. SpecFlow will run a method with this attribute at the end of each test scenario. Add the following method to the test class:
C#
[After]
public void CleanUp()
{
if (notepadProcess != null)
{
notepadProcess.Close();
}
}
Final Code
The final test code is as follows:
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;
using System;
using TechTalk.SpecFlow;
namespace SpecFlowWithTestLeft
{
[Binding]
public class RunTheUndoCommandUsingHotkeysSteps
{
private IProcess notepadProcess;
private ITextEdit edit;
[Given(@"I open Notepad")]
public void GivenIOpenNotepad()
{
IDriver driver = new SmartBear.TestLeft.LocalDriver();
// Run the application
notepadProcess = driver.Applications.Run("notepad.exe");
}
[Given(@"there is no text in Notepad")]
public void GivenThereIsNoTextInNotepad()
{
// Obtain the edit box object
edit = notepadProcess.Find<ITopLevelWindow>(new WindowPattern()
{
WndClass = "Notepad"
}).Find<ITextEdit>(new WindowPattern()
{
WndClass = "Edit"
});
// Get the value of the wText property
string wTextValue = edit.wText;
// Verify that the edit box does not contain any text
Assert.AreEqual("", wTextValue);
}
[When(@"I type ""(.*)""")]
public void WhenIType(string p0)
{
// Type the specified text
edit.Keys(p0);
}
[When(@"I press \[Ctrl \+ z]")]
public void WhenIPressCtrlZ()
{
// Press hotkeys
edit.Keys("^z");
}
[Then(@"I should see no text in Notepad")]
public void ThenIShouldSeeNoTextInNotepad()
{
// Verify that the edit box is empty
Assert.AreEqual("", edit.wText);
}
[After]
public void CleanUp()
{
// Close the application
if (notepadProcess != null)
{
notepadProcess.Close();
}
}
}
}
See Also
Behavior-Driven Development
About Behavior-Driven Development