• <i id='9SIjb'><tr id='9SIjb'><dt id='9SIjb'><q id='9SIjb'><span id='9SIjb'><b id='9SIjb'><form id='9SIjb'><ins id='9SIjb'></ins><ul id='9SIjb'></ul><sub id='9SIjb'></sub></form><legend id='9SIjb'></legend><bdo id='9SIjb'><pre id='9SIjb'><center id='9SIjb'></center></pre></bdo></b><th id='9SIjb'></th></span></q></dt></tr></i><div id='9SIjb'><tfoot id='9SIjb'></tfoot><dl id='9SIjb'><fieldset id='9SIjb'></fieldset></dl></div>

    <small id='9SIjb'></small><noframes id='9SIjb'>

        <bdo id='9SIjb'></bdo><ul id='9SIjb'></ul>

      1. <tfoot id='9SIjb'></tfoot>
        <legend id='9SIjb'><style id='9SIjb'><dir id='9SIjb'><q id='9SIjb'></q></dir></style></legend>

        断言元素被聚焦

        时间:2023-10-12
        1. <i id='tMwCk'><tr id='tMwCk'><dt id='tMwCk'><q id='tMwCk'><span id='tMwCk'><b id='tMwCk'><form id='tMwCk'><ins id='tMwCk'></ins><ul id='tMwCk'></ul><sub id='tMwCk'></sub></form><legend id='tMwCk'></legend><bdo id='tMwCk'><pre id='tMwCk'><center id='tMwCk'></center></pre></bdo></b><th id='tMwCk'></th></span></q></dt></tr></i><div id='tMwCk'><tfoot id='tMwCk'></tfoot><dl id='tMwCk'><fieldset id='tMwCk'></fieldset></dl></div>

            <bdo id='tMwCk'></bdo><ul id='tMwCk'></ul>

              <small id='tMwCk'></small><noframes id='tMwCk'>

            • <tfoot id='tMwCk'></tfoot>

                    <tbody id='tMwCk'></tbody>
                • <legend id='tMwCk'><style id='tMwCk'><dir id='tMwCk'><q id='tMwCk'></q></dir></style></legend>
                • 本文介绍了断言元素被聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  根据我如何断言一个元素是有焦点的? 线程,您可以通过切换到 activeElement() 来检查元素是否获得焦点,并断言这是您期望获得焦点的同一元素:

                  According to the How do I assert an element is focused? thread, you can check if an element is focused by switching to an activeElement() and assert this is the same element you've expected to have the focus:

                  expect(page.element.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
                  

                  在我的例子中,当前聚焦的元素没有id属性.

                  In my case, the currently focused element does not have an id attribute.

                  我应该怎么做而不是检查 id?

                  What should I do instead of checking an id?

                  奖励问题:此外,正如您从我尝试解决它的尝试中看到的那样,看起来我不能期望/断言一个元素(或 Web 元素)作为一个完整的对象.为什么?

                  Bonus question: Also, as you can see from my tries to solve it, it looks like I cannot expect/assert an element (or web element) as a complete object. Why?

                  我试过了:

                  expect(page.element).toEqual(browser.driver.switchTo().activeElement());
                  

                  但是由于我什至无法理解的错误而失败 - 有一个巨大的回溯(在控制台中滚动大约需要 10 分钟),但内部没有用户友好的错误.

                  But is is failing with an error I cannot even understand - there is a huge traceback (it is about 10 minutes to scroll in the console), but no user-friendly error inside.

                  我也尝试过使用 getWebElement():

                  expect(page.element.getWebElement()).toEqual(browser.driver.switchTo().activeElement());
                  

                  但这导致了以下错误:

                  错误:期望使用 WebElement 参数调用,期望一个 Promise.做过你的意思是使用 .getText()?

                  Error: expect called with WebElement argument, expected a Promise. Did you mean to use .getText()?

                  使用最新的量角器开发版本.

                  Using the latest protractor development version.

                  推荐答案

                  在我的回答中,我将假设 activeElempageElem 都是量角器元素查找器,并且指向同一个网络元素.

                  In my answer I'm going to assume activeElem and pageElem are both protractor element finders, and are pointing to the same web element.

                  首先回答你关于为什么的问题

                  First to answer your question about why

                  expect(activeElem).toEqual(pageElem);
                  

                  进入一个无限循环,这是因为量角器在断言之前修补了jasmine的 expect 以解决promise,因此 expect(activeElem.getText()).toEqual('text'); 无需执行即可工作

                  Gets into an infinite loop, it's because protractor patched jasmine's expect to resolve the promise before asserting, so that things like expect(activeElem.getText()).toEqual('text'); works without having to do

                  activeElem.getText().then(function(text) {
                    expect(text).toEqual('text');
                  })
                  

                  你可以说,为什么不只解决一次承诺呢?但是还有嵌套的 Promise.

                  You could say, why not just resolve the promise once? But then there are nested promises.

                  所以现在您可能会认为这是一个问题,但实际上并不是因为您永远不会在实际用例中比较两个 elementFinder.Jasmine 的 toEqual 进行引用检查,而不是深度比较,因此 expect(activeElem).toEqual(pageElem) 与简单的引用比较相同:(activeElem === pageElem).toToTruthy(),这样做真的没有意义.(注意 element(by.css('html')) === element(by.css('html')) 是假的,因为它不是同一个引用.)

                  So now you might be thinking this is an issue, but it really isn't because you would never compare two elementFinders in a real use case. Jasmine's toEqual does a reference check, and not a deep compare, so expect(activeElem).toEqual(pageElem), is just the same as a simple reference comparison: (activeElem === pageElem).toToTruthy(), and there's really no point doing that. (Note element(by.css('html')) === element(by.css('html')) is false because it's not the same reference.)

                  所以,回答这个线程的真正问题:如何查看两个 elementFinder 是否具有相同的底层 web 元素:

                  So, to answer the real question for this thread: how to see if two elementFinders have the same underlying webelements:

                  expect(activeElem.getId()).toEqual(pageElem.getId());
                  

                  这篇关于断言元素被聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何让量角器按回车键? 下一篇:Protractor Cucumber BDD 测试在执行前显示通过

                  相关文章

                  最新文章

                  <i id='J8lZs'><tr id='J8lZs'><dt id='J8lZs'><q id='J8lZs'><span id='J8lZs'><b id='J8lZs'><form id='J8lZs'><ins id='J8lZs'></ins><ul id='J8lZs'></ul><sub id='J8lZs'></sub></form><legend id='J8lZs'></legend><bdo id='J8lZs'><pre id='J8lZs'><center id='J8lZs'></center></pre></bdo></b><th id='J8lZs'></th></span></q></dt></tr></i><div id='J8lZs'><tfoot id='J8lZs'></tfoot><dl id='J8lZs'><fieldset id='J8lZs'></fieldset></dl></div>

                    • <bdo id='J8lZs'></bdo><ul id='J8lZs'></ul>
                    <tfoot id='J8lZs'></tfoot>

                    <legend id='J8lZs'><style id='J8lZs'><dir id='J8lZs'><q id='J8lZs'></q></dir></style></legend>

                    1. <small id='J8lZs'></small><noframes id='J8lZs'>