2017-05-22 209 views
0

這幾乎是我開始進行自動化測試的原因,所以如果我不能讓你清楚,我想道歉!使用Selenium和NUnit進行UI自動化測試測試執行順序

因此,一兩個星期閱讀自動化的博客後,我決定用NUNIT硒的webdriver的UI自動化。

我的應用是在開發3年後留存的企業級應用。這是一個項目和投資組合管理系統。

我有數百頁,其中約50%執行CRUD操作。

,我決定爲我的應用程序如下strucutre:

Project structure

我使用的測試數據爲JSON在下面的格式和這個數據retrive到我的視圖模型:

[ 
    { 
    "Name": "Finance", 
    "Description": "division with complete test data", 
    "Color": "#ff0000", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "IT", 
    "Description": "IT projects", 
    "Color": "pink", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "Business", 
    "Description": "division with name and color name", 
    "Color": "yellow", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "", 
    "Description": "division without name and color name, should add white color", 
    "Color": "", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": true, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "", 
    "Description": "without name and color name and will not be added", 
    "Color": "black", 
    "ExpectedStatus": { 
     "WillBeAdded": false, 
     "WillBeDeleted": false, 
     "WillBeUpdated": false 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": false 
    } 
    } 
] 

我這裏使用兩件事:

1. PerformableActions ie ie what我可以通過使用這個測試用例數據來執行操作。例如,shouldAdd表示應該爲添加記錄執行此TestCase,shouldDelete表示此TestCase是否應該運行以刪除記錄,同樣應該運行UpdateUpdate。

2. ExpectedStatus即測試用例的預期結果是什麼 WillBeAdded意思是將這個數據添加到網格中。同樣的WillBeDeleted和WillBeUpdated工作。

問題:

1)我使用相同的測試數據爲所有CUD操作,這樣做的原因是,我要保持我的應用程序的CUD流。由於只需要測試用戶界面,所以我只是假設創建,檢索和刪除將按順序工作。

這是在CRUD操作情況下進行測試的正確方法嗎?

2)想我要在我的項目運行所有測試,也可能是數百或數千,我將如何維持秩序,因爲我能看到的唯一可能的方法是使用訂購屬性, NUNit。我該如何使用它,或者是否有任何替代方法來單獨測試獨立模塊組和獨立模塊組?

回答

1
  1. 您的方法沒有任何問題。您可以爲每個案例創建一個函數,並按照您希望的順序製作C,U和D.也許你需要在它們之間添加wait,讓後端執行它的企業內容。
  2. NUnit的OrderAttribute工作得很好,即使有更好的方法。您還可以創建測試用例列表並按順序依次運行它們。爲什麼不?不要將自己限制在類和方法上。
  3. 我知道,你用C#標記了你的問題,但是對於這麼多的工作,或許值得去看看一些其他的庫,而不是你現在擁有的。如果你不害怕很少學習,你可以享受F#:

    • Expecto你以何種順序和測試運行
    • Canopy您對硒的一個很好的DSL完全控制。舉個小例子,這裏是starter kit。你甚至可以在docker爲Chrome瀏覽器無頭模式
    • 運行它,你甚至可以得到智能感知您的JSON測試定義與Json Type Provider

UPDATE:爲命令你可以定義自己的屬性,甚至使用NUnit中的現有屬性,並以正確的順序調用每個反射的方法。或者你定義自己的DSL進行測試並遍歷列表並提取你需要的信息。

public struct CRUD 
{ 
    public bool C; 
    public bool R; 
    public bool U; 
    public bool D; 
    public CRUD(bool c, bool r, bool u, bool d) 
    { 
     this.C = c; 
     this.R = r; 
     this.U = u; 
     this.D = d; 
    } 
} 
public enum ActionKind 
{ 
    Create, 
    Read, 
    Update, 
    Delete 
} 
public struct TestResult 
{ 
    public ActionKind Action; 
    public bool IsPerformed; 
} 
public class TestCase 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Color { get; set; } 
    public CRUD Action { get; set; } 
    public List<TestResult> Actual { get; set; } 
    public List<TestResult> Expected { get; set; } // or List<TestResult> 

} 
public class Tests 
{ 
    public void TestAListOfCases() 
    { 
     var cases = new List<TestCase> 
     { 
      new TestCase { Name = "Finance", Description = "division with complete test data", Color = "#ff0000", 
       Expected = new List<TestResult> {new TestResult {Action = ActionKind.Create, IsPerformed = true }, 
               new TestResult {Action = ActionKind.Update, IsPerformed = false}, 
               new TestResult {Action = ActionKind.Delete, IsPerformed = true } 
       }}, 
      new TestCase { Name = "IT", Description = "Description"} 
     }; 

     cases 
      .Where(x => x.Name == "IT") 
      .Where(x => x.Color != "pink") 
      .Select(RunTestCase) 
      .Where(x => !x.Item2) 
      .Select(x => OutputFailedTestCase(x.Item1)); // boah c# is verbose 
    } 
    public Tuple<TestCase, bool> RunTestCase(TestCase testCase) 
    { 
     foreach(var exp in testCase.Expected) 
     { 
      switch (exp.Action) { 
       case ActionKind.Delete: 
        // do delete if needed and create actual result 
        var actual = exp.IsPerformed 
         ? new TestResult { Action = exp.Action, IsPerformed = true } 
         : new TestResult { Action = exp.Action, IsPerformed = false }; 
        break; 

      } 
     } 
     var isFailed = 
      Enumerable.Zip(
       testCase.Actual, 
       testCase.Expected, 
       (expected, actual) => expected.Action == actual.Action && expected.IsPerformed == actual.IsPerformed) 
      .All(x=>x); 
     // your selenium stuff 
     return Tuple.Create(testCase, isFailed); 
    } 
    public bool OutputFailedTestCase(TestCase testCase) 
    { 
     // write to console or do something else 
     Console.Write($"{testCase.Name} is failed to perform following actions: {calculateFailedActions}"); 
     return true; 
    } 
} 
+0

感謝至少有人看着我的問題。非常感謝你!主要問題仍然是訂購。我現在有超過20個CUD實體,如果我使用訂單,我必須遵循1,2,3,4,當我訂購超過9個訂單時,列表更新爲1,12,13,2,3 ,4等等。意味着它按字母順序排列。我被困住了,現在發現它非常厭惡硒元素的使用。你能否給我提供一些演示,如果我的測試數量在幾百,我如何分類我的測試,我需要用我的願望來執行它們。我甚至可以使用包裝來做到這一點。我處於可以接受任何事情的地步。 – Umar