量角器UI测试因Jenkins失败,在cmd运行的WinServer上成功

量角器UI测试因Jenkins失败,在cmd运行的WinServer上成功

问题描述:

我在Windows Server上配置了Jenkins,用于每晚生成,编译等.以及UI测试(E2E测试)自动化,但遇到了一些问题. 我们有一个在Chrome上运行的Web应用程序,并且需要Jenkins使用量角器(与Selenium)一起运行这些测试.

I have a Jenkins configured on Windows Server for Nightly builds, compilations etc.. and UI Test (E2E Tests) automation which I'am having some problems. We have a Web application runs on Chrome and need Jenkins to run these tests using protractor (with Selenium).

通过Windows cmd控制台手动运行,测试成功完成.

With the manual run from Windows cmd console, the tests finish successfully.

---------这是控制台输出;

-------Here is the console output;

***C:\Jenkins\jobs\UI Automation\workspace> protractor e2e-tests/protractor.conf.js

Using ChromeDriver directly...

[launcher] Running 1 instances of WebDriver

WARNING - more than one element found for locator by.repeater("attachment in pos
t_item.attachments") - the first result will be used

.
Finished in 68.394 seconds

1 test, 8 assertions, 0 failures

[launcher] 0 instance(s) of WebDriver still running

[launcher] chrome #1 passed***

-----这是詹金斯(Jenkins)运行的输出;

-----Here is the output of the run from Jenkins;

C:\Jenkins\jobs\UI Automation\workspace>protractor e2e-tests/protractor.conf.js  
Using ChromeDriver directly...
[launcher] Running 1 instances of WebDriver
WARNING - more than one element found for locator by.repeater("attachment in post_item.attachments") - the first result will be used
[31mF[0m
Failures:
  1) new_message_with_attachment should send new message with attachment
   Message:
     [31mElementNotVisibleError: element not visible
  (Session info: chrome=47.0.2526.111)
  (Driver info: chromedriver=2.13.307647 (5a7d0541ebc58e69994a6fb2ed930f45261f3c29),platform=Windows NT 6.0 SP2 x86_64)[0m
   Stacktrace:
     ElementNotVisibleError: element not visible
  (Session info: chrome=47.0.2526.111)
  (Driver info: chromedriver=2.13.307647 (5a7d0541ebc58e69994a6fb2ed930f45261f3c29),platform=Windows NT 6.0 SP2 x86_64)
    at new bot.Error (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)
    at Object.bot.response.checkResponse (C:\Users\x\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:106:9)
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:362:20
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\base.js:1582:15
    at [object Object].webdriver.promise.ControlFlow.runInNewFrame_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1654:20)
    at notify (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:465:12)
    at notifyAll (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:442:7)
    at resolve (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:420:7)
    at [object Object].fulfill (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:535:5)
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1520:10
==== async task ====
WebElement.click()
    at [object Object].webdriver.WebDriver.schedule (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:345:15)
    at [object Object].webdriver.WebElement.schedule_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:1727:23)
    at [object Object].webdriver.WebElement.click (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:1832:15)
    at actionFn (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:75:32)
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:393:17
    at Array.forEach (native)
    at actionResults.getWebElements.then.then.e.stack (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:392:9)
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\base.js:1582:15
    at [object Object].webdriver.promise.ControlFlow.runInNewFrame_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1654:20)
==== async task ====
Asynchronous test function: it()
    at [object Object].<anonymous> (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd\index.js:93:33)
    at [object Object].<anonymous> (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\async-callback.js:45:37)
    at [object Object].jasmine.Block.execute (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:1174:17)
    at [object Object].jasmine.Queue.next_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31)
    at [object Object]._onTimeout (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2199:18)
Error
    at [object Object].ElementArrayFinder.applyAction_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:389:21)
    at [object Object].self.(anonymous function) [as click] (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:77:19)
    at [object Object].self.(anonymous function) [as click] (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\lib\element.js:697:11)
    at [object Object].<anonymous> (C:\Jenkins\jobs\UI Automation\workspace\e2e-tests\new_message_with_attachment.js:223:62)
    at C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd\index.js:94:14
    at [object Object].webdriver.promise.ControlFlow.runInNewFrame_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1654:20)
    at [object Object].webdriver.promise.ControlFlow.runEventLoop_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1518:8)
Error
    at [object Object].<anonymous> (C:\Jenkins\jobs\UI Automation\workspace\e2e-tests\new_message_with_attachment.js:76:3)
    at [object Object].jasmine.Env.describe_ (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21)
    at [object Object].jasmine.Env.describe (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
    at describe (C:\Users\X\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
    at Object.<anonymous> (C:\Jenkins\jobs\UI Automation\workspace\e2e-tests\new_message_with_attachment.js:75:1)
Finished in 59.756 seconds
[31m1 test, 8 assertions, 2 failures
[0m
[launcher] 0 instance(s) of WebDriver still running
Build step 'Execute Windows batch command' marked build as failure
Discard old builds...
Sending e-mails to: me@home.com
Finished: FAILURE*

---------这是protractor.conf.js;

-------Here is the protractor.conf.js;

*exports.config = {
  allScriptsTimeout: 20000,
  specs: [
    'new_message_with_attachment.js',
  ],

  capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
    prefs: {'profile.managed_default_content_settings.notifications': 1}},
  shardTestFiles: true,
    maxSessions: 1

  },

  directConnect: true,
  jasmineNodeOpts: {
    defaultTimeoutInterval: 1000000
  }
};*

有何评论?

将量角器与Jenkins CI集成在一起对我来说曾经发生过一次.因此,根据您的问题,我将提出一些建议,这些建议将有助于提高e2e测试的稳定性.

It has been happened once for me when integrate Protractor with Jenkins CI. So according to your question, I will make some suggestions that will help to increase the stability of your e2e test.

似乎您在这里不仅遇到一个单一的问题.而且我相信这可以解决您的一些问题,但可能无法解决所有问题.

As it seem like you are not just having a single problems here. And I am sure this will solve some of your problems but may not solve all of it.

和诸如未找到元素"之类的错误很可能是由于未完成网页安装导致的..

and some errors like "element not found" are most probably because of the uncompleted webpage install..

...每次运行都会出现不同的错误

...I am getting different errors on every run

这是量角器最常见的问题,有很多原因导致这种情况发生.但是幸运的是,可以通过非常简单的方法与 结合使用来避免页面对象 .

This is the most common issue with protractor, there are many cause for this to happen. But fortunately it can be avoid with very simple method combine with Page Object.

这是页面对象:

var MyPage = function () {

    // SET ng-view or ui-view
    this.view = $('.view');

    // SET interactable components
    this.button = element(by.css('button'));

};

MyPage.prototype.get = function () {
    browser.setLocation('myRoute/something');
};

MyPage.prototype.waitForPresent = function() {
    var EC = protractor.ExpectedConditions;
    browser.wait(EC.presenceOf(this.button), 10000);
};

在您的测试套件/规格中:

In your test suite/spec:

it("should test something", function() {
    var page = new MyPage();
    page.get();

    browser.waitForAngular();
    page.waitForPresent();

    page.button.click();
});

这里的技巧是使用Expected条件并为页面对象waitForPresent编写原型,等待组件出现.选择一个您认为将在视图/页面上呈现的最后一个. (注意:您不需要使用页面对象,但是当您的套件变得更大时,要进行管理将是非常痛苦的事情)

The trick here is using Expected conditions and wrote a prototype for your page object waitForPresent , waiting for the component(s) to be presented. Pick one that you think it will be the last one to got rendered on your view/page. (NOTE: you don't need to use page objects, but that will be quite a pain to manage when you suites getting bigger)

但是测试运行非常快,并且某些错误(例如找不到元素")很可能是由于网页安装未完成.有时,它甚至可以输入带有一些丢失字符的登录用户名.

But the tests are running very fast, and some errors like "element not found" are most probably because of the uncompleted webpage install..Sometimes it can even enter the login username with some missing characters.

您似乎那里确实有很好的资源(CPU和RAM),但是您要说的是,有时登录时会丢失一些字符.这意味着您仍然缺乏其他资源,并且怀疑是图形的资源.图形资源几乎缺少所有服务器,因为它不一定总是需要渲染某些东西.而要解决此问题,则无需使用任何代码调整,技巧或变通办法.您至少必须尝试以下两种方法之一(或同时进行两种操作):

It seem like you does have very good resources there (CPU and RAM), but the point you said that sometimes it missed some characters when logging in. This mean you are still lack of resource else where, and suspecting is your Graphic's resources. Graphic reources is lacking almost all of servers, because it don't always need to render something. And to deal with this, there is no code adjustment, trick or work-around to use. You must at least try either of these (or do both):

  • 找到要运行的无头测试.因为渲染浏览器很重. (使用Chrome或FireFox,因为不建议将PhantomJS用于量角器)
  • 尝试增加您的图形资源.

编辑:github 调用element.sendKeys( )似乎没有发送所有它最初在旧版本的Webdriver上怀疑的密钥.但是最近使用最新版本的Webdriver的人也得到了它.原因不明,但我高度怀疑这是由于缺乏某种资源造成的.无论如何,这仅是为您提供信息.希望对您有所帮助:)

EDIT : there is an issue on github Calling element.sendKeys() seems to not send all the keys it was first suspecting on an old version of Webdriver. But recently people who used latest version of Webdriver got it too. Unknow cause, but I am highly doubt it was due to lacking of some sort of resources. Anyways this is just for your informations. Hope it is helped :)