在上一步基础上做登录界面的测试

测试计划:

cs架构怎么测试安全性 cs测试流程_IP

一、界面:

    1. 登录界面

cs架构怎么测试安全性 cs测试流程_UI_02

    界面元素:Ip地址下拉框,端口输入框,用户名下拉框,密码输入框,记住密码选中框,登录按钮,关闭按钮。

   2.提示界面

    

cs架构怎么测试安全性 cs测试流程_用户登录_03

    元素:确定按钮,提示信息,标题。

结果:成功,失败,异常。

先把所有已知的可能失败操作一遍,最后使用成功登录,中间能测试出异常是最好了(说明测试出了原本手工测试遗漏的地方)。


二、修改测试代码


将UIMap中的自动生成的测试代码,提取出来(因为直接修改UIMap会被后续的自动生成代码覆盖),并修改成通用的。

原来UIMap中的代码:

/// <summary>
        /// LoginUserError - Use 'LoginUserErrorParams' to pass parameters into this method.
        /// </summary>
        public void LoginUserError()
        {
            #region Variable Declarations
            WpfComboBox uIIPComboBox = this.UI用户登录Window.UIIPComboBox;
            WpfEdit uIPART_EditableTextBoxEdit = this.UI用户登录Window.UIIPComboBox.UIPART_EditableTextBoxEdit;
            WpfEdit uIPortEdit = this.UI用户登录Window.UIPortEdit;
            WpfComboBox uIUserComboBox = this.UI用户登录Window.UIUserComboBox;
            WpfEdit uIPasswordEdit = this.UI用户登录Window.UIPasswordEdit;
            WpfCheckBox uI记住密码CheckBox = this.UI用户登录Window.UI记住密码CheckBox;
            WpfButton uI登录Button = this.UI用户登录Window.UI登录Button;
            WpfButton uI确定Button = this.UI提示Window.UI确定Button;
            #endregion

            // Select '127.0.0.1' in 'IP' combo box
            uIIPComboBox.EditableItem = this.LoginUserErrorParams.UIIPComboBoxEditableItem;

            // Type '{Tab}' in 'PART_EditableTextBox' text box
            Keyboard.SendKeys(uIPART_EditableTextBoxEdit, this.LoginUserErrorParams.UIPART_EditableTextBoxEditSendKeys, ModifierKeys.None);

            // Type '60005' in 'Port' text box
            uIPortEdit.Text = this.LoginUserErrorParams.UIPortEditText;

            // Select 'admin2' in 'User' combo box
            uIUserComboBox.EditableItem = this.LoginUserErrorParams.UIUserComboBoxEditableItem;

            // Move 'Password' text box
            Mouse.StartDragging(uIPasswordEdit, new Point(54, 13));
            Mouse.StopDragging(uIPasswordEdit, -49, 2);

            // Type '********' in 'Password' text box
            Keyboard.SendKeys(uIPasswordEdit, this.LoginUserErrorParams.UIPasswordEditSendKeys, true);

            // Select '记住密码' check box
            uI记住密码CheckBox.Checked = this.LoginUserErrorParams.UI记住密码CheckBoxChecked;

            // Click '登录' button
            Mouse.Click(uI登录Button, new Point(72, 19));

            // Click '确定' button
            Mouse.Click(uI确定Button, new Point(38, 24));
        }

提取到测试类中后修改后的代码

/// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class LoginUITest
    {
        private WpfComboBox uIIPComboBox;
        private WpfEdit uIPART_EditableTextBoxEdit;
        private WpfEdit uIPortEdit;
        private WpfComboBox uIUserComboBox;
        private WpfEdit uIPasswordEdit;
        private WpfCheckBox uI记住密码CheckBox;
        private WpfButton uI登录Button;
        private WpfButton uI确定Button;

        public LoginUITest()
        {
            uIIPComboBox = UIMap.UI用户登录Window.UIIPComboBox;
            uIPART_EditableTextBoxEdit = UIMap.UI用户登录Window.UIIPComboBox.UIPART_EditableTextBoxEdit;
            uIPortEdit = UIMap.UI用户登录Window.UIPortEdit;
            uIUserComboBox = UIMap.UI用户登录Window.UIUserComboBox;
            uIPasswordEdit = UIMap.UI用户登录Window.UIPasswordEdit;
            uI记住密码CheckBox = UIMap.UI用户登录Window.UI记住密码CheckBox;
            uI登录Button = UIMap.UI用户登录Window.UI登录Button;
            uI确定Button = UIMap.UI提示Window.UI确定Button;
        }

        public void TestIpInner(string ip, string port, string user, string pass, bool isCheck, string message)
        {
            uIIPComboBox.EditableItem = ip;
            uIPortEdit.Text = port;
            uIUserComboBox.EditableItem = user;
            uIPasswordEdit.Text = pass;
            uI记住密码CheckBox.Checked = isCheck;
            Mouse.Click(uI登录Button, new Point(72, 19));// Click '登录' button
            string text = UIMap.UI提示Window.UIMessagePane.UIMessageText.DisplayText; // Click '确定' button
            if(!string.IsNullOrEmpty(message))
                Assert.AreEqual(message, text, "Message");
            Mouse.Click(uI确定Button, new Point(38, 24));
        }



具体测试函数

[TestMethod]
        public void TestIP()
        {
            //TestIpInner("127.0.0.1", "60005", "admin", "admin", true, "");//正确参数
            TestIpInner("", "60005", "admin", "admin", true, "IP地址不合法!");//空IP
            TestIpInner("1", "60005", "admin", "admin", true, "IP地址不合法!");//IP格式错误
            //TestIpInner("abc", "60005", "admin", "admin", true, "IP地址不合法!");//非法字符
            TestIpInner("127.0.0.2", "60005", "admin", "admin", true, "登录失败");//IP地址错误
        }

        [TestMethod]
        public void TestPort()
        {
            //TestIpInner("127.0.0.1", "60005", "admin", "admin", true, "");//正确参数
            TestIpInner("127.0.0.1", "", "admin", "admin", true, "登录失败");//空
            TestIpInner("127.0.0.1", "60000", "admin", "admin", true, "登录失败");//错误
            TestIpInner("127.0.0.1", "abc", "admin", "admin", true, "登录失败");//错误
        }

使用不同的典型参数,测试各种情况。

说明1:

string text = UIMap.UI提示Window.UIMessagePane.UIMessageText.DisplayText;

部分是一开始没有的,通过UIMap的添加控件功能,将提示框的提示信息控件也添加进来了,不同的失败情况会有不同的提示。

说明2:

27.0.0.1",而不是输入1,而且也不是输入127.0.0.1而是出现一个异常:

cs架构怎么测试安全性 cs测试流程_UI_04

说明3:

    因为IP下拉框不允许输入数字和.以外的符号,像"abc"作为参数会导致异常:

cs架构怎么测试安全性 cs测试流程_cs架构怎么测试安全性_05

    顺便说明一下,限制非法字符是在PreviewKeyDown事件中设置的

private void IP_PreviewKeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button_Click(sender, e);
            }
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Left || e.Key == Key.Right || e.Key.ToString() == "Tab" || e.Key == Key.Back)
            {

            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) &&
             e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                 
            }
            else
            {
                e.Handled = true;
               IP.Focus();
            }
        }

意外收获1:

发现用127.0.0.2也能登录,查了一下,发现:

127段是保留区段,127.0.0.2现实中上根本用不到这个IP,只是理论上的地址
这个应该是现在网络设备的一种特性吧,任何ping向保留区段127的都会指向127.0.0.1

这是我所不知道的一个知识点。

每个设置界面参数的步骤,如 uIIPComboBox.EditableItem = ip; 都是先将原有输入框的内容清空,然后重新输入数据,有点慢,是在搜索控件吗。

总之只是测试Ip的话,没必要重新设置其他参数。

修改代码为:

private void Submit(string message)
        {
            Mouse.Click(uI登录Button, new Point(72, 19));// Click '登录' button
            string text = UIMap.UI提示Window.UIMessagePane.UIMessageText.DisplayText; // Click '确定' button
            if (!string.IsNullOrEmpty(message))
                Assert.AreEqual(message, text, "Message");
            Mouse.Click(uI确定Button, new Point(38, 24));
        }

        private void SetInfo(string ip, string port, string user, string pass, bool isCheck)
        {
            uIIPComboBox.EditableItem = ip;
            uIPortEdit.Text = port;
            uIUserComboBox.EditableItem = user;
            uIPasswordEdit.Text = pass;
            uI记住密码CheckBox.Checked = isCheck;
        }

        private void TestIp(string ip, string message)
        {
            uIIPComboBox.EditableItem = ip;
            Submit(message);
        }

        [TestMethod]
        public void TestIPSimple()
        {
            SetInfo("127.0.0.1", "60005", "admin", "admin", true);//初始化参数
            TestIp("","IP地址不合法!");//空IP
            TestIp("1", "IP地址不合法!");//IP格式错误
            TestIp("1.1.1.1.1", "IP地址不合法!");//IP格式错误
            TestIp("127.1.1.9999999", "登陆失败");//登陆失败
            TestIp("127.1.1.9999", "登陆失败");//登陆失败
            TestIp("127.1.1.256", "登陆失败");//登陆失败
            TestIp("0.0.0.0", "登陆失败");//登陆失败
            TestIp("255.255.255.255", "登陆失败");//登陆失败
            TestIp("128.0.0.1", "登陆失败");//登陆失败 会等待21s
            TestIp("192.168.200.200", "登陆失败");//登陆失败 会等待21s
        }

TestIPSimple总共用时2分57秒

原来的TestIP(相同的测试内容)用时4分47秒 相差了2分钟。