Ranorex入门指南40 – 引用其他类

上一节里面,我们用一个hard coding的期望值字符串和静态文本上得到的字符串作比较,在这节里,我们将会把生成期望值的工作专门分离到一个类里面,然后我们的测试用例将会引用这个新的类,从而得到期望值字符串。

首先,我们在项目里面新建一个C#类文件,取名为MyString.cs,因为我们的测试项目名是SimpleGUITest,所以默认地,该类将属于命名空间SimpleGUITest, 我们这里人为地把该类的命名空间改为TestTools, 整个类文件内容如下:
using System;

namespace TestTools
{
///

/// Description of MyString.
///

public class MyString
{
public MyString()
{
}

public string AddHello(string str)
{
return “Hello ” + str;
}
}
}

该类只有一个用来生成期望字符串的函数AddHello(),他将返回添加了Hello以后的字符串。

然后,我们修改SimpleCode.cs,在引用部分,添加一句引用MyString.cs的声明:Using TestTools;
并且新建一个测试用的函数UsingTest(),更新后的测试代码如下:
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;

UsingTest();
}

void UsingTest()
{
repo.SimpleGUI.SearchTimeout = new Duration(1000);
if (!repo.SimpleGUI.SelfInfo.Exists())
{
Host.Local.RunApplication(“C:\\SimpleGUI.exe”, “”, “”, false);
}
repo.SimpleGUI.Self.Activate();

string str = “Ranorex”;

// clean the text field first
repo.SimpleGUI.Text.TextValue = “”;

// input
repo.SimpleGUI.Text.TextValue = str;

repo.SimpleGUI.AddHello.Check();

repo.SimpleGUI.ButtonSay.Click();

string result = repo.SimpleGUI.TextSay.TextValue;

MyString stringTest = new MyString();

string expectStr = stringTest.AddHello(str);

Report.Info(“The result text is ” + result);
Report.Info(“The expect text is ” + expectStr);

// we expect the check pass
Validate.IsTrue(result==expectStr, “Check result”);
}

从代码里面可以看到下面两句新建了一个MyString类的对象,紧接着就调用了AddHello()函数来生成期望字符串。
MyString stringTest = new MyString();

string expectStr = stringTest.AddHello(str);

Comments 3

  1. Reply

    Recording1.cs里面的内容不能修改啊。怎么把自己写的代码加进去呢?请大侠指教

    1. Reply

      没错,请参见ranorex用户指南。

      Every time you change and save a recording, the main code file ‘AddNewEntry.cs’ is newly generated. Any change of code should always be made within the recording’s UserCode.cs file.

  2. Reply

    我想赋值后自动选中GridView的某一行。录制的动作是:
    6 0ms Mouse Click Left 36;7 InputTagCK
    我添加了这个动作:
    6 0ms Invoke Action PerformClick ¥VarIndexCk InputTagCK
    绑定1、2、3的值回放时他自己不会去选中第一二三行。我应该怎么设置呢。
    😳 😳

Ella进行回复 取消回复

请输入正确的验证码