javascript 量角器:如何比较单击按钮前后同一网页元素的文本内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21909220/
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 21:58:55  来源:igfitidea点击:

Protractor : how to compare the text content of the same web element before and after clicking on a button

javascriptangularjsjasmineprotractor

提问by Ziwdigforbugs

I would like to test a filtering function in my angularJS app. In fact when I click on the filter the number of search results displayed on the page should decrease Here is is my code so far :

我想在我的 angularJS 应用程序中测试过滤功能。事实上,当我点击过滤器时,页面上显示的搜索结果数量应该减少这是我的代码:

    element(by.id('foundNumber')).getText().then (function(text){console.log(text); })
    element(by.repeater('term in facets.uproctype').row(0)).click ()
    element(by.id('foundNumber')).getText().then (function(text){console.log(text); })

And here is my console log :

这是我的控制台日志:

Using the selenium server at http://localhost:4444/wd/hub
6209
6195
.... 

I don't know how can I compare theses two values in an expect line as I can't use them inside their function. Any help?

我不知道如何在期望行中比较这两个值,因为我不能在它们的函数中使用它们。有什么帮助吗?

Thanks Zied

谢谢齐德

回答by rjferguson21

I believe you would have to nest your then functions to ensure the original value is available.

我相信您必须嵌套 then 函数以确保原始值可用。

element(by.id('foundNumber')).getText().then( function(original_text) {

  element(by.repeater('term in facets.uproctype').row(0)).click ();

  element(by.id('foundNumber')).getText().then( function(new_text){
    expect(original_text).not.toBe(new_text);
  });

});

This link also might be helpful. https://code.google.com/p/selenium/wiki/WebDriverJs#Control_Flows

此链接也可能有帮助。 https://code.google.com/p/selenium/wiki/WebDriverJs#Control_Flows

回答by Max

Another way is to schedule the comparison at the protractor control flow so it will be executed after all values are available.

另一种方法是在量角器控制流中安排比较,以便在所有值可用后执行。

var oldValue,newValue;
element(by.id('foundNumber')).getText().then(function(text){oldValue=text})
element(by.repeater('term in facets.uproctype').row(0)).click()
element(by.id('foundNumber')).getText().then(function(text){newValue=text})

protractor.promise.controlFlow()
  .execute(function(){return protractor.promise.fulfilled()},'wait for control flow')
    .then(function(){
      expect(oldValue).not.toEqual(newValue);
    });

There is a nice explanation of flow.execute() in this blog post.

这篇博文中对 flow.execute() 有很好的解释。

回答by Michal Charemza

I suspect you can just compare the results of .getText()from different points using expect

我怀疑您可以使用以下方法比较.getText()不同点的结果expect

var text1 = element(by.id('foundNumber')).getText();
element(by.repeater('term in facets.uproctype').row(0)).click();
var text2 = element(by.id('foundNumber')).getText();
expect(text1).not.toBe(text2);

expecthandles unwrapping of the promises passed to it and actually compares resolved values.

expect处理传递给它的承诺的解包,并实际比较已解析的值。