2016-11-29 85 views
0

我試圖習慣於使用Selenium進行自動化測試。到目前爲止,我已經成功地完成了這個測試用例的所有方面,除了最後一步是檢查是否存在警報(已收到交易確認)錯誤:驅動程序無法解析爲變量 - 硒java日食

我試圖用布爾值來執行此功能,但我一直在同樣的錯誤:驅動程序無法解析爲變量。

有什麼建議嗎?

package com.scotia.test;  

import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class ScotiaTest1 { 

    public static void main(String[] args) throws InterruptedException { 
     System.setProperty("webdriver.chrome.driver","/Users/theone/Downloads/chromedriver-new");   

     // Create a new instance of the Chrome driver 
     WebDriver driver = new ChromeDriver();      

     //Login using Username and Password 
     driver.get("https://LEAP:[email protected]/LEAP_Prototype/desktop/html/Chile_index.html#"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Find Proximity Serie A link and click on it 
     driver.findElement(By.cssSelector("#investing_tab tr:nth-child(7) a.acct-name")).click(); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Find New Funds Button and click on it 
     driver.findElement(By.cssSelector(".pad-top-10.txt-right .btn.action-btn")).click(); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Rescue investment and choose Rescue 
     Select dropdown = new Select(driver.findElement(By.id("mf_action"))); 
     dropdown.selectByVisibleText("Inversión"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Current account and choose current account 
     dropdown = new Select(driver.findElement(By.id("selaccount_drpdown"))); 
     dropdown.selectByVisibleText("Cuenta Corriente *** 0002 USD 10.000,00"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Fund Type and choose medium term 
     dropdown = new Select(driver.findElement(By.id("term"))); 
     dropdown.selectByVisibleText("Mediano Plazo"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Mutual Fund Name and choose Proximity Series A 
     dropdown = new Select(driver.findElement(By.id("typefund"))); 
     dropdown.selectByVisibleText("Proximidad Serie A"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Fund account Name and choose 001 
     dropdown = new Select(driver.findElement(By.id("sub_accnt"))); 
     dropdown.selectByVisibleText("001"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Select Field Rode and type 222 
     driver.findElement(By.id("amount_field")).sendKeys("222"); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Find to Accept Button and click on it 
     driver.findElement(By.id("next")).click(); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     //Find to Confirm Button and click on it 
     driver.findElement(By.id("next")).click(); 

     //Wait for 5 Sec 
     Thread.sleep(2000); 

     // Print a Log In message to the screen 

     //Wait for 5 Sec 
     Thread.sleep(2000); 
    }  

    public boolean isAlertPresent(){ 
     try{ 
      Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent()); 
      if(a!=null){ 
       System.out.println("Alert is present"); 
       driver.switchTo().alert().accept(); 
       return true; 
      }else{ 
       throw new Throwable(); 
      } 
     } 
     catch (Throwable e) { 
      System.err.println("Alert isn't present!!"); 
      return false; 
     } 

     //Wait for 5 Sec 
     Thread.sleep(2000);   
    } 

} 

回答

1

這是您正在使用的實際代碼?它有很多問題:isAlertPresent()不被調用,你在那裏使用驅動程序,但它不是類的字段等等(你有「錯誤:驅動程序無法解析爲變量」因此)
I將其修改爲可運行(將isAlertPresent()併入main()),並在最後得到了綠色「警報」。你的意思是?如果是的話,它不是一個警告,它只是一個div,所以不是這樣的:

Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent()); 

寫這樣的事情:

WebElement div = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.className("success-msg"))); 

如果不是這種情況,請編輯與您的問題提醒你的意思。 enter image description here