1

我試圖讓我的測試穩定, 但有時它不是等待集成彈出窗口中的所有元素,而是失敗。量角器 - 當打開彈出窗口時,它不會等待此窗口中的所有元素

這個測試用例是有點複雜,因爲它包括如果裏面另一份聲明中,如果爲了運行Firefox和Chrome瀏覽器相關的代碼以及聲明(他們有不同的處理與彈出窗口),

也第二個條件是用戶已經登錄並且不應該點擊id =「loadlogin」的元素。

被測元素的CSS:

<input type="submit" value="Log in to your PayPal account" id="loadLogin" name="login_button" class="parentSubmit"> 

測試案例 - 代碼:

it('Exisiting Client LiveSite - Payments - Continue To Payment', function() { 
    element(by.binding("client_zone.payous.new.continue")).click(); 
    waitPageToLoad(); 
}); 

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

    browser.wait(EC.visibilityOf(element(by.binding("buttonText"))), 15000); 
    element(by.binding("buttonText")).click(); 

    if (browser === 'firefox') { 
     browser.getAllWindowHandles().then(function (handles) {  
      browser.switchTo().window(handles[handles.length - 1]); 

      element.all(by.id('loadLogin')).then(function(){ 

      // expect(browser.driver.getCurrentUrl()).toContain('www.paypal.com'); 
       if (loadLogin) { 
        loadLogin.click(); 
        browser.driver.sleep(3000); 
              element(by.id("login_email")).sendKeys("username"); 
        element(by.id("login_password")).sendKeys("password"); 
        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 - FF -=-=-"); 
       } else { 
        element(by.id("login_email")).clear(); 
        browser.driver.sleep(1000); 
               element(by.id("login_email")).sendKeys("username"); 
        element(by.id("login_password")).sendKeys("password"); 
        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 - FF -=-=-") 
       } 
      }); 

      browser.getAllWindowHandles().then(function (handles) { 
       browser.switchTo().window(handles[0]); 
      }); 
     }); 
    } 

    else { 
     browser.getAllWindowHandles().then(function (handles) {  
      browser.switchTo().window(handles[1]); 

      element.all(by.id('loadLogin')).then(function(){ 
      // expect(browser.driver.getCurrentUrl()).toContain('www.paypal.com'); 
       if (loadLogin) { 
        loadLogin.click(); 
        browser.driver.sleep(3000); 
              element(by.id("login_email")).sendKeys("username"); 
        element(by.id("login_password")).sendKeys("password"); 
        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 - Chrome -=-=-"); 
       } else { 
        element(by.id("login_email")).clear(); 
        browser.driver.sleep(1000); 
        element(by.id("login_email")).sendKeys("username"); 
        element(by.id("login_password")).sendKeys("password"); 
        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 - Chrome -=-=-") 
       } 
      }); 

      browser.getAllWindowHandles().then(function (handles) { 
       browser.switchTo().window(handles[0]); 
      }); 
     }); 
    }; 
}); 

錯誤:

c:\automation\tests>protractor conf.js 
Using the selenium server at http://localhost:4444/wd/hub 
[launcher] Running 1 instances of WebDriver 
imapConnected 
LiveSite Portal - Existing client pay by PayPal 
    LiveSite - Home Page - pass 
    Exisiting Client LiveSite - Payments - Payment form - pass 
    LiveSite Portal - Exisiting client payments - Welcome note for existing client 
- pass 
    Exisiting Client LiveSite - Payments - Continue To Payment - pass 
-=-=- first condition - Chrome -=-=- 
    Exisiting Client LiveSite - Payments - Perform a payment by PayPal - fail 


Failures: 

    1) LiveSite Portal - Existing client pay by PayPal Exisiting Client LiveSite - 
Payments - Perform a payment by PayPal 
    Message: 
    ElementNotVisibleError: element not visible 
    (Session info: chrome=43.0.2357.130) 
    (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9 
cf),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any sta 
cktrace information) 
Command duration or timeout: 23 milliseconds 
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50' 
System info: host: 'vCitaQA', ip: '192.168.152.1', os.name: 'Windows 8.1', os.ar 
ch: 'amd64', os.version: '6.3', java.version: '1.8.0_25' 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEn 
abled=false, chrome={userDataDir=C:\Users\idan\AppData\Local\Temp\scoped_dir7328 
_2982}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, versi 
on=43.0.2357.130, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents= 
true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, 
browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEn 
abled=true}] 
Session ID: 64d27a4f28d3db93bbb59b8184c87a07 
    Stacktrace: 
    ElementNotVisibleError: element not visible 
    (Session info: chrome=43.0.2357.130) 
    (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9 
cf),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any sta 
cktrace information) 
Command duration or timeout: 23 milliseconds 
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50' 
System info: host: 'vCitaQA', ip: '192.168.152.1', os.name: 'Windows 8.1', os.ar 
ch: 'amd64', os.version: '6.3', java.version: '1.8.0_25' 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEn 
abled=false, chrome={userDataDir=C:\Users\idan\AppData\Local\Temp\scoped_dir7328 
_2982}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, versi 
on=43.0.2357.130, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents= 
true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, 
browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEn 
abled=true}] 
Session ID: 64d27a4f28d3db93bbb59b8184c87a07 
    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\se 
lenium-webdriver\lib\webdriver\webdriver.js:377:20 
    at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\idan\AppData\Ro 
aming\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\np 
m\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\promi 
se.js:552:16) 
    at goog.async.run.processWorkQueue (C:\Users\idan\AppData\Roaming\npm\node_m 
odules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21) 
Error 
    at [object Object].ElementArrayFinder.applyAction_ (C:\Users\idan\AppData\Ro 
aming\npm\node_modules\protractor\lib\element.js:403:21) 
    at [object Object].self.(anonymous function) [as click] (C:\Users\idan\AppDa 
ta\Roaming\npm\node_modules\protractor\lib\element.js:76:19) 
    at [object Object].self.(anonymous function) [as click] (C:\Users\idan\AppDa 
ta\Roaming\npm\node_modules\protractor\lib\element.js:733:11) 
    at c:\automation\tests\payments.js:94:35 
    at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\idan\AppData\Ro 
aming\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\np 
m\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\promi 
se.js:552:16) 
From: Task: Asynchronous test function: it() 
    at [object Object].<anonymous> (C:\Users\idan\AppData\Roaming\npm\node_modul 
es\protractor\node_modules\jasminewd\index.js:93:33) 
    at [object Object].<anonymous> (C:\Users\idan\AppData\Roaming\npm\node_modul 
es\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:1 
7) 
    at [object Object].jasmine.Queue.next_ (C:\Users\idan\AppData\Roaming\npm\no 
de_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31) 

    at onComplete (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\nod 
e_modules\minijasminenode\lib\jasmine-1.3.1.js:2205:18) 
    at [object Object].jasmine.WaitsForBlock.execute (C:\Users\idan\AppData\Roam 
ing\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.j 
s:2704:5) 
    at [object Object]._onTimeout (C:\Users\idan\AppData\Roaming\npm\node_module 
s\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2718:12) 
Error 
    at [object Object].<anonymous> (c:\automation\tests\payments.js:43:5) 
    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\n 
ode_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.<anonymous> (c:\automation\tests\payments.js:1:63) 

Finished in 19.473 seconds 
5 tests, 3 assertions, 1 failure 

[launcher] 0 instance(s) of WebDriver still running 
[launcher] chrome #1 failed 1 test(s) 
[launcher] overall: 1 failed spec(s) 
[launcher] Process exited with error code 1 

c:\automation\tests> element.all(by.id('loadLogin')).then(function(){ 
'element.all' is not recognized as an internal or external command, 
operable program or batch file. 

c:\automation\tests> 

回答

2

我覺得問題可能在於你沒有切換到正確的窗口,因爲窗口的順序可能是動態的。這是我的建議,

  1. Before clicking on the loadLogin button I would save the main window handle. After all window handles are nothing but an array of hash
  2. When you click on the login, just switch to the window that is NOT main window. This way you can perform login without having to blindly switching to a window. I think this will also simplify your firefox vs chrome logic

因此,像這樣的僞代碼,

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); 
     element.all(by.id('loadLogin')).then(function(){ 
      //do more stuff 
     } 
    } 
    } 
}); 
+0

非常感謝!你爲我解決了一個大問題! ;) –

+0

很高興它爲你工作。乾杯 – nilesh

相關問題