javascript 量角器:获取警报文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19879631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:08:14  来源:igfitidea点击:

Protractor: Get text of an alert?

javascriptangularjswebdriverjasmineprotractor

提问by nickcoxdotme

I'm testing my Angular app with Protractor. I've looked through the docsand can't find any way to get the text of an alert. It's not an element in the DOM per se (at least, not that I can figure out; when there's an alert up, Chrome's inspector won't allow you to inspect it). How would I test that an alert has the correct message? Or even, that one is present?

我正在使用 Protractor 测试我的 Angular 应用程序。我已经浏览了文档,但找不到任何获取警报文本的方法。它本身不是 DOM 中的元素(至少,我无法弄清楚;当有警报时,Chrome 的检查器将不允许您检查它)。我将如何测试警报是否包含正确的消息?甚至,那个人在场?

Edit

编辑

Here is my code. HTML:

这是我的代码。HTML:

  <button id='alertButton' data-ng-click='ngAlert()'>Button</button>

JS:

JS:

$scope.ngAlert = function(){
  window.alert('Hello');
};

Protractor spec:

量角器规格:

  describe('alert', function(){
    var ptor = protractor.getInstance();
    beforeEach(function(){
      button = $('#alertButton');
      button.click();
    });
    it('tells the alert message', function(){
      expect(button.getText()).toEqual('Button');
    });
  });

When I make an assertion on the button text like this:

当我像这样对按钮文本做出断言时:

it('tells the alert message', function(){
  expect(button.getText()).toEqual('Button');
});

It passes. But if I try to read an alert like this:

它通过。但是,如果我尝试阅读这样的警报:

it('tells the alert message', function(){
  var alertDialog = ptor.switchTo().alert();
  expect(alertDialog.getText()).toEqual('Hello');
});

I get this error:

我收到此错误:

$ protractor spec/e2e/conf.js Using the selenium server at http://localhost:4444/wd/hub.F

Failures:

1) alert tells the alert message Message: NoSuchAlertError: no alert open (Session info: chrome=30.0.1599.101) (Driver info: chromedriver=2.2,platform=Mac OS X 10.9.0 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 3 milliseconds Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01' System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9', java.version: '1.6.0_65' Session ID: edbaa752eb14ad45f7e961903b69a466 Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.2}, rotatable=false, locationContextEnabled=true, version=30.0.1599.101, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]

Finished in 2.125 seconds 2 tests, 2 assertions, 1 failure

/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1542 throw error; ^ NoSuchAlertError: no alert open

$ protractor spec/e2e/conf.js 在http://localhost:4444/wd/hub.F使用 selenium 服务器

失败:

1) 警报告诉警报消息 Message: NoSuchAlertError: no alert open (Session info: chrome=30.0.1599.101) (Driver info: chromedriver=2.2,platform=Mac OS X 10.9.0 x86_64) (WARNING: 服务器没有提供任何堆栈跟踪信息)命令持续时间或超时:3 毫秒构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'2013-08-12 15:42:01'系统信息:os.name:' Mac OS X',os.arch:'x86_64',os.version:'10.9',java.version:'1.6.0_65' 会话 ID:edbaa752eb14ad45f7e961903b69a466 驱动程序信息:org.openqa.selenium Capabilities.{ChromeDriveplatform Capabilities.{ChromeDriveplatform Capabilities. =MAC,acceptSslCerts=true,javascriptEnabled=true,browserName=chrome,chrome={chromedriverVersion=2.2},rotatable=false,locationContextEnabled=true,version=30.0.1599.101,cssSelectorsEnabled=true,databaseEnabled=true,handlesAlerts=true,browserConnectionEnabled=false,nativeEvents=true,webStorageEnabled=true,applicationCacheEnabled=false,takesScreenshot=true}]

在 2.125 秒内完成 2 次测试、2 次断言、1 次失败

/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1542 抛出错误;^ NoSuchAlertError:没有打开警报

But I've tested it on the page and it works, and the test can clearly find the button in the DOM. So either the click()function isn't working, or something else is going on?

但是我已经在页面上测试过了,可以正常运行,测试可以清晰的找到DOM中的按钮。那么要么该click()功能不起作用,要么正在发生其他事情?

回答by Colin Brock

Inside your test, get the current Protractor instance and use switchTo().alert()to access the alertdialog box:

在您的测试中,获取当前的 Protractor 实例并用于switchTo().alert()访问alert对话框:

var ptor = protractor.getInstance();

var alertDialog = ptor.switchTo().alert();

expect(alertDialog.getText()).toEqual("Hello");

Keep in mind that Protractor is basically just a wrapper for Selenium WebDriver so, as far as I know, anything you can do with Selenium WebDriver you can do with Protractor.

请记住,Protractor 基本上只是 Selenium WebDriver 的包装器,因此,据我所知,您可以使用 Selenium WebDriver 执行的任何操作都可以使用 Protractor 执行。

Edited to include the full test:

编辑以包括完整的测试:

describe('Alert dialog', function () {

    var ptor = protractor.getInstance(),
        button;

    beforeEach(function () {
        // This line is necessary on my end to get to my test page.
        // browser.driver.get('http://localhost:8000/test.html');
        button = ptor.findElement(protractor.By.id('alertButton'));
        button.click();
    });

    it('tells the alert message', function () {
        var alertDialog = ptor.switchTo().alert();
        expect(alertDialog.getText()).toEqual("Hello");
    });

});

It is possible that your app is still initializing at the point that your test is executed, which would explain why no dialog seems to be appearing. Ensure that your app is "ready to go" and can actually display the alert before making your assertion. Hope that helps!

在执行测试时,您的应用程序可能仍在初始化,这可以解释为什么似乎没有出现对话框。确保您的应用程序“准备就绪”并且可以在做出断言之前实际显示警报。希望有帮助!

回答by Uli

You have to wait for the browser to open/display the alert. Example using Protractor 2.2.0:

您必须等待浏览器打开/显示警报。使用量角器 2.2.0 的示例:

   var timeoutInMilliseconds = 1000;
   browser.wait(protractor.ExpectedConditions.alertIsPresent(), timeoutInMilliseconds);
   var alertDialog = browser.switchTo().alert();
   expect(alertDialog.getText()).toEqual("Hello World!");