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

时间:2015-05-03 09:19:13

标签: javascript selenium-webdriver automation jasmine protractor

我在Selenium Webdriver测试期间无法使用Jasmine JS切换窗口。

Runner:Protractor JS

平台:Node.JS上的Selenium Webdriver

代码框架:Jasmine JS

代码如下,请注意第二个代码块:

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的错误:

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)

这是相关的css代码:

<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 个答案:

答案 0 :(得分:3)

  

UnknownError:条目中的null值:name = null

此错误表示您正在尝试切换到undefined名称或句柄的窗口。换句话说,此刻窗口没有打开。

此外,您提供的代码存在多个问题:

  • 您需要将两个it块组合成一个,因为它们使用不同的浏览器窗口
  • 逻辑中存在打开paypal付款页面的问题 - 您需要点击ContinueContinue to payment按钮
  • 您需要解析getAllWindowHandles()并继续在then功能块
  • 内部工作
  • 使用explicit waits"Expected Conditions"而不是sleep()使您的测试不可靠,缓慢而脆弱
  • 无需使用browser.driver,使用browser(您正在测试角度页面)
  • 在打开新的非角度窗口之前将ignoreSynchronization设置为true,并将其设置回false afterEach()中的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 }); }); });

修复了适用于我的代码:

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

初步答复:

括号未正确分组。替换:

)

with(注意isPresent()之后的结束expect (element(by.text("Your payment summary")).isPresent()).toBe(true); ):

findElement

此外,您还有一个没有参数的额外browser.driver.findElement(); 调用,您需要将其删除:

package animals;

public class AnimalstestDrive {

    public Animals[] myZoo = new Animals[5];
    int zooCounter = 0;

    public static void main(String[] args) {

        //Set animals array
        Dog zooDog = new Dog();
        addAnimals(zooDog);

        Cat zooCat = new Cat();
        addAnimals(zooCat);

    }

    public void addAnimals(Animals a){
        if ( zooCounter > 5 ){
            myZoo[zooCounter] = a;
            zooCounter++;
        }
        else
            System.out.println("Zoo is full");
    }
}