当使用isDisplayed时,结果是"预期false为真",即使在屏幕上显示web元素

时间:2016-07-01 07:15:54

标签: protractor

在我的spec文件中,我正在识别一个web元素,然后执行取消发布等操作,然后通过将按钮标签验证为" Publish"来识别新状态。

我的Article_Spec文件如下,

    //Article_spec//

'use strict';

var FunLib = require('/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/FFAutomation/Function_Lib.js'); 
var ArticlePO = require('/Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/FFAutomation/Article_PO.js'); 

describe('News: ', function() {

var FuncLib;
var Article;

FuncLib = new FunLib();
Article = new ArticlePO();

//scenario 1: Navigate to News Menu
it('Navigate to News menu', function() {
    browser.ignoreSynchronization = true;
    FuncLib.SelectMenu.get(0).click(); //Click on News Menu
    console.log('Article menu is clicked.');
    browser.sleep(3500);
    expect(FuncLib.SelectSubMenu.getText()).toEqual(["News", "Partners", "My News", "New article"]); // Verify the Submenus: News, Partners, My News, New article are present
});
//scenario 15: Verify published news can be unpublished
it('Verify published news can be unpublished', function() {
    FuncLib.SelectSubMenu.get(2).click(); // Click on "New article" submenu 
    console.log('My News submenu is clicked.');
    browser.sleep(7000);
    Article.MyArticle.isDisplayed().then(function(result1) {
    console.log(result1);
        if (result1){
            Article.MyArticle.click();
            Article.Unpublish.isDisplayed().then(function(result2) {
                if (result2) {
                    console.log('Published article is available');
                    Article.Unpublish.click();
                    Article.Publish.isDisplayed().then(function(result3) {
                        if (result3) {
                        console.log('Article unpublished successfully');
                        }
                        else {
                        console.log('Article is not unpublished.');
                        }
                    });
                }
                else {
                console.log('Can not unpublish as all articles are unpublished.');
                }
            });
        }
        else {
        console.log('No articles are available on My News screen');
        }
    });
});

我的文章PO文件如下,

//Article Page object file - Article_PO.js
'use strict';

var ArticlePO = function(){

this.Title = element(by.model('article.title'));
this.Header = element(by.model('article.header'));
this.ImgFooter = element(by.model('article.cover_footer'));
this.ArtDsc = element.all(by.css('.ng-pristine.ng-untouched.ng-valid.ta-bind')).get(0);
this.KeyWrd = element(by.model('$mdChipsCtrl.chipBuffer'));
this.msg1 = "Title is missing";
this.msg2 = "Insert title, image, header and body before publishing your New.";
this.msg3 = "ARTICLE SAVED";
this.msg4 = "ERROR_UPLOADING_FILE";
this.MyArticle = element.all(by.css('.md-whiteframe-z2.controls.layout-column')).get(0);
//element.all(by.css('[ng-class="{ notint: !item.is_published }"]'));

this.PreviewTab = element.all(by.css('.md-fab.md-accent.md-mini.md-button.md-white-theme.md-ink-ripple')).first();
this.EditTab = element.all(by.css('.md-fab.md-accent.md-mini.center.md-button.md-white-theme.md-ink-ripple'));
this.Unpublish = element.all(by.buttonText("UNPUBLISH"));
this.Publish = element.all(by.buttonText("PUBLISH"));
this.deleteArt = element(by.css('[ng-click="deleteArticle($event, item)"]'));
this.LastArticle = element.all(by.css('[ng-class="{ notint: !item.is_published }"]')).last();
this.ConfirmDelete = element(by.css('.md-dialog-content'));
};
module.exports = ArticlePO;

我面临的问题是,即使我的屏幕有多篇文章

Article.MyArticle.isDisplayed().then(function(result1) { 

返回为假。因此,此脚本的输出是“我的新闻”屏幕上没有可用的文章'。

控制台输出如下,

Rohits-MacBook-Pro:FFAutomation rohitgathibandhe$ /Users/rohitgathibandhe/npm-global/lib/node_modules/protractor/bin/protractor conf.js
Report destination:   target/screenshots/Report.html
Using FirefoxDriver directly...
[launcher] Running 1 instances of WebDriver
Started
Browser title is: ForFirm
.When registred email Id and password is entered user logged in successfully
Logged in user is : Guglielmo Della Valle
.Article menu is clicked.
.My News submenu is clicked.
false
No articles are available on My News screen

4 specs, 0 failures
Finished in 131.21 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] firefox #01 passed
Closing report

其他详细信息如下:protractor@3.2.2,nodeVersion:4.2.4,npmVersion:2.14.12,jasmine:2.4.1,selenium-webdriver:2.52.0,firefox:46.0​​.1

请告诉我这里出了什么问题。我希望得到正确的结果。

下面为对象添加了HTML代码:第一篇文章

<div ng-class="{ notint: !item.is_published }" ng-if="item.manageable" class="manage overlay ng-scope layout-column flex"><div class="md-whiteframe-z2 controls layout-column"><div class="button-row layout-row"><div class="border-right flex"><a class="md-fab md-accent md-mini md-button md-white-theme md-ink-ripple" ng-transclude="" href="/article/view/378-test" md-theme="white"><ng-md-icon icon="visibility" size="20" class="ng-scope"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20"><path d="M12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-12.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5z"></path><path d="M12 9c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"></path></svg></ng-md-icon></a></div><div class="border-right flex"><a class="md-fab md-accent md-mini center md-button md-white-theme md-ink-ripple" ng-transclude="" href="/article/edit/378" md-theme="white"><ng-md-icon icon="edit" size="20" class="ng-scope"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25z"></path><path d="M20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"></path></svg></ng-md-icon></a></div><div class="flex"><button class="md-fab md-accent md-mini right md-button md-white-theme md-ink-ripple" type="button" ng-transclude="" ng-click="deleteArticle($event, item)" md-theme="white"><ng-md-icon icon="delete" size="20" class="ng-scope"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20"><path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12z"></path><path d="M19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"></path></svg></ng-md-icon></button></div></div><button class="md-raised md-primary md-hue-2 md-button md-noga-theme md-ink-ripple" type="button" ng-transclude="" ng-click="togglePublish(item)" md-theme="noga" ng-show="item.is_published" aria-hidden="false"><span class="ng-binding ng-scope">UNPUBLISH</span></button><button class="md-raised md-primary md-hue-1 md-button md-ink-ripple ng-hide" type="button" ng-transclude="" ng-click="togglePublish(item)" ng-show="!item.is_published" aria-hidden="true"><span class="ng-binding ng-scope">PUBLISH</span></button></div></div>

3 个答案:

答案 0 :(得分:1)

首先,感谢提供整个测试,控制台输出,页面对象和html。非常有帮助!

一个问题是控制台日志消息未显示在控制流的上下文中。也就是说,消息可能在测试开始运行之前出现,因为当console.log立即发生时,量角器操作被推迟为异步调用。要解决此问题,请将console.log消息放在延迟语句中,如下所示:

//scenario 1: Navigate to News Menu
it('Navigate to News menu', function() {
    browser.ignoreSynchronization = true;
    //Click on News Menu
    FuncLib.SelectMenu.get(0).click().then(function() {
        // put console.log HERE so that it executes at the right time
        console.log('Article menu is clicked.'); 
    }); 
    browser.sleep(3500).then(function() {
        console.log('slept for 3500ms');
    });
    expect(FuncLib.SelectSubMenu.getText()).toEqual(["News", "Partners", "My News", "New article"]); // Verify the Submenus: News, Partners, My News, New article are present
});

//scenario 15: Verify published news can be unpublished
it('Verify published news can be unpublished', function() {
    // Click on "New article" submenu 
    FuncLib.SelectSubMenu.get(2).click().then(function() {
        console.log('My News submenu is clicked.');
    });
    browser.sleep(7000).then(function() {
        console.log('slept for 7000ms');
    });
    Article.MyArticle.isDisplayed().then(function(articleIsDisplayed) {
        console.log('articleIsDisplayed: ' + articleIsDisplayed);
        if (articleIsDisplayed){
            Article.MyArticle.click();
            Article.Unpublish.isDisplayed().then(function(unpublishIsDisplayed) {
                if (unpublishIsDisplayed) {
                    console.log('Published article is available');
                    Article.Unpublish.click();
                    Article.Publish.isDisplayed().then(function(publishIsDisplayed) {
                        if (publishIsDisplayed) {
                            console.log('Article unpublished successfully');
                        }
                        else {
                            console.log('Article is not unpublished.');
                        }
                    });
                }
                else {
                console.log('Can not unpublish as all articles are unpublished.');
                }
            });
        }
        else {
            console.log('No articles are available on My News screen');
        }
    });
});

第一个测试中的另一个问题是browser.ignoreSynchronization = true,但第二个测试中没有。{如果页面是有角度的,则不需要忽略同步(并且您不需要所有browser.sleep命令)。但是,如果确实需要它,它应该在两个测试中,或者在beforeEach

由于isDisplayed承诺看起来正确,它可能是时间问题(点击后可能需要超过7秒?),或者选择器错误,或者pageObject实现不是我做得很对。

在测试中,尝试使用Article.MyArticle.isDisplayed()替换element(by.css('.md-whiteframe-z2.controls.layout-column')).isDisplayed()以尝试在不使用pageObject的情况下查找元素。如果可行,请尝试在测试中将其更改为element.all(...).get(0).isDisplayed()。如果能够以这种方式找到元素,那么我们可以更新pageObjects。

答案 1 :(得分:0)

我站得住了!在您的情况下使用first()get(0)是相同的。我认为这里的问题是在执行测试之前调用页面对象中的元素,如 -

  this.MyArticle = element.all(by.css('.md-whiteframe-z2.controls.layout-column')).get(0);

上面的代码只尝试访问页面对象中的元素,当尝试在测试中使用它时它不起作用。

在页面对象中,应该只定义元素:

 this.MyArticle = element.all(by.css('.md-whiteframe-z2.controls.layout-column'));

然后在测试中对该元素执行一些操作:

Article.MyArticle.get(0).isDisplayed().then(function(result1) {
console.log(result1);
    if (result1){
        Article.MyArticle.click();

这肯定会起作用并且提升 Martin的答案,因为他也指定了这个。

答案 2 :(得分:0)

browser.waitForAngular().then(function(){
    #your code here
    return this;
}

可能没有加载元素,你必须等待它出现。