第一次成功迭代后函数崩溃(元素未附加到页面文档中)

时间:2018-08-17 11:46:14

标签: javascript jasmine protractor

想问一下下面的函数有什么问题,第一次迭代没有任何问题,但是在刷新网格后,当量角器试图移动到下一个单元格时,它会显示以下错误消息:

失败:过时的元素引用:元素未附加到页面文档

此方法的想法是读取第5列(如果考虑从0开始则为第4列),检查包含值“ true”的每一行,如果包含true,则执行编辑该行的操作,单击复选框并保存(所有操作均在该行执行) 每个元素都有一个与状态代码相关的唯一ID,因此为什么我在函数中“不使用行”(只是从行中获取文本,然后作为字符串传递来完成元素的ID)

**每次更改和保存值时,表都会刷新

/**                                                                                                                                                
 * Get Wav Amplitudes                                                                                                                              
 *                                                                                                                                                 
 * @param file                                                                                                                                     
 * @return                                                                                                                                         
 * @throws UnsupportedAudioFileException                                                                                                           
 * @throws IOException                                                                                                                             
 */                                                                                                                                                
private int[] getWavAmplitudes(File file) throws UnsupportedAudioFileException , IOException {                                                     

    //Get Audio input stream                                                                                                                       
    try (AudioInputStream input = AudioSystem.getAudioInputStream(file)) {                                                                         
        AudioFormat baseFormat = input.getFormat();                                                                                                

        //Encoding                                                                                                                                 
        Encoding encoding = AudioFormat.Encoding.PCM_UNSIGNED;                                                                                     
        float sampleRate = baseFormat.getSampleRate();                                                                                             
        int numChannels = baseFormat.getChannels();                                                                                                

        AudioFormat decodedFormat = new AudioFormat(encoding, sampleRate, 16, numChannels, numChannels * 2, sampleRate, false);                    
        int available = input.available();                                                                                                         

        //Get the PCM Decoded Audio Input Stream                                                                                                   
        try (AudioInputStream pcmDecodedInput = AudioSystem.getAudioInputStream(decodedFormat, input)) {                                           
            final int BUFFER_SIZE = 4096; //this is actually bytes                                                                                 

            //Create a buffer                                                                                                                      
            byte[] buffer = new byte[BUFFER_SIZE];                                                                                                 

            //Now get the average to a smaller array                                                                                               
            int maximumArrayLength = 100000;                                                                                                       
            int[] finalAmplitudes = new int[maximumArrayLength];                                                                                   
            int samplesPerPixel = available / maximumArrayLength;                                                                                  

            //Variables to calculate finalAmplitudes array                                                                                         
            int currentSampleCounter = 0;                                                                                                          
            int arrayCellPosition = 0;                                                                                                             
            float currentCellValue = 0.0f;                                                                                                         

            //Variables for the loop                                                                                                               
            int arrayCellValue = 0;                                                                                                                

            //Read all the available data on chunks                                                                                                
            while (pcmDecodedInput.readNBytes(buffer, 0, BUFFER_SIZE) > 0)                                                                         
                for (int i = 0; i < buffer.length - 1; i += 2) {                                                                                   

                    //Calculate the value                                                                                                          
                    arrayCellValue = (int) ( ( ( ( ( buffer[i + 1] << 8 ) | buffer[i] & 0xff ) << 16 ) / 32767 ) * WAVEFORM_HEIGHT_COEFFICIENT );  

                    //Every time you him [currentSampleCounter=samplesPerPixel]                                                                    
                    if (currentSampleCounter != samplesPerPixel) {                                                                                 
                        ++currentSampleCounter;                                                                                                    
                        currentCellValue += Math.abs(arrayCellValue);                                                                              
                    } else {                                                                                                                       
                        //Avoid ArrayIndexOutOfBoundsException                                                                                     
                        if (arrayCellPosition != maximumArrayLength)                                                                               
                            finalAmplitudes[arrayCellPosition] = finalAmplitudes[arrayCellPosition + 1] = (int) currentCellValue / samplesPerPixel;

                        //Fix the variables                                                                                                        
                        currentSampleCounter = 0;                                                                                                  
                        currentCellValue = 0;                                                                                                      
                        arrayCellPosition += 2;                                                                                                    
                    }                                                                                                                              
                }                                                                                                                                  

            return finalAmplitudes;                                                                                                                
        } catch (Exception ex) {                                                                                                                   
            ex.printStackTrace();                                                                                                                  
        }                                                                                                                                          
    } catch (Exception ex) {                                                                                                                       
        ex.printStackTrace();                                                                                                                      

    }                                                                                                                                              

    //You don't want this to reach here...                                                                                                         
    return new int[1];                                                                                                                             
}  

编辑行之前的HTML:

    function resetGoodItemStatus(siteToResetValues){
      var cellsConform = element.all(by.css('#datatableDir tr td:nth-of-type(5)'));
      var conformCounter = 0;
      selectValueDropDown(siteToResetValues)
        cellsConform.each((eachCell) => {
            eachCell.getText().then((cellText) => {
                switch (cellText)
                {
                    case 'true':
                        element(by.id(conformCounter+'-1')).getText().then(function(value){
                            element(by.id('btnEdit-US.'+value)).click();
                            element(by.xpath("//*[@editable-checkbox=\"scopeValue.getConformMapping(scopeValue.getDataRow("+'\'US.'+value+"\')).conform\"]/../span/span/input")).click();
                            element(by.id('btnSubmit-US.'+value)).click();
                        })
                    default:
                        browser.sleep(100)
                }
                conformCounter += 1
                });

        });
      }
点击“编辑”按钮后的

HTML:

 <tr role="row" class="odd">
 <td id="0-0" class="ng-scope">
<form editable-form="" name="scopeValue.rowforms['US.DAM']" onaftersave="scopeValue.saveData('US.DAM',0)" ng-show="scopeValue.rowforms['US.DAM'].$visible" class="form-buttons form-inline ng-pristine ng-valid ng-hide" style="">
    <button type="button" class="btn btn-xs btn-trans kni kni-check-circle text-info" id="btnSubmit-US.DAM" ng-disabled="scopeValue.rowforms['US.DAM'].$waiting" ng-click="scopeValue.rowforms['US.DAM'].$submit()">
    </button>
    <button type="button" class="btn btn-link kni kni-x-circle-slim" ng-disabled="scopeValue.rowforms['US.DAM'].$waiting" id="btnCancel-US.DAM" ng-click="scopeValue.cancelData('US.DAM',0)">
    </button>
</form>
<div class="buttons" ng-show="!scopeValue.rowforms['US.DAM'].$visible">
    <button type="button" class="btn btn-xs btn-trans kni kni-edit-circle text-info" ng-click="scopeValue.rowforms['US.DAM'].$show()" id="btnEdit-US.DAM">
    </button>
</div></td>
<td id="0-1" class="ng-scope sorting_1">DAM</td>
<td id="0-2" class="ng-scope">US.DAM</td>
<td id="0-3" class="ng-scope">Generic Damaged Code</td>
<td id="0-4" class="ng-scope">
<span editable-checkbox="scopeValue.getConformMapping(scopeValue.getDataRow('US.DAM')).conform" e-name="conform" e-form="scopeValue.rowforms['US.DAM']" e-required="" class="ng-scope ng-binding editable">false</span>
</td></tr>

谢谢您的时间!

1 个答案:

答案 0 :(得分:1)

一种简单的方法是将第5列中所有单元格的文本放入文本数组,然后迭代该文本数组,该数组的索引等于表的行索引。

一旦单元格文本等于true,请使用行索引查找表行。其余元素可以在表格行中找到。

由于在每次迭代中,下面的代码将再次从页面中查找所有表行,Stale Exception

function resetGoodItemStatus(siteToResetValues){
    var cellsConform = element.all(by.css('#datatableDir tr td:nth-of-type(5)'));

    selectValueDropDown(siteToResetValues);

    cellsConform.getText().then(function(conforms) {

        // conforms is a string Array, each one is the text of one cell of 5th column
        conforms.forEach(function(conform, rowIndex) {

            if(conform === 'true') {
                var row = element.all(by.css('#datatableDir tr').get(rowIndex);

                row.element(by.css('button[id^="btnEdit-US"]')).click();

                row.element(by.css('input[type="checkbox"]')).click();

                row.element(by.css('button[id^="btnSubmit-US"]')).click();

                browser.sleep(3000)
            }
        });
    });

}