简体   繁体   English

无法在 protractor e2e 测试中找到 MatSnackBar 元素

[英]Unable to locate MatSnackBar element in protractor e2e test

I am not able to locate MatSnackBar element using protractor.我无法使用 protractor 找到 MatSnackBar 元素。

This is how I display snack bar.这就是我展示小吃店的方式。

const snackBarRef = this.snackBar.open('Book added.', 'Undo', {
  duration: 300000
});

This is e2e test.这是e2e测试。

const snackBar = element(by.tagName('simple-snack-bar'));
browser.wait(ExpectedConditions.visibilityOf(snackBar), 30000);
element(by.tagName('simple-snack-bar')).getText().then(function (val) {
  console.log(val);
  expect(val).toEqual('Book added');
});

Test code fails with this error.测试代码因此错误而失败。 Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Thanks for your message, @dplavcic.感谢您的留言,@dplavcic。 The reason that my code didn't work is that snackbar was out of angular root.我的代码不起作用的原因是,snackbar 不在 angular 根目录下。 So I have to use browser.driver.findElement() instead of element() , and finally have solved the issue.所以我必须使用browser.driver.findElement()而不是element() ,终于解决了这个问题。

Something like:就像是:

public getSnackBar(): promise.Promise<string> {
    const snackbar = browser.driver.wait(until.elementLocated(By.tagName('simple-snack-bar')), 10000);

    return snackbar.getText();
}```

The time out is from the browser.wait of 30 second, so the application cannot find the elemnt with in that time.超时是从 browser.wait 30 秒开始的,所以应用程序在那个时间内找不到元素。 so increase the time out as:所以增加超时时间:

add below property to your config file:将以下属性添加到您的配置文件中:

jasmineNodeOpts: {defaultTimeoutInterval: 40000}

About element not found关于未找到元素

  1. make sure elemnt is not inside iframe or shadown, if so, then switch to parent before accessing it确保元素不在 iframe 或 shadown 内,如果是,则在访问之前切换到父级

  2. use promise chaining properly or use await instead正确使用 promise 链接或使用 await 代替

 it('should find an element by text input model', async function() { await browser.get('app/index.html#/form'); var username =element(by.model('username')); await username.clear(); await username.sendKeys('Jane Doe'); var name = element(by.binding('username')); expect(await name.getText()).toEqual('Jane Doe'); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM