量角器:不移至下一段代码

时间:2019-02-08 13:01:39

标签: typescript protractor

“ IsPresent()”中的“其他”代码未执行。仅执行“ IF”代码。它发生的任何原因。我正在用茉莉花框架量角器。在输出中,首先代码在“ IsPresent()”函数内执行“ If”,然后再次调用递归,我希望应该按照我从excel输入的值执行“ else”块。但是执行到此为止,当我手动关闭浏览器时,它将在“ IsPresent()”的“ else”中打印该语句。无法确定为什么卡住并且不移动。任何建议,将不胜感激。

注意:已经使用了递归,以便可以从excel文件读取值并根据索引打印输出。

以下是我正在使用的代码:

import { browser, protractor, element, by, By, ElementArrayFinder, ElementFinder } from "protractor";
import { Workbook } from "exceljs";
import { exists } from "fs";


describe('Search PID', () => {
    var Excel = require('exceljs');
    var wrkbook = new Excel.Workbook();
    wrkbook.xlsx.readFile("Path to the excel file");

    beforeAll(() => {
        browser.ignoreSynchronization = true;
        browser.get('My App URL');

    });

    function PIDMatching(value, index) {
        element(By.css('text[transform="translate(800.8947256481425,612.9323227754049)rotate(0)"]'))
                    .getText().then(function (PID) {
            console.log("PID is: " +PID);
            if (PID!=value)  {
                console.log("Unmatched PID is: " + value + " and counter is: " + index);
                element(By.css('a:nth-child(2) > div > img')).click();
                element(By.css('div.col-md-10 > input')).sendKeys(value);
                browser.sleep(2000);
                element(By.css('div.col-md-2 > button')).click();
                browser.sleep(5000);
                element(By.css('text[transform="translate(800.8947256481425,612.9323227754049)rotate(0)"]'))
                        .getText().then(function (PID) {

                    if (PID!=value) {
                        console.log("Unmatched PID in 2nd attempt is: " + 
                            value + " and counter is: " + index);
                    } else {
                        console.log("matched for PID  in 2nd time: " +
                            value + " and counter is: " + index);
                    }
                });
            } else if (PID==value) { 
                console.log("matched for PID at first attempt: " +
                    value + " and counter is: " + index) 
            }
        });
    }




    fit('Navigation', () => {
        var userNameField = element(By.id('mat-input-0'));
        var userPassField = element(By.id('mat-input-1'));
        var userLoginBtn = element(By.xpath('/html/body/app-root/app-side-menu/mat-card-actions/button'));
        userNameField.sendKeys('username');
        userPassField.sendKeys('pwd');
        userLoginBtn.click();
        browser.actions().mouseMove(element(by.css('#assetsId > rect')), { x: 214.211, y: 69.6306 }).doubleClick().perform();
        browser.actions().mouseMove(element(by.css('#assetsId > rect')), { x: 263.353, y: 177.73 }).doubleClick().perform();
        element(By.xpath('/html/body/a/div[2]/img')).click();
        browser.sleep(3000);
        element(By.css('li:nth-child(5) > a > div')).click().then(function () {
            // Click on Search icon
            var worksheet = wrkbook.getWorksheet('Sheet1');
            var rows = worksheet.rowCount;

            var loop = function(i) {
                if (i >= 1) {
                    var a = worksheet.getRow(i).getCell(1).value;  
                    element(By.css('a:nth-child(2) > div > img')).click();
                    element(By.css('div.col-md-10 > input')).sendKeys(a);
                    browser.sleep(2000);
                    element(By.css('div.col-md-2 > button')).click()
                    element(by.xpath('//*[contains(@id,"mat-dialog")]/app-error-dialog/div/div[1]/div/span'))
                        .isPresent().then(function (isVisible){   
                    console.log("count Value is: " +isVisible) ;   
                    if (isVisible == true) {
                        console.log("its present: ");
                        element(By.xpath('//*[contains(@id,"mat-dialog")]/app-error-dialog/div/div[1]/div/span')).click();
                        loop(i - 1);
                    } else {
                        console.log("its not present: ") ; 
                        PIDMatching(a, i);
                    }
                });
                element(By.css('li:nth-child(5) > a > div'))
                        .click().then(function () {
                    loop(i-1);
                });
            }
    }
    loop(rows);  

});
browser.close();
});
});

1 个答案:

答案 0 :(得分:0)

初始尝试:使用异步/等待(仍然需要作者的更多信息)

我建议使用async / await,因为它将清理您的诺言并链接then语句。在上面的代码中,有未与.then链接的promise。使用async / await应该可以清除其中的一些内容。

因此,对于初次通过,请修复为使用异步/等待。您的递归有问题,我不确定如何根据所提供的内容进行修复(以下评论中的讨论)。

您需要执行异步/等待的另一个原因是,Protractor在不久的将来将不再支持控制流。要关闭控制流(使用异步/等待),您需要在量角器配置文件中设置SELENIUM_PROMISE_MANAGER: false

// Please be consistent with your import of by. If you are using "By"
// then do not also use "by".

export async function loop(i: number): Promise<void> {

  if (i >= 1) {
    // This is not locating a web element by css. You should just use a
    // css selector. This can lead to brittle tests.
    await element(by.css('body > app-root > app-side-menu > mat-sidenav-container > mat-sidenav > mat-nav-list > div > div > div.col-md-2 > button')).click();

    // Same issue here. Documented at http://www.protractortest.org/#/style-guide
    const count = await element.all(by.xpath('//*[contains(@id,"mat-dialog")]/app-error-dialog/div/div[1]/div/span')).count();
    console.log(`count Value is: ${count}`);

    if (count <= 0) {
      const PID = await element(by.css('text[transform="translate(800.8947256481425,612.9323227754049)rotate(0)"]')).getText();
      console.log("PID is: " + PID);
    } else {
      await element(by.xpath('//*[contains(@id,"mat-dialog")]/app-error-dialog/div/div[1]/div/span')).click();
      await loop(i - 1);
    }

    // Not exactly sure what's going on here, I'm guessing you keep clicking
    // at the same transform and the count keeps decreasing. When the i - 1 = 0,
    // then break the recursion. Let's say in the first recursion, we i = 4.
    // We print out all the PIDs and then when i = 1, we will click the link,
    // then loop(0) and whatever loop(rows) is. After that, i = 2, we'll then
    // again do loop(1) and whatever loops(rows) is.
    //
    // Something isn't right here. I would maybe discuss what you are doing
    // html requires recursion.
    await element(By.css('body > app-root > app-side-menu > mat-sidenav-container > mat-sidenav-content > app-process-view-container > div > app-process-pane > div > mat-card > nav > div > ul > li:nth-child(5) > a > div')).click();

    // Much recursion. Such loops.
    await loop(i - 1);

    // rows = ¯\_(ツ)_/¯
    await loop(rows);
  }
}

请修改您的问题,以便我们了解递归部分。您点击什么?您的测试实际应该做什么。请提供更多信息。