Webdriver JS测试期间无法切换窗口

时间:2023-04-30
本文介绍了Webdriver JS测试期间无法切换窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 Selenium Webdriver 使用 Jasmine JS 进行测试期间,我无法切换窗口.

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

跑步者:量角器 JS

平台:Node.JS 上的 Selenium Webdriver

Platform: Selenium Webdriver on Node.JS

代码框架:Jasmine 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();
  });

});

来自 Node.JS 的错误:

The error from Node.JS:

UnknownError: null value in entry: name=null at new bot.Error (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibatomserror.js:113:18) at Object.bot.response.checkResponse (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibatoms
esponse.js:106:9) at C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibwebdriverwebdriver.js:377:20 at [object Object].promise.ControlFlow.runInFrame_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibwebdriverpromise.js:1877:20) at [object Object].promise.Callback_.goog.defineClass.notify (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibwebdriverpromise.js:2464:25) at [object Object].promise.Promise.notify_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibwebdriverpromise.js:563:12) at Array.forEach (native) at Object.goog.array.forEach (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibgoogarrayarray.js:203:43) at [object Object].promise.Promise.notifyAll_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibwebdriverpromise.js:552:16) at goog.async.run.processWorkQueue (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesselenium-webdriverlibgoogasync
un.js:125:21) From: Task: Asynchronous test function: it() at [object Object]. (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesjasminewdindex.js:93:33) at [object Object]. (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibasync-callback.js:45:37) at [object Object].jasmine.Block.execute (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:1174:17) at [object Object].jasmine.Queue.next_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2209:31) at [object Object].jasmine.Queue.start (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2162:8) at [object Object].jasmine.Spec.execute (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2503:14) at [object Object].jasmine.Queue.next_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2209:31) at onComplete (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2205:18) at [object Object].jasmine.Spec.finish (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2477:5) at [object Object].onComplete (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:2504:10)==== async task ==== Error at [object Object]. (c:automation	estspayments.js:44:3) at [object Object].jasmine.Env.describe_ (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:913:21) at [object Object].jasmine.Env.describe (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:898:15) at describe (C:UsersidanAppDataRoaming
pm
ode_modulesprotractor
ode_modulesminijasminenodelibjasmine-1.3.1.js:658:27) at Object. (c:automation	estspayments.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)

这是相关的css代码:

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">

推荐答案

UnknownError: null value in entry: name=null

UnknownError: null value in entry: name=null

此错误表示您正试图切换到具有 undefined 名称或句柄的窗口.也就是说,此时窗口没有打开.

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:

  • 您需要将两个 it 块组合成一个块,因为它们使用不同的浏览器窗口
  • 打开paypal支付页面的逻辑有问题——需要依次点击ContinueContinue to payment按钮
  • 您需要解析 getAllWindowHandles() 并继续在 then 功能块内工作
  • 将显式等待与"预期条件" 而不是使用 sleep() 进行测试不可靠、缓慢且脆弱
  • 无需使用browser.driver,使用browser(你正在测试一个角度页面)
  • 在打开一个新的非角度窗口之前将 ignoreSynchronization 设置为 true,在 afterEach()<中将其设置回 false/code> (点击后打开的非角度页面)
  • 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)

适合我的固定代码:

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);

isPresent()之后加上(注意结束的)):

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

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

另外,你有一个额外的 findElement 调用,没有参数,你需要删除它:

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

browser.driver.findElement();

这篇关于Webdriver JS测试期间无法切换窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!