自動化測試程式之一自訂鍵盤的類比測試程式(C語言)

來源:互聯網
上載者:User

標籤:自動化的測試   鍵盤類比程式   類比鍵盤事件   自訂鍵盤自動化測試   鍵盤模擬測試   

一、測試程式編寫說明
我們做的終端裝置上啟動並執行是QT應用程式,使用自訂的鍵盤介面。經過測試人員長時間的人機互動測試,來確認系統的功能是否滿足需求。現在需要編寫一個自動化的測試程式,能夠按照預設的指令碼執行,比如某個按鍵需要連續執行10000次,或是通過連續幾個按鍵動作執行特定的商務程序10W次。通過這樣的自動化的測試,可以減輕測試人員的負擔,還可以查看觸發N次按鍵後,畫面執行N次後的系統的穩定性,如記憶體使用量率,cup使用率等等。

裝置有4*4的鍵盤,包括0-9,C(Call),A,U(up),D(Down),F1,F2功能鍵,螢幕的不同畫面上根據前述按鍵的動作執行相應的響應動作。

二、測試程式的結構分析

根據上述的簡單要求,先分析測試程式的結構如下:

讀入的指令檔可以是TXT檔案大概結構如下:

   --------Script_Sample.txt------------
3A 5R 3 2U 5C 6A 3

其中的第一行表示以下執行的5個動作,分別是按A 、U、C、A鍵並且每次按鍵後休息相應的秒數(即後面的數值),其中的R 5 1 行表示以下1行重複再重複5次。指令檔中R可不寫;若不寫,表示依次順序執行,沒有迴圈操作。

測試程式根據這個指令碼構造一個鏈表,鏈表中的節點表示相應的操作,操作序列中迴圈動作再列表中構成局部的單向迴圈列表。

三、測試程式實現主要邏輯
1、定義鏈表

typedef struct List  {      char operation;    int  seconds;    FLAG c_flag;    int  i_repeatCnt;    int  i_repeatLines;    struct List *nextGp; //指標域      struct List *nextMemeber; //指標域  }List;  List *oprtData_Set;

2、上報輸入事件

int reportkey(int fd, uint16_t type, uint16_t keycode, int32_t value)  {      struct input_event event;      event.type = type;      event.code = keycode;      event.value = value;      gettimeofday(&event.time, 0);      if (write(fd, &event, sizeof(struct input_event)) < 0) {          printf("report key error!\n");          return -1;      }      return 0;  } 

3、使用尾插法按照指令碼中的動作構造按鍵動作的鏈表

void TailCreatList(List *L,char * fname)  //尾插法建立鏈表  {      List *tmpData;    List *tail ;    List *groupheader;      int i_repeatNums = 0;    int i_repeatLines = 0 ,i_repeatLines_tmp = 0;    char c_repeatID;    FLAG  flag = HEADER;//flag = 1 header    char buffer[512];    char c_repeatFlag;    FILE *infile;    infile=fopen(fname,"r");    tail=L;  //NULL    groupheader=tail->nextGp;//NULL    if(infile==NULL)    {        printf("\nFailed to open the file : %s \n",fname);        exit(0);    }    else    {        printf("open success! \n");    }    fgets( buffer, sizeof(buffer), infile );    sscanf( buffer,"%d",&i_stepMaxNum);    printf("i_stepMaxNum = %d \n",i_stepMaxNum);    memset(buffer,0,sizeof(buffer));    while ( fgets(buffer, sizeof(buffer), infile))    {            tmpData=(struct List*)malloc(sizeof(struct List));            if(!tmpData)            {                   printf("malloc() [email protected] \n");                exit(0);            }            memset(tmpData,0,sizeof(struct List));            tmpData->nextMemeber=NULL;            tmpData->nextGp=NULL;            sscanf(buffer,"%c",&c_repeatFlag);            if(c_repeatFlag == ‘R‘)            {                sscanf( buffer,"%c %d %d",&c_repeatID,&i_repeatNums,&i_repeatLines );                printf( "Repeat = %c , RepeatNums = %d,RepeatLines = %d \n",c_repeatID,i_repeatNums,i_repeatLines );                memset(buffer,0,sizeof(buffer));                continue;            }            else            {                sscanf( buffer,"%c %d",&(tmpData->operation),&(tmpData->seconds));                printf( "Operation = %c , seconds = %d\n",tmpData->operation,tmpData->seconds);                if(i_repeatLines > 0)                {                    if(flag==HEADER)                    {                        groupheader=tmpData;                        tmpData->c_flag=flag;                        tmpData->i_repeatCnt = i_repeatNums;                        flag = MEMBER;                        tmpData->nextMemeber=groupheader;                        tmpData->nextGp=NULL;                        tail->nextGp=tmpData; // 注意串連臃絞?每個Group的頭 用 nextGp 指標串連                    }                    else                    {                        tmpData->c_flag=flag;                        tmpData->nextMemeber=groupheader;                        tmpData->nextGp=NULL;                        tail->nextMemeber=tmpData; //group中的成員用 nextMemeber 指標相連                    }                    tail=tmpData; //修改尾指標的位置。                    i_repeatLines--;                }                else                {  //--OK!!                    flag=HEADER;                    groupheader = tmpData;                    tmpData->c_flag = flag;                    tmpData->nextMemeber=groupheader;                    tmpData->nextGp=NULL;                    tail->nextGp=tmpData;                    tail=tmpData;                }                if(i_repeatLines==0)                {                    flag=HEADER;                    }            }                            memset(buffer,0,sizeof(buffer));    }    tail->nextGp=NULL;     //tail->nextMemeber=NULL;    fclose(infile);    return ;}

4、構造鍵盤事件,包括按下和抬起

void PressKeyEvent(char operation, int seconds){    uint16_t keycode;    printf("Key-%c ,%3d Sec |",operation,seconds);    keycode = KeyToVal(operation);    reportkey(KB_Fd, EV_KEY, keycode, KEYDOWN);    reportkey(KB_Fd, EV_KEY, keycode, KEYUP);      sleep(seconds);}

5、按照鏈表中的資料逐條發送按鍵事件

void EmitEvent_Test(List *L)  {      List *p=L->nextGp;         int loop=0;        CYCLE_MODE mode_flag = FirstCycle;        printf("-------EmitEvent_Test-------\n");    while(p!=NULL)      {                   //printf ("[** %d **,%c,%d,%d]  ",p->c_flag, p->operation,p->seconds,p->i_repeatCnt);         PressKeyEvent(p->operation,p->seconds);            if(p->nextMemeber->c_flag != HEADER) //             {                   p = p->nextMemeber;            }            else            {              /*                    printf("p->nextMemeber Node is [** %d **,%c,%d,%d ]",                        p->nextMemeber->c_flag,                         p->nextMemeber->operation,                        p->nextMemeber->seconds,                        p->nextMemeber->i_repeatCnt);                 */                    if(mode_flag == FirstCycle &&  p->nextMemeber->i_repeatCnt >0)                    {                       loop = p->nextMemeber->i_repeatCnt;                        mode_flag = OtherCycle;                        p = p->nextMemeber;                        loop--;                        printf("\n----------------\n");                        continue;                    }                if( loop > 0 && mode_flag == OtherCycle )//未重複完                    {                        p = p->nextMemeber;                        loop--;                        printf("\n----------------\n");                        continue;                    }                    mode_flag = FirstCycle;//恢複預設值                    p = p->nextGp;                    printf("\n\n");            }    }  }

四、參考源碼程式

http://download.csdn.net/detail/flyeagle022/8799555

自動化測試程式之一自訂鍵盤的類比測試程式(C語言)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.