量角器-当元素不可见且没有执行'else"时测试失败条件

Protractor - Test is failed when element is not visible and doesn't perform the "else" condition

本文关键字:else quot 条件 失败 测试 执行 元素 量角器      更新时间:2023-09-26

我试着用"if/else语句"来运行一个流,这取决于某些元素是否可见。

但是在这个元素不可见的情况下,它不会遇到"else条件",所有的测试用例都失败了。

请帮忙!谢谢。

测试用例:
   it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
   browser.wait(EC.visibilityOf(element(by.binding("buttonText"))), 30000);
    element(by.binding("buttonText")).click();
            var mainWindow;
            browser.getAllWindowHandles().then(
                function(handles) {
                    mainWindow = handles[0]; //at this point there should be only 1 window
                }
            );
            browser.getAllWindowHandles().then(function (handles) {    
               handles.forEach(function(handle) {
                 if (handle !== mainWindow) {
                    browser.switchTo().window(handle)
                    var loadLogin = element(by.id("loadLogin"));
                    element.all(by.id('loadLogin')).then(function(){ 
                        expect(browser.driver.getCurrentUrl()).toContain('www.paypal.com');
                        browser.sleep(10000); 
                    loadLogin.isDisplayed().then(function(result) {
                          if (result === true) {
                                loadLogin.click();
                                browser.driver.sleep(3000);
                                element(by.id("login_email")).sendKeys("username");
                                element(by.id("login_password")).sendKeys("pass");
                                browser.driver.sleep(1000);
                                element(by.id("submitLogin")).click();
                                browser.driver.sleep(5000);
                                element(by.id("submit.x")).click();
                                browser.driver.sleep(10000);
                                console.log("-=-=- first condition - I'm not logged-in -=-=-");
                            } else {
                                 browser.driver.sleep(3000);
                                 element(by.id("login_email")).clear();
                                 browser.driver.sleep(1000);
                                 element(by.id("login_email")).sendKeys("username");
                                 element(by.id("login_password")).sendKeys("pass");
                                 browser.driver.sleep(1000);
                                 element(by.id("submitLogin")).click();
                                 browser.driver.sleep(10000);
                                 element(by.id("submit.x")).click();
                                 browser.driver.sleep(10000);
                                 console.log("-=-=- second condition - I'm already logged-in -=-=-")
                            }
                        });
                    });
                 }
            });
        });
            browser.getAllWindowHandles().then(function (handles) {
                browser.switchTo().window(handles[0]);
            });
});

错误:[edited]

 New Client LiveSite - Payments - Perform a payment by PayPal - fail
Error occurred in loadLogin.isDisplayed: { [NoSuchElementError: No element found
 using locator: By.id("loadLogin")]
  code: 7,
  state: 'no such element',
  message: 'No element found using locator: By.id("loadLogin")',
  name: 'NoSuchElementError',
  stack: 'NoSuchElementError: No element found using locator: By.id("loadLogin")
'n    at new bot.Error (C:''Users''idan''AppData''Roaming''npm''node_modules''pr
otractor''node_modules''selenium-webdriver''lib''atoms''error.js:113:18)'n    at
 C:''Users''idan''AppData''Roaming''npm''node_modules''protractor''lib''element.
js:706:15'n    at [object Object].promise.ControlFlow.runInFrame_ (C:''Users''id
an''AppData''Roaming''npm''node_modules''protractor''node_modules''selenium-webd
river''lib''webdriver''promise.js:1877:20)'n    at [object Object].promise.Callb
ack_.goog.defineClass.notify (C:''Users''idan''AppData''Roaming''npm''node_modul
es''protractor''node_modules''selenium-webdriver''lib''webdriver''promise.js:246
4:25)'n    at [object Object].promise.Promise.notify_ (C:''Users''idan''AppData'
'Roaming''npm''node_modules''protractor''node_modules''selenium-webdriver''lib''
webdriver''promise.js:563:12)'n    at Array.forEach (native)'n    at Object.goog
.array.forEach (C:''Users''idan''AppData''Roaming''npm''node_modules''protractor
''node_modules''selenium-webdriver''lib''goog''array''array.js:203:43)'n    at [
object Object].promise.Promise.notifyAll_ (C:''Users''idan''AppData''Roaming''np
m''node_modules''protractor''node_modules''selenium-webdriver''lib''webdriver''p
romise.js:552:16)'n    at goog.async.run.processWorkQueue (C:''Users''idan''AppD
ata''Roaming''npm''node_modules''protractor''node_modules''selenium-webdriver''l
ib''goog''async''run.js:125:21)'n    at runMicrotasksCallback (node.js:337:7)'nE
rror'n    at [object Object].ElementArrayFinder.applyAction_ (C:''Users''idan''A
ppData''Roaming''npm''node_modules''protractor''lib''element.js:403:21)'n    at
[object Object].self.(anonymous function) [as isDisplayed] (C:''Users''idan''App
Data''Roaming''npm''node_modules''protractor''lib''element.js:76:19)'n    at [ob
ject Object].self.(anonymous function) [as isDisplayed] (C:''Users''idan''AppDat
a''Roaming''npm''node_modules''protractor''lib''element.js:733:11)'n    at c:''a
utomation''tests''paymentsNewClient.js:71:39'n    at [object Object].promise.Con
trolFlow.runInFrame_ (C:''Users''idan''AppData''Roaming''npm''node_modules''prot
ractor''node_modules''selenium-webdriver''lib''webdriver''promise.js:1877:20)'n
   at [object Object].promise.Callback_.goog.defineClass.notify (C:''Users''idan
''AppData''Roaming''npm''node_modules''protractor''node_modules''selenium-webdri
ver''lib''webdriver''promise.js:2464:25)'n    at [object Object].promise.Promise
.notify_ (C:''Users''idan''AppData''Roaming''npm''node_modules''protractor''node
_modules''selenium-webdriver''lib''webdriver''promise.js:563:12)'n    at Array.f
orEach (native)'n    at Object.goog.array.forEach (C:''Users''idan''AppData''Roa
ming''npm''node_modules''protractor''node_modules''selenium-webdriver''lib''goog
''array''array.js:203:43)'n    at [object Object].promise.Promise.notifyAll_ (C:
''Users''idan''AppData''Roaming''npm''node_modules''protractor''node_modules''se
lenium-webdriver''lib''webdriver''promise.js:552:16)' }

没有找到loadLogin元素。它没有说它不可见。

 NoSuchElementError: No element found using locator: By.id("loadLogin")
   Stacktrace:
     NoSuchElementError: No element found using locator: By.id("loadLogin")

看起来您打算在切换新窗口后与loadLogin元素进行交互,但在切换到窗口之前,您似乎也试图找到元素loadLogin。在测试开始时检查下面的代码。也许您需要切换到loadLogin元素存在的窗口,然后寻找它。

 it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
        var loadLogin = element(by.id("loadLogin"));
         browser.sleep(2000);

编辑1

当元素不可见时可能会抛出一些异常,这就是为什么你永远不会达到else条件的原因。也许可以添加这样的内容,

loadLogin.isDisplayed().then(function(result) {
      //keep your code here
   }, function(err) {
       console.log('Error occurred in loadLogin.isDisplayed:', err);
  });