在RFT中新增檔案檢查點

TIB發表於2010-01-17

不像Testcomplete,在QTP和RFT中都沒有提供檔案檢查點,但是在RFT中可以利用VpUtil和VpManual來方便地建立一個檔案檢查點。例如下面程式碼所示:

 

 

    public void testMain(Object[] args)

    {

        vpManual("Test1", "The rain in GZ").performTest();

        vpManual("Test2", "The rain in GZ","The rain in SZ").performTest();

       

        fileContentsTest("Test3","D://1.txt","D://2.txt");

    }

 

    protected byte[] getRawFileContents(File file)

    {

        byte[] result = null;

        if ( file != null && file.exists() )

        {

            FileInputStream input = null;

            try

            {

                // Read in the specified file ...

                input = new FileInputStream(file);

                result =  new byte[(int)file.length()];

                int bytesRead = 0;

                int offset = 0;

                final int length = result.length;

                while ( bytesRead != -1 && offset < length )

                {

                    bytesRead = input.read(result, offset, length-offset);

                    if ( bytesRead >= 0 )

                        // bytesRead == -1 when end-of-file is reached

                        offset += bytesRead;

                }

                input.close();

            }

            catch ( IOException ioexc )

            {

                throw new FileSupportException(ioexc.getClass().getName()+": "+ioexc.getMessage());

            }

        }

        else

            throw new FileSupportException("Invalid file specification: "+file);

        return result;

    }

   

    protected String getFileContents(File file)

    {

        byte[] result = getRawFileContents(file);

        return ( result != null ? new String(result) : null );

    }

   

    private File getFile(String fileName)

    {

        File file = new File(fileName);

        if ( !file.exists() )

        {

            file = new File((String)getOption(IOptionName.DATASTORE), fileName);

            if ( !file.exists() )

                throw new FileSupportException("Invalid file name specified to fileContentsTest: "+fileName);

        }

        return file;

    }

   

    public boolean fileContentsTest(String vpName, String expected, String actual)

    {

        return fileContentsTest(vpName, getFile(expected), getFile(actual));

    }

   

    public boolean fileContentsTest(String vpName, File expected, File actual)

    {

        ITestData dataExp = VpUtil.getTestData(getFileContents(expected));

        ITestData dataAct = VpUtil.getTestData(getFileContents(actual));

        return vpManual(vpName, dataExp, dataAct).performTest();

    }

   

 

 

 

關於VpUtil的getTestData方法的使用和VpManual的使用在RFT的幫助文件中有描述:

 vpManual

使用vpManual可以在指令碼中插入檢查點。

 

Constructs a manual verification point object. Manual verification points require that the user supply any necessary data before performTest can be executed.

This verification point form has several unique characteristics:

·         This verification point must be inserted by a script developer. It is not a record-time capability. The data is managed directly by the script developer and is not extracted from a TestObject in the way that static or dynamic verification points can automatically create the data.

·         If a baseline version of the data does not already exist from a previous playback, the actual data is used to create a baseline for subsequent script executions. The performTest method logs an informational message only (not pass or fail) when constructing a baseline file.

·         As the baseline is not created until the first time the script is executed, if you are using ClearCase change management, you should run the script before checking it in. If you check the script in before running it, this verification point baseline will not be checked in.

·         If a baseline exists, the actual data is compared to the initial baseline of the data when the performTest method is executed.

·         performTest persists the supplied data regardless of the outcome of the test. Therefore, it must be an object based on the Value class that can be persisted and compared.

·         The script developer is responsible for supplying a script-relative unique name for any single instance of these verification points. Reusing a verification point name commonly causes false negative results to occur because the incorrect data is tested.

 

VpUtil

VpUtil物件中的getTestData方法用於為檢查點獲取所需的測試資料。

 

Provides a default set of utility methods that can be used to create interesting data objects. The primary usage for this class is in conjunction with vpManual verification points.

 

 

 

相關文章