Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

116
LINES

< > BotCompany Repo | #1028656 // Test Scripting for web bots [Include]

JavaX fragment (include)

1  
concept TestScript {
2  
  S scriptName, comments;
3  
  bool active;
4  
  long lastRun;
5  
  bool testedOK;
6  
  S botLinePattern1, userLine1;
7  
  S botLinePattern2, userLine2;
8  
  S botLinePattern3, userLine3;
9  
  S botLinePattern4, userLine4;
10  
  S botLinePattern5, userLine5;
11  
  S botLinePattern6, userLine6;
12  
  S botLinePattern7, userLine7;
13  
  S botLinePattern8, userLine8;
14  
15  
  int highestIndex() { ret 8; }
16  
  
17  
  S botLinePattern(int i) {
18  
    ret trim(getString(this, "botLinePattern" + i));
19  
  }
20  
  
21  
  S userLine(int i) {
22  
    ret trim(getString(this, "userLine" + i));
23  
  }
24  
}
25  
26  
sclass TestScripting { 
27  
  sO html(S uri, SS params, AuthedDialogID auth) null {
28  
    S uri2 = appendSlash(uri);
29  
    bool requestAuthed = auth != null;
30  
  
31  
    if (startsWith(uri2, "/testscripts/")) {
32  
      if (!requestAuthed) ret serveAuthForm(params.get('uri));
33  
      ret serveTestScriptingAdmin(uri, params);
34  
    }
35  
  }
36  
37  
  sO serveTestScriptingAdmin(S uri, SS params) {
38  
    S nav = p(ahref(rawBotLink(dbBotID), "Main admin") + " | " + ahref(baseLink + "/testscripts", "Test scripts admin"));
39  
    
40  
    if (ewic(uri, "/run-test")) {
41  
      long id = parseLong(params.get("id"));
42  
      TestScript script = getConcept TestScript(id);
43  
      if (script == null) ret "Script " + id + " not found";
44  
      S output = hijackPrint_tee_pcall(() -> runTestScript(script));
45  
      S title = "Test results for script: " + script.scriptName;
46  
      
47  
      ret hhtml(hhead_title(title) + hbody(
48  
          hsansserif()
49  
          + nav + h1(title)
50  
          + hsourcecode(output)));
51  
    }
52  
    
53  
    HCRUD_Concepts<TestScript> data = new HCRUD_Concepts<>(TestScript);
54  
55  
    HCRUD crud = new(rawLink("testscripts"), data) {
56  
      S frame(S title, S contents) {
57  
        ret hhtml(hhead_title_htmldecode(title) + hbody(
58  
          hsansserif() + hcss_responstable()
59  
          /*+ loadJQuery()
60  
          + hscript_reloadOnHistoryNav()*/
61  
          + nav + h1(title)
62  
          + contents));
63  
      }
64  
      
65  
      S renderValue(S field, O value) {
66  
        if (eq(field, "lastRun"))
67  
          ret renderHowLongAgo(toLong(value));
68  
        ret super.renderValue(field, value);
69  
      }
70  
    };
71  
    crud.renderCmds = item -> crud.renderCmds_base(item)
72  
      + " | " + ahref(rawLink("testscripts/run-test" + hquery(id := item.get("id"))), "run test");
73  
    crud.cmdsLeft = true;
74  
    crud.tableClass = "responstable";
75  
    crud.uneditableFields = litset("lastRun", "testedOK");
76  
    ret subBot_serveHTMLNoCache(crud.renderPage(params));
77  
  }
78  
 
79  
  svoid runTestScript(TestScript script) { 
80  
    try {
81  
      temp WebBotTester tester = new(programID());
82  
      tester.params.put("testMode", "1");
83  
      tester.start();
84  
      
85  
      int i = 0;
86  
      for (int idx = 1; idx <= script.highestIndex(); idx++) {
87  
        S pat = script.botLinePattern(idx);
88  
        if (nempty(pat)) {
89  
          print("Bot line pattern: " + pat);
90  
          tester.waitForMessages(i+1);
91  
          WebChatBotMsg msg = get(tester.msgs, i);
92  
          try {
93  
            assertTrueVerbose(i + ": " + pat + "/" + msg.msgText, mmo2_match(pat, msg.msgText));
94  
            assertTrue("fromBot", !msg.fromUser);
95  
          } on fail {
96  
            print(msgs := tester.msgs);
97  
          }
98  
          ++i;
99  
        }
100  
        
101  
        S userLine = script.userLine(idx);
102  
        if (nempty(userLine)) {
103  
          print("Sending line: " + userLine);
104  
          tester.sendMsg(userLine);
105  
          ++i;
106  
        }
107  
      }
108  
      
109  
      print("Script OK: " + script.scriptName);
110  
      cset(script, testedOK := true);
111  
    } catch print e {
112  
      cset(script, testedOK := false);
113  
    }
114  
    cset(script, lastRun := now());
115  
  }
116  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028656
Snippet name: Test Scripting for web bots [Include]
Eternal ID of this version: #1028656/30
Text MD5: 03531171ae66732ab17691cf7ac9b4fd
Author: stefan
Category: javax / web chat bots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-02 18:23:51
Source code size: 3687 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 432
Version history: 29 change(s)
Referenced in: [show references]