Can't switch windows during testing by Webdriv

2020-04-18 08:15发布

问题:

I can't switch windows during my testing by Selenium Webdriver using Jasmine JS.

Runner: Protractor JS

Platform: Selenium Webdriver on Node.JS

Code framework: Jasmine JS

The code is below, Please note the second code block:

describe('payments', function() {

    // beforeEach(function() {
    //             browser.ignoreSynchronization = true;
    //         });

    // afterEach(function(){
    //     browser.ignoreSynchronization = false;
    //  });

    this.selectWindow = function (index) {

      browser.driver.wait(function() {
        return browser.driver.getAllWindowHandles().then(function (handles) {

            if(handles.length > index) {
              return true;
            }
          });
      });
      return browser.driver.getAllWindowHandles().then(function (handles) {
        return browser.driver.switchTo().window(handles[index]);
      });
    };


  it('Client LiveSite - Payments - Perform a payment', function() {

    browser.driver.get("https://www.vcita.com/v/bungee/home");
    browser.driver.sleep(5000);
    element(by.xpath("//div[@class='actions-row']//a[.='Make Payment']")).click();
    browser.driver.sleep(2000);
    element(by.name("amount")).sendKeys("5");
    // element(by.model("payou_request.payou.amount")).sendKeys("5");
    element(by.name("pay_for")).sendKeys("Mister Bungee");
    element(by.name("notes")).sendKeys("Please approve my payments. Thanks a lot, Dr. Kugler. +972545453435");
    element(by.name("email")).sendKeys("idanvcita@gmail.com");
    element(by.name("first_name")).click();
    browser.driver.sleep(5000);
    element(by.css("button.btn.ng-binding")).click();
      });

  it('paypal', function() {
    browser.ignoreSynchronization = false;
        browser.driver.getAllWindowHandles().then(function (handles) {
        browser.driver.switchTo().window(handles[1]);
    });
    element(by.id("pay_button")).click();
    browser.driver.sleep(10000);


  // it('Client LiveSite - Payments - PayPal Lightbox', function() {

    // handlePromise.then(function (handles) {
    //   var popUpHandle = handles[1];
    //   var handle = driver.switchTo().window(popUpHandle).getWindowHandle();
    //   expect (handle).toEqual(popUpHandle);
    //     });

    //expect (element(by.text("Your payment summary")).isPresent()).toBe(true);
    element(by.id("loadLogin")).click();
    browser.driver.sleep(1000);
    element(by.id("login_email")).sendKeys("email");
    element(by.id("login_password")).sendKeys("pass");
    element(by.id("submitLogin")).click();
    element(by.id("login_password")).click();
    element(by.id("submit.x")).click();
  });

});

The error from Node.JS:

UnknownError: null value in entry: name=null at new bot.Error (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18) at Object.bot.response.checkResponse (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:106:9) at C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:377:20 at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20) at [object Object].promise.Callback_.goog.defineClass.notify (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2464:25) 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) at Array.forEach (native) 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) at [object Object].promise.Promise.notifyAll_ (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:552:16) at goog.async.run.processWorkQueue (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21) From: Task: Asynchronous test function: it() at [object Object]. (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd\index.js:93:33) at [object Object]. (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\async-callback.js:45:37) at [object Object].jasmine.Block.execute (C:\Users\idan\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\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31) at [object Object].jasmine.Queue.start (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2162:8) at [object Object].jasmine.Spec.execute (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2503:14) at [object Object].jasmine.Queue.next_ (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31) at onComplete (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2205:18) at [object Object].jasmine.Spec.finish (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2477:5) at [object Object].onComplete (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2504:10)==== async task ==== Error at [object Object]. (c:\automation\tests\payments.js:44:3) at [object Object].jasmine.Env.describe_ (C:\Users\idan\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\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15) at describe (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27) at Object. (c:\automation\tests\payments.js:2:1) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12)

This is the relevant css code:

<title>Pay with a PayPal account - PayPal</title><meta name="description" content="PayPal is the safer, easier way to pay online without revealing your credit card number."><meta http-equiv="X-UA-Compatible" content="IE=9"><link media="screen" rel="stylesheet" type="text/css" href="https&#x3a;&#x2f;&#x2f;www&#x2e;paypalobjects&#x2e;com&#x2f;WEBSCR&#x2d;640&#x2d;20150318&#x2d;1/css/core/global.css"><link rel="stylesheet" type="text/css" href="https&#x3a;&#x2f;&#x2f;www&#x2e;paypalobjects&#x2e;com&#x2f;WEBSCR&#x2d;640&#x2d;20150318&#x2d;1/Merchant/css/AP/checkout.css">

回答1:

UnknownError: null value in entry: name=null

This error means you are trying to switch to a window with undefined name or handle. In other words, the window is not opened at the moment.

Also, there are multiple issues with the code you've presented:

  • you need to group two it blocks into single one since they use different browser windows
  • there is a problem in logic that opens the paypal payment page - you need to click Continue and Continue to payment buttons sequentually
  • you need to resolve the getAllWindowHandles() and continue working inside the then function block
  • use explicit waits with "Expected Conditions" instead of using sleep() that makes your test unreliable, slow and fragile
  • no need to use browser.driver, use browser (you are testing an angular page)
  • set ignoreSynchronization to true before opening a new non-angular window, set it back to false in afterEach() (Non-angular page opened after a click)

Fixed code that works for me:

describe('payments', function () {

    afterEach(function () {
        browser.ignoreSynchronization = false;
    });

    it('Client LiveSite - Payments - Perform a payment', function () {
        var EC = protractor.ExpectedConditions;

        browser.get("https://www.vcita.com/v/bungee/home");

        var makePayment = element(by.xpath("//div[@class='actions-row']//a[.='Make Payment']"));
        browser.wait(EC.visibilityOf(makePayment), 5000);
        makePayment.click();

        var amount = element(by.name("amount"));
        browser.wait(EC.visibilityOf(amount), 5000);
        amount.sendKeys("5");

        element(by.name("pay_for")).sendKeys("Mister Bungee");
        element(by.name("notes")).sendKeys("Please approve my payments. Thanks a lot, Dr. Kugler. +972545453435");
        element(by.name("email")).sendKeys("idanvcita@gmail.com");
        element(by.name("first_name")).click();

        browser.sleep(5000);

        // Click "Continue"
        element(by.xpath("//button[. = 'Continue']")).click();

        // Click "Continue to Payment"
        element(by.xpath("//button[. = 'Continue to Payment']")).click();

        browser.ignoreSynchronization = true;
        browser.getAllWindowHandles().then(function (handles) {
            browser.switchTo().window(handles[1]);

            var loadLogin = element(by.id("loadLogin"));
            browser.wait(EC.visibilityOf(loadLogin), 15000);

            loadLogin.click();

            // TODO: continue with payment
        });
    });

});

Initial answer:

The parenthesis are not grouped correctly. Replace:

expect (element(by.text("Your payment summary"))).isPresent().toBe(true);

with (note the closing ) after isPresent()):

expect (element(by.text("Your payment summary")).isPresent()).toBe(true);

Also, you have an extra findElement call with no arguments, which you need to remove:

browser.driver.findElement();