2013-03-26 79 views
0

當我輸入錯誤的用戶名和密碼,一個彈出消息和一個確定按鈕是有阻止我的應用程序。但我無法找到該按鈕的ID因爲螢火蟲沒有在這方面的工作。我搜索了更多的博客,我知道它是操作系統生成彈出,它可以由AutoIT處理。請幫助我如何處理彈出框。這裏是我的代碼。如何處理操作系統生成彈出使用WebDriver

![package test; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 

import jxl.Sheet; 
import jxl.Workbook; 
import jxl.read.biff.BiffException; 
import jxl.write.Label; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import jxl.write.WriteException; 
import jxl.write.biff.RowsExceededException; 

import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.NoAlertPresentException; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.Test; 

public class Read { 

    public WebDriver driver; 

    @BeforeMethod 
    public void launch() throws Exception { 
     System.setProperty("webdriver.chrome.driver", 
       "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } 

    @Test 
    public void testImportexport1() throws BiffException, IOException, 
      RowsExceededException, WriteException, InterruptedException { 

     FileInputStream fis = new FileInputStream("Data//Logindev.xls"); 
     Workbook w = Workbook.getWorkbook(fis); 
     Sheet s = w.getSheet(0); 
     String a\[\]\[\] = new String\[s.getRows()\]\[s.getColumns()\]; 

     FileOutputStream fos = new FileOutputStream("Data//Logindev_1.xls"); 
     WritableWorkbook wwb = Workbook.createWorkbook(fos); 
     WritableSheet ws = wwb.createSheet("LoginResult", 0); 

     System.out.println("s.getRows() = " + s.getRows()); 

     for (int i = 0; i < s.getRows(); i++) { 
      System.out.println("s.getColumns() = " + s.getColumns()); 

      for (int j = 0; j < s.getColumns(); j++) { 
       a\[i\]\[j\] = s.getCell(j, i).getContents(); 
       Label l = new Label(j, i, a\[i\]\[j\]); 
       Label l1 = new Label(2, 0, "Result"); 

       ws.addCell(l); 
       ws.addCell(l1); 


      } 
     } 
     for (int i = 1; i < s.getRows(); i++) { 
      driver.get("url"); 

      driver.findElement(By.name("txtUserName")).sendKeys(
        s.getCell(0, i).getContents()); 
      driver.findElement(By.name("txtPwd")).sendKeys(
        s.getCell(1, i).getContents()); 
      driver.findElement(By.name("btnSignIn")).click(); 

      Thread.sleep(15000); 

      // System.out.println("Title = " + driver.getTitle()); 
      if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) { 
       System.out.println("Element is found"); 
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
       driver.findElement(
         By.xpath("//*\[@id='ctl00_headerContent_lnkLogOut'\]")) 
         .click(); 
       Thread.sleep(2000); 
       Label l2 = new Label(2, i, "Pass"); 
       ws.addCell(l2); 
      } else if (driver.getTitle().equalsIgnoreCase(
        "Welcome to ShopMyFarm")) { 

       driver.switchTo().alert().accept(); 
       Thread.sleep(5000); 
       System.out.println("Element Not Found"); 
       Label l2 = new Label(2, i, "Fail"); 
       ws.addCell(l2); 
      } 
     } 
     Thread.sleep(2000); 
     wwb.write(); 
     wwb.close(); 
    } 
}][1] 
+0

我已經更新了我對於您爲前一個問題的共享代碼響應,檢查是否是有幫助 – CheryJose 2013-03-26 12:31:05

+0

您是指原生窗口操作系統生成彈出?張貼生成的彈出窗口的屏幕截圖。 – Manigandan 2013-03-27 11:49:37

回答

0

WebDriver無法直接與對話窗口交互,這是因爲對話窗口是操作系統的域而不是網頁。我想你可以在java中使用Robot類來完成你想要做的鍵盤操作,就像在你的情況下按下Enter鍵來關閉操作系統對話框一樣。

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

public class RobotExp 
{ 

    public static void main(String[] args) 
    { 

    try { 

      Robot robot = new Robot(); 
      robot.keyPress(KeyEvent.VK_ENTER); 
     } 
    catch (AWTException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

嗨CheryJose,我試過你的代碼。但它不適合我。我分享了我的代碼。請給我任何建議。 – sarmila 2013-03-28 06:50:09

相關問題