Jasmine 量角器元素(by.css(';id#u name';)。getText()使浏览器等待 版本信息

Jasmine 量角器元素(by.css(';id#u name';)。getText()使浏览器等待 版本信息,jasmine,protractor,angular6,angular-e2e,Jasmine,Protractor,Angular6,Angular E2e,量角器:5.4.0 茉莉花芯:2.6.2 jasmine spec reporter:4.1.0 问题 我想做一个简单的e2e测试,在浏览器重定向到页面后检查元素的内部文本。但是,当检查元素内部时,浏览器只是停留在页面上10秒钟,然后出现以下错误: 1) material App should re-route to login page - Error: Timeout - Async callback was not invoked within timeout specified by

量角器:5.4.0 茉莉花芯:2.6.2 jasmine spec reporter:4.1.0

问题 我想做一个简单的e2e测试,在浏览器重定向到页面后检查元素的内部文本。但是,当检查元素内部时,浏览器只是停留在页面上10秒钟,然后出现以下错误:

1) material App should re-route to login page
  - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  - ScriptTimeoutError: Timed out waiting for asynchronous Angular tasks to finish after 10 seconds. This may be because the current page is not an Angular application. Please see the FAQ for more details: htt
ps://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular
  While waiting for element with locator - Locator: By(css selector, #globTitle)

Executed 1 of 1 spec (1 FAILED) in 14 secs.
[20:04:24] I/launcher - 0 instance(s) of WebDriver still running
[20:04:24] I/launcher - chrome #01 failed 1 test(s)
[20:04:24] I/launcher - overall: 1 failed spec(s)
[20:04:24] E/launcher - Process exited with error code 1
An unexpected error occured: undefined
这是我的密码: HTML

<div class="main-container">  
    <mat-toolbar [ngStyle]="{'background-color': topBarColor, 'color': 'white'}" class="topbar telative" style = "z-index: 0 !important;">  
        <button mat-icon-button (click)="snav.toggle()" *ngIf = "showbtn" value="sidebarclosed">
             <mat-icon>menu</mat-icon>  
         </button>  
         <span id = "globTitle">Programme Admin Dashboard</span>  
         <span style = "flex: 1 1 auto;"></span>  
         <button mat-raised-button colour = "warn" *ngIf = "showbtn" (click) = "signOut(); snav.close();" style = "margin-right: 20px;">Sign out</button>  
         <img src="assets/images/logo.png" alt="No logo available" style = "height: inherit; margin-bottom: 5px;">  
     </mat-toolbar>  
     <mat-sidenav-container class="example-sidenav-container" [style.marginTop.px]="mobileQuery.matches ? 0 : 0">  
         <mat-sidenav style = "text-align: center;" #snav id="snav" class="dark-sidebar pl-xs" [mode]="mobileQuery.matches ? 'side' : 'over'" fixedTopGap="0" [opened]= false [disableClose]="mobileQuery.matches" >  
             <app-sidebar (click) = "snav.close()"></app-sidebar>  
         </mat-sidenav>  
         <mat-sidenav-content class="page-wrapper">  
             <div class="page-content">  
                 <router-outlet><app-spinner></app-spinner></router-outlet>  
             </div>  
         </mat-sidenav-content>  
         <ng-template #loadingSpinner>  
         <mat-spinner></mat-spinner>  
             <p>Fetching permissions...</p>  
         </ng-template>  
    </mat-sidenav-container>  
</div>
import {browser, by, element, protractor} from 'protractor';  

export class AppPage {  
  navigateTo() {  
    return browser.get('/');  
  }  

  getURIChange(url: string) {  
    const ec = protractor.ExpectedConditions;  
  browser.wait(ec.urlContains(url), 2000);  
  }  
}
附录e2e-spec.ts: (请注意,当我删除最后一行代码时,测试会运行。事实上,即使是
元素(by.css(“#globTitle”)
也可以正常工作。 但是,如果我将其更改为
元素(by.css('#globTitle')).getText()
,浏览器就会在这里等待,并出现上面提到的错误。 从“/app.po”导入{AppPage};
从“量角器”导入{browser,element,by}

describe('material App', () => {  
  let page: AppPage;  

  beforeEach(() => {  
    page = new AppPage();  
  });  

  it('should re-route to login page', async () => {  
    console.log('Waiting for angular');  
  await browser.waitForAngularEnabled();  
  console.log('Angular initialized');  
  console.log('Waiting on navigation to page');  
  // page.navigateTo();  
  await browser.get('http://localhost:4201/');  
  console.log('Navigation completed, should be redirected to the login page');  
  page.getURIChange('/login');  
  expect(element(by.css('#globTitle')).getText()).toEqual('Programme Admin Dashboard');  
  });  
});
此外,我不确定这是否有帮助,但这里是我的量角器配置和因果报应配置以防万一。 量角器.conf.js: const{SpecReporter}=require('jasmine-spec-reporter')

karma.conf.js

module.exports = function (config) {  
  config.set({  
    basePath: '',  
  frameworks: ['jasmine', '@angular-devkit/build-angular'],  
  plugins: [  
      require('karma-jasmine'),  
  require('karma-chrome-launcher'),  
  require('karma-jasmine-html-reporter'),  
  require('karma-coverage-istanbul-reporter'),  
  require('@angular-devkit/build-angular/plugins/karma')  
    ],  
  client:{  
      clearContext: false // leave Jasmine Spec Runner output visible in browser  
  },  
  coverageIstanbulReporter: {  
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],  
  fixWebpackSourcePaths: true  
  },  
  angularCli: {  
      environment: 'dev'  
  },  
  reporters: ['progress', 'kjhtml'],  
  port: 9876,  
  colors: true,  
  logLevel: config.LOG_INFO,  
  autoWatch: true,  
  browsers: ['Chrome'],  
  customLaunchers: {  
      ChromeNoSandbox: {  
        base: 'Chrome',  
  flags: ['--no-sandbox']  
      }  
    },  
  singleRun: false,  
  });  
};

您好,您没有在步骤定义中返回回调

因此,jasmine认为您正在执行或等待某些操作。但一旦发生超时,即10秒,它就会抛出一个错误

it('should re-route to login page', async (done) => { 
  console.log('Waiting for angular'); 
  await browser.waitForAngularEnabled(); 
  console.log('Angular initialized'); 
  console.log('Waiting on navigation to page'); // page.navigateTo(); 
  await browser.get('http://localhost:4201/'); 
  console.log('Navigation completed, should be redirected to the login page'); 
  page.getURIChange('/login'); 
 Var a = element(by.css('#globTitle');
  Var b = a.getText().then((value)=>{console.log('title is'+value);
return value;}) 

expect(b).toEqual('Programme Admin Dashboard'); done() 
    }); 
试试这个:

const el1 =  await $('#globTitle').getText();
expect(el1).toEqual('Programme Admin Dashboard');

getText()等待承诺的解析,因此无法像以前那样工作。

步骤定义?您是指在it代码块内部吗?此外,我还尝试了元素(by.css('#globTitle')).getText()。然后((text)=>{expect(text).toBe('Program Admin Dashboard');});这也不能像我在回答中添加的那样进行添加。如果需要,请增加全局超时我已将全局超时增加到100000毫秒,但错误仍然存在。我意识到,几乎所有返回承诺的函数都会出现此问题。browser.getTitle()给了我同样的行为。我也尝试了一个catch语句,但没有成功。没有一个承诺得到解决。请检查它是否在console.log中打印标题从“jquery”添加
import*作为$后;
在顶部添加您建议的行,我收到以下错误:(节点:11188)未经处理的PromisejectionWarning:错误:jQuery需要一个带有文档的窗口……此外,我的代码直接来自文档,仍然不适用于我,尽管它适用于其他人。您是否尝试打印el1?它说了什么?它不允许我打印,因为jQuery错误阻止了typescript的编译你坚持等待angular?这看起来像是等待浏览器。waitForAngularEnabled(false);如果你能接受的话,可以解决这个问题。
it('should re-route to login page', async (done) => { 
  console.log('Waiting for angular'); 
  await browser.waitForAngularEnabled(); 
  console.log('Angular initialized'); 
  console.log('Waiting on navigation to page'); // page.navigateTo(); 
  await browser.get('http://localhost:4201/'); 
  console.log('Navigation completed, should be redirected to the login page'); 
  page.getURIChange('/login'); 
 Var a = element(by.css('#globTitle');
  Var b = a.getText().then((value)=>{console.log('title is'+value);
return value;}) 

expect(b).toEqual('Programme Admin Dashboard'); done() 
    }); 
const el1 =  await $('#globTitle').getText();
expect(el1).toEqual('Programme Admin Dashboard');